tags:

views:

71

answers:

2

How can I change my working directory to the path of the file that I currently have open?

Example

  1. current working directory is $HOME
  2. vim /tmp/test.log
  3. ???
  4. CWD is now /tmp/
+4  A: 

You can just type

:cd %:h

since %:h will be replaced by the head of the path to the current file.

Peter
+5  A: 

That's actually a builtin. (here's the help link)

:set autochdir

Stick that in your .vimrc or whatnot (:e $MYVIMRC). As mentioned here, sometimes plugins will have issues with that and you need to use something more complicated like

autocmd BufEnter * lcd %:p:h
jkerian
Thanks, I wasn't aware of this! However, I want to do it manually, when needed.
emostar
excellent bonus tip re lcd! I didn't know that existed, but will be very useful.
Peter