Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 948
tester Avatar asked Jul 25 '11 19:07

tester


People also ask

How do I run a command in Vim?

You can run commands in Vim by entering the command mode with : . Then you can execute external shell commands by pre-pending an exclamation mark ( ! ). For example, type :! ls , and Vim will run the shell's ls command from within Vim.

How do you chain command in Vim?

You can execute more than one command by placing a | between two commands.

What is Vim command line mode?

vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.

How do I run a .Vim file in Linux?

If you're on a Linux system right now, open up a terminal and type vim filename. Enter insert mode and type a bit (or copy some of the text from this article into Vim) and then hit Escape to start practicing movement around the file.


2 Answers

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 . 
like image 116
sidyll Avatar answered Sep 21 '22 22:09

sidyll


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 
like image 21
djangonaut Avatar answered Sep 22 '22 22:09

djangonaut