Vim - how to run a command immediately when starting vim?
Solution 1:
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 .
Solution 2:
There is also the -c flag of vim. I do this in my tmuxp config to have vim start with a vertical split:
vim -c "vnew"
At least with neovim you can also open a file at the same time:
nvim -c "colorscheme mustang" some_file
Solution 3:
Create a file named ~/.vim/after/plugin/whatever_name_you_like.vim
and fill it with
FindFileCache .
The order in which scripts are read and executed in the vim directories is described in :help 'runtimepath'