How to execute Vim command from shell

Vim can be started just for the purpose of executing one or more Vim commands. This can be done using the -c commandline option to Vim. When one or more of these is provided, Vim is started, then executes these commands first and then renders the edit window.

For example, to open Vim with its internal help documentation:

$ vim -c help

Note how the internal help command is :help and we can drop the colon when using the command option.

To open a file named foo in Vim using this command option:

$ vim -c "e foo"

To open two files named foo and bar in Vim using this command option:

$ vim -c "e foo" -c "e bar"

The above examples are convoluted, since files can be opened directly without using this command option.

Here is a problem that I could solve only using this command option: I update the Vim text spellfile manually and I want to compile the binary spellfile from the shell or from a script. Easy to achieve by calling the command to compile spellfile mkspell and then quit q:

$ vim -c "mkspell! ~/.vim/spell/en.utf-8.add" -c q

Tried with: Vim 7.4.52 and Ubuntu 14.04


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.