How to run Vim command from the shell?

11,937

Solution 1

From the vim(1) man page:

+{command}

-c {command}

{command} will be executed after the first file has been read. {command} is interpreted as an Ex command. If the {command} contains spaces it must be enclosed in double quotes (this depends on the shell that is used). Example: Vim "+set si" main.c

Note: You can use up to 10 "+" or "-c" commands.

Solution 2

You can execute your command like this:

vim -E -c BundleInstall -c q

which will avoid opening a Vim window in your terminal.

Note: My first answer included the -s option which I had needed for another application but was incorrect here because it prevented much of Vim's intialization including sourcing the plugin that defined the BundleInstall command.

Solution 3

While the vim specific recipe above is the right way to do it, you can always use a more general approach like autoexpect.

Share:
11,937

Related videos on Youtube

DavidHolmes
Author by

DavidHolmes

Updated on September 18, 2022

Comments

  • DavidHolmes
    DavidHolmes over 1 year

    This might be a dumb question, but bear with me.

    I'm automating some of the usual stuff I do when setting up a new work environment, and would like to automate the Vim command :BundleInstall (for installing all my Vim plugins).

    Is it possible to run this from the shell?

    Alternatively, is it possible to have the script run Vim, execute :BundleInstall, wait until it finishes and quit?

    Thanks.

  • DavidHolmes
    DavidHolmes over 11 years
    Thanks! I knew I'd feel dumb... :-/ Serves me right for not reading the manpage thoroughly. I ended up using vim +BundleInstall +qall!.
  • DavidHolmes
    DavidHolmes over 11 years
    Thanks, but for some reason it won't work for me.
  • garyjohn
    garyjohn over 11 years
    Now I see why. I forgot that -s does more than inhibit certain messages--it also inhibits intializations--so the definition of BundleInstall wasn't being sourced. One way to fix that would be to add an option like this before the first -c: --cmd 'runtime plugin/bundle.vim'. Edit that file name to suit. See :help -s-ex.