vim command after cd

假装没事ソ 提交于 2019-12-14 02:13:47

问题


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?


回答1:


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/



回答2:


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.



来源:https://stackoverflow.com/questions/2112842/vim-command-after-cd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!