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

tes*_*ter 91 vim

我有一个插件(FindFile.vim),:FindFileCache .每当我启动vim收集文件缓存以便快速打开时都需要运行..我必须在每次启动vim时运行它.

每次vim启动时,我怎么能写一个运行一次的命令?

sid*_*yll 131

保存配置内容的最佳位置是.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
Run Code Online (Sandbox Code Playgroud)

如您所见,您的.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.
Run Code Online (Sandbox Code Playgroud)

然后,只需将此行放在.vimrc中:

autocmd VimEnter * FindFileCache .
Run Code Online (Sandbox Code Playgroud)

  • +1,这应该是公认的答案,它比为一个命令创建单独的文件干净得多。 (2认同)

dja*_*aut 79

还有vim的-c标志.我在我的tmuxp配置中执行此操作以使vim以垂直拆分开始:

vim -c "vnew"
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这就是我想要的.有时,我只需要像你所示的那样运行一个像split这样的临时启动命令,而不需要将它存储在我的vimrc文件中 (6认同)

Ben*_*oit 13

创建一个名为的文件~/.vim/after/plugin/whatever_name_you_like.vim并填充它

FindFileCache .
Run Code Online (Sandbox Code Playgroud)

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