Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

Vim - how to run a command immediately when starting vim?

I have a plugin (FindFile.vim) that needs to run :FindFileCache . whenever I start vim to gather a file cache for quick opening.. I have to run this every time I start vim though.

How might I write a command that runs once, every time that vim starts up?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best place to keep your configuration stuff is in your .vimrc file. However, it's sourced too early, check :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

As you can see, your .vimrc will be loaded before plugins. If you put :FindFileCache . in it an error will occur, since that command does not exist yet. (It will exist once the plugin is loaded in step 4.)

To solve this, instead of executing the command directly, create an auto-command. Auto-commands execute some command when an event occurs. In this case, the VimEnter event looks appropriate (from :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.

Then, just place this line in your .vimrc:

autocmd VimEnter * FindFileCache .

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...