vim’s lcd command

I find vim’s :lcd command to be quite useful. For a long time I didn’t use it, mainly because I didn’t understand how it works.

First, using it is simple. It works just like :cd. You type :lcd path/to/directory in command mode. The path is relative to the current directory, so :lcd .., :lcd ~/Desktop, and :lcd /etc/apache2 all work.

The :lcd command changes the path of the current window. The current window is the current frame within the current tab. Now, what’s really neat, is that a new window (created from commands such as :sp and :tabedit) gets the local directory of the window it’s created from.

Most plugins respect it. Both Command-T and ack.vim search within the local current directory. Another plugin, fugitive, bases its commands on the git repository of the current file so it works smoothly with different local directories as well.

Bang commands (like :!cp README.md ~/Desktop) also use the local current directory.

Finally, after using it for a while it gets easy to tell which directory I’m in. The status line displays the path of the current file based on the local current directory, so if I know where the current file is I can quickly figure out where the local current directory is.

I used it the other day with bundle show to dig through the source of two dependencies of my Rails project to better understand them.

If you use vim and haven’t got in the habit of using :lcd I highly recommend it. If you use another editor and wish it handled working in multiple directories gracefully, consider giving vim a shot!

5 Responses to vim’s lcd command

  1. Caleb,

    I like NERDTree too; I haven’t been using it lately, though. I installed MacVim with a GUI file browser, and I don’t find it nearly as useful as NERDTree. When I get a chance I’m going to go back to regular MacVim.

    I don’t like having the file drawer turned on by default, though. I prefer to have more space for long lines or vertical splits, or nothing at all.

    • Heh, that sidebar in the screenshots looks ridiculous. Basically seems like the opposite of vim…but yeah, NERDTree is pretty sweet.

      I bind leader-t to NERDTree’s toggle command and I generally hide it when I’m not using it. This has the added advantage that there is a single command to move focus directly to the tree and open a new file in my most recent split.

      I also have the luxury of using pretty big screens most of the time…my regular work monitor can display three vertical splits side-by-side comfortably (110-char lines with a reasonable font size) if I leave NERDTree closed.

  2. Cool post. I have one small suggestion:

    If you don’t want to type out % or %:h everytime you want to change to the current buffer or it’s parent directory, you should check out my plugin Fuzzee.vim which has a cool trick for doing :lcd :cd :e[dit] :b[uffer] commands relative to the current working directory or current buffer depending on how you shape your arguments.

    Cheers!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>