【发布时间】:2011-07-25 19:06:40
【问题描述】:
我有一个插件 (FindFile.vim),每当我启动 vim 时都需要运行 :FindFileCache . 以收集文件缓存以便快速打开。但我每次启动 vim 时都必须运行它。
我如何编写一个在每次 vim 启动时运行一次的命令?
【问题讨论】:
标签: vim
我有一个插件 (FindFile.vim),每当我启动 vim 时都需要运行 :FindFileCache . 以收集文件缓存以便快速打开。但我每次启动 vim 时都必须运行它。
我如何编写一个在每次 vim 启动时运行一次的命令?
【问题讨论】:
标签: vim
保存配置文件的最佳位置是 .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 .
【讨论】:
还有 vim 的 -c 标志。我在我的 tmuxp 配置中这样做是为了让 vim 从垂直分割开始:
vim -c "vnew"
至少用neovim你也可以同时打开一个文件:
nvim -c "colorscheme mustang" some_file
【讨论】:
vim -c ':colo default' test.txt
xolox/vim-notes 并且我想要一个可以打开 Vim 并准备好新注释的 fish 函数。
vim -c 'help | only' 或简称:vim -c'h|on' - 这会使用帮助启动 vim,然后执行 :only 来完成它一个窗口
vim -c "command1" -c "command2"
创建一个名为~/.vim/after/plugin/whatever_name_you_like.vim 的文件并将其填充
FindFileCache .
在vim目录中读取和执行脚本的顺序在:help 'runtimepath'中有描述
【讨论】:
要获得比其他答案更晚但仍处于启动后的时间,请在 .vimrc 中使用计时器。例如,.vimrc 中的这段代码在启动后等待半秒后才设置变量。
function DelayedSetVariables(timer)
let g:ycm_filetype_blacklist['ignored'] = 1
endfunction
let timer=timer_start(500,'DelayedSetVariables')
(示例中的变量是来自 YouCompleteMe 插件的黑名单。我假设,插件异步启动了一些其他进程,然后创建变量,但在 vim 启动时还没有准备好。当变量不存在时我尝试在 .vimrc、一个后文件或 VimEnter 事件中设置它。这是特定于我的 Windows 系统的,YCM 文档说 .vimrc 应该适用于设置选项。)
【讨论】:
将FindFileCache 放入您的.vimrc。
自动加载命令不同,不适用于您的场景。
【讨论】:
plugin 而不是autoload。
你可以运行vim file.txt "+:FindFileCache ."
【讨论】:
+/ 的真正含义了