Vim – 如何在启动vim时立即运行命令?

我有一个需要运行的插件(FindFile.vim) :FindFileCache . 每当我启动vim收集文件caching快速打开..我必须运行这个每次我启动vim。

每当vim启动时,我该如何编写一次运行一次的命令?

保持你的configuration的最好的地方是你的.vimrc文件。 但是,它来源太早,请检查:h startup

 At startup, Vim checks environment variables and files and sets values accordingly. Vim proceeds in this order: 1. Set the 'shell' and 'term' option *SHELL* *COMSPEC* *TERM* 2. Process the arguments 3. Execute Ex commands, from environment variables and/or files *vimrc* *exrc* 4. Load the plugin scripts. *load-plugins* 5. Set 'shellpipe' and 'shellredir' 6. Set 'updatecount' to zero, if "-n" command argument used 7. Set binary options 8. Perform GUI initializations 9. Read the viminfo file 10. Read the quickfix file 11. Open all windows 12. Execute startup commands 

正如你所看到的,你的.vimrc会在插件之前加载。 如果你把:FindFileCache . 在那里会发生错误,因为该命令不会退出(但是,一旦插件在步骤4中加载,将会存在)。

为了解决这个问题,不是直接执行命令,而是创build一个自动命令。 自动命令在事件发生时执行一些命令。 在这种情况下, VimEnter事件看起来是合适的(来自:h VimEnter ):

  *VimEnter* VimEnter After doing all the startup stuff, including loading .vimrc files, executing the "-c cmd" arguments, creating all windows and loading the buffers in them. 

然后,把这行放在你的.vimrc里

 autocmd VimEnter * FindFileCache . 

还有vim的-c标志。 我在我的tmuxpconfiguration中做了这个,让vim以垂直分割开始:

 vim -c "vnew" 

创build一个名为~/.vim/after/plugin/whatever_name_you_like.vim并填入

 FindFileCache . 

在vim目录中读取和执行脚本的顺序如下所示:help 'runtimepath'

FindFileCache放到.vimrc

自动加载命令是不同的,将不适用于您的scheme。