Question

I want to execute a command automatically after cd'ing to a new directory from within vim. For example, I open gvim and run:

:cd ~/src/player

I would like vim at this point to automatically source a file that is in that directory.

Is this possible?

Was it helpful?

Solution

You can write an alias for that in your .vimrc:

command -nargs=1 Mycd call MyCd(<args>)
function MyCd(path)
  cd a:path
  e somefile.ext
endfunction

Then just type:

:Mycd /some/path/

OTHER TIPS

Not exactly what you're asking for, but

:au BufEnter,BufFilePost * lc <afile>:h

will make it so that whenever you open a new file (e.g. with :e ~/src/player/README), you will automatically change directories to ~/src/player. If you open multiple buffers, you will be changed to the directory containing the local buffer as you change between them, and if you open multiple tabs, they will remain in their respective directories.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top