Vim Tips Wiki
Advertisement

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 64 Printable Monobook Previous Next

created March 28, 2001 · complexity intermediate · author William Lee · version 7.0


Sometimes it's helpful if your working directory is always the same as the buffer you are editing. To achieve this, you can put the following in your vimrc:

set autochdir

That's it! Unfortunately, though, some plugins will have problems when this option is set if they make assumptions about the current directory and do not check for this option. Although many of these problems will occur no matter which method you use to change directories (see caveats below) you may be able to fix some issues by using the following instead of 'autochdir':

autocmd BufEnter * lcd %:p:h

This works better in some cases because the autocmd is not nested, and will therefore not fire when switching buffers via another autocmd. It will also work in older versions of Vim or versions compiled without the 'autochdir' option. Note, however, that there is no easy way to test for this autocmd in a script like there is for the 'autochdir' option.

Either of these methods will "cd" to the directory of the file in the current buffer, each time you switch to the buffer. The autocmd method is similar to VimTip2 but more automatic.

Using the autocmd method, you could customize when the directory change takes place. For example, to not change the directory if one is editing in /tmp:

autocmd BufEnter * if expand("%:p:h") !~ '^/tmp' | lcd %:p:h | endif

Caveats

  • Either of these automatic methods will make loading and saving sessions work incorrectly.
  • Problems with 'autochdir' and netrw have been reported in the past, though they are fixed now.

Alternatives

Mapping or command for quick directory change

Rather than automatically changing the working directory, you could set Vim up so that you can easily change directory to the file being edited. The mapping below maps the keystrokes ,cd (comma then cd) to do just that.

map ,cd :cd %:p:h<CR>

You can use

map ,cd :cd %:p:h<CR>:pwd<CR>

to print the directory after changing, so you know where you ended up.

If you prefer, you could use a command instead of a mapping. The following allows you to use :CDC:

" CDC = Change Directory to Current buffer's directory
command CDC cd %:p:h

That command also prints the new current directory after changing.

Add file directory to Vim path

If your main purpose in setting the working directory is to easily open files in a project, another approach is to add the directory to the path, so that you can use the :find command to open any other file in the directory:

" always add the current file's directory to the path if not already there
autocmd BufRead *
      \ let s:tempPath=escape(escape(expand("%:p:h"), ' '), '\ ') |
      \ exec "set path-=".s:tempPath |
      \ exec "set path+=".s:tempPath

This could be modified using :set path^= instead of :set path+=, which would prepend the new directory to the beginning of the path instead of appending it to the end. If you do so, you should probably remove and then prepend the default value of 'path' as well, so that the default value remains at the beginning.

References

Comments

This was added by an anonymous user, to the Caveats section above:

  • Files outside of the local directory cannot be opened from the command line (at least in gVim 7.2 on Windows). e.g. 'gvim dirA/dirB/file.txt' will change into 'dirA/dirB' and then try to open 'dirA/dirB/file.txt'. Unsurprisingly, dirA/dirB/dirA/dirB/file.txt does not exist.

This statement is simply false. I use gvim 7.2 on Windows, and the example given works fine for me. Additionally, both autochdir and the autocmd method given above, work by changing the current directory after loading the file. The directory change will have no impact on whether the file loads using either of these two methods.

Anonymous user, I suggest posting to the vim_use mailing list with your problem. Perhaps they can figure out your true root cause. This tip should not be the cause, unless combined with something else in your setup.

--Fritzophrenic 02:46, June 20, 2010 (UTC)

Advertisement