75

There are many stackoverflow questions about running shell programs from within vim. Is it is possible to do the reverse, i.e.,

$ vim :BundleInstall

to allow me to run BundleInstall as part of a shell script, rather than having to open vim and run it manually?

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
jvc26
  • 6,363
  • 6
  • 46
  • 75

5 Answers5

108

Note, now the syntax has changed, and the line should read (As per @sheharyar):

vim +PluginInstall +qall

For posterity, previously, the correct line was:

vim +BundleInstall +qall

Should anyone other than me be looking! Note: this is in the Github README for vundle.

Community
  • 1
  • 1
jvc26
  • 6,363
  • 6
  • 46
  • 75
  • 4
    **If** you need to pass arguments: `echo '{"foo":"bar", "baz":{"baa":0,"bao":1}}' | vim "+setf json" -` – risto Dec 17 '14 at 01:16
  • 2
    To execute multiple commands, e.g. for vim-plug: `vim +PlugInstall +PlugClean! +qall` – thiagowfx Jun 07 '17 at 06:22
34

Per the vim man page (man vim):

+{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.

or:

--cmd {command}
    Like using "-c", but the command is  executed  just  before
    processing  any  vimrc file.  You can use up to 10 of these
    commands, independently from "-c" commands.

It really depends on what you want to do. Also, as described at the vundle readme file, if you launch vim like this:

    vim +BundleInstall +qall

This will install all bundle options without opening vim. And just for clarification, from the vim documentation:

:qall

    This stands for "quit all".  If any of the windows contain changes, Vim will
    not exit.  The cursor will automatically be positioned in a window with
    changes.  You can then either use ":write" to save the changes, or ":quit!" to
    throw them away.
Flimm
  • 136,138
  • 45
  • 251
  • 267
Daniel Noguchi
  • 593
  • 3
  • 7
4

How about something more complex?

vim "+set ff=unix" "+wq" node_modules/less-monitor/bin/less-monitor

Not sure whether that is totally correct, but it works for me. Thanks @jvc26

graceman9
  • 608
  • 2
  • 8
  • 20
  • I can't understand whether this is a new question (which should then be asked separately) or a new answer. – Fabio says Reinstate Monica Apr 12 '16 at 15:46
  • 1
    It's just an example how to use the solution with multi-word command like `+set ff=unix` and with `+wq` instead of `+qall`. In my case I need to update the file. I posted this as answer just because I can't post a comment (50+ reputation needed) – graceman9 Apr 12 '16 at 17:41
2

I'll add another answer for people who are looking for a more general solution.

vim +command works to run one Vim command but to run several Vim commands from a shell. Instead, start Vim in Ex-mode and supply commands with a Here document. This is an example from a script I wrote. It searches for a pattern in the file and inserts some text before it.

    ex --noplugin +1 "$v_file" <<-END 
            set maxmempattern=8000
            /^\s*\<endmodule\>/i

            FIXME   \`ifdef XXX_INCLUDE_API
              \`include "${api_file}"
            \`endif

            .
            w
            q
    END
Steve K
  • 2,124
  • 16
  • 19
0

Does

vim --cmd BundleInstall

do what you want?

Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51