2011-07-25 116 views
68

我有一个插件(FindFile.vim),需要运行:FindFileCache .,每当我启动vim收集文件缓存以便快速打开时。我必须在每次启动vim时运行此操作。Vim - 如何在启动vim时立即运行命令?

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

回答

108

保留你的配置的最好的地方是你的.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中加载,它就会存在。)

要解决这个问题,不是直接执行命令,而是创建一个 自动命令。自动命令在事件发生时执行一些命令。在这种情况下,在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 . 
+1

+1,这应该是公认的答案,它比为一个命令创建单独的文件要干净得多。 – stackunderflow

+0

我用它在Ubuntu的全屏模式下打开vim:https://github.com/emmett9001/dotfiles/blob/115831ec75f19b60b30ddd521cd6e806467db18a/vimrc#L342 –

+0

想补充我在E172上有这个突破,因为我之后有一个空间。 – Caroline

13

创建一个名为~/.vim/after/plugin/whatever_name_you_like.vim文件,并

FindFileCache . 

在脚本中:help 'runtimepath'

1

.vimrcFindFileCache描述读取和执行在VIM目录顺序填充。

自动加载命令不同,不适用于您的方案。

+0

感谢指出了这一点,实际上它是'plugin'不'autoload'。 – Benoit

53

还有-c标志vim。我这样做,我tmuxp配置有VIM开始与垂直分割:

vim -c "vnew" 
+3

谢谢,这就是我一直在寻找的。有时,我只需要运行临时启动命令,就像你已经显示的那样,并且不需要将它存储在我的vimrc文件中。 – verboze

+0

这不能打开一个文件:'vim -c':colo default'test。 txt' –