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

6

139

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?

Dorpat answered 25/7, 2011 at 19:6 Comment(0)
A
171

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 .
Airt answered 25/7, 2011 at 20:7 Comment(4)
+1, this should be the accepted answer, it is much cleaner than creating a separate file for one command.Phenocryst
Want to add I had this break on me with E172 because I had a space after .Huff
tried using this approach for calling :AnsiEsc however it did not really have the desired effect - any chance you know how to detect if a command such as :AnsiEsc has been run before?Bridgettebridgewater
This can solve many problems thank you so much for creating this post.Commissary
M
125

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
Mahon answered 7/1, 2015 at 0:39 Comment(7)
Thanks, this is what I was looking for. Sometimes, I just need to run a temporary startup command like split as you've shown, and do not need to store this in my vimrc fileBrentbrenton
This can't open a file: vim -c ':colo default' test.txtBourse
Perfect for me too - I'm using xolox/vim-notes and I want a fish function that opens Vim with a new note ready to go.Crum
I use it to start the vim help when I need it in a separate window: vim -c 'help | only' or short: vim -c'h|on' - this start vim with the help and afterward executes :only to make it a single windowMischa
Multiple commands can be chained by repeating the '-c' option as follows:vim -c "command1" -c "command2"Revolve
But what if im doing :e? I have a nnoremap that does :e +/string_im_looking_for/ -e "vnew" /somedir/file.sh<CR> but i just get 'string_looking_for' error.Resonant
This nnoremap shortcut takes me to another file. and places me right on a string. But i want to fix the monitor each time as well.Resonant
S
13

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'

Sfumato answered 25/7, 2011 at 19:8 Comment(0)
D
3

To get even later than other answers but still just after startup, use a timer in .vimrc. For example, this code in .vimrc waits half a second after startup before setting a variable.

function DelayedSetVariables(timer)
    let g:ycm_filetype_blacklist['ignored'] = 1
endfunction
let timer=timer_start(500,'DelayedSetVariables')

(The variable in the example is the blacklist from YouCompleteMe plugin. I assume, the plugin starts up some other process asynchronously which then creates variables, but is not quite ready by the time vim has started. The variable does not exist when I try to set it in .vimrc, an after file, or VimEnter event. This is specific to my Windows system somehow, YCM documentation says .vimrc should work for setting options.)

Deflate answered 15/5, 2018 at 12:39 Comment(0)
M
2

You can run vim file.txt "+:FindFileCache ."

Mannos answered 26/8, 2021 at 16:33 Comment(1)
Oh now I understand what +/ really meansBaseler
H
1

Put FindFileCache in your .vimrc.

Autload commands are different and will not work for your scenario.

Halfprice answered 25/7, 2011 at 19:27 Comment(1)
thanks for pointing that out, actually it was plugin not autoload.Sfumato

© 2022 - 2024 — McMap. All rights reserved.