Run vim command from commandline

2019-03-08 15:21发布

There are lots of SO questions on running shell programs from vim. What I'm wondering is if it is possible to do the reverse - i.e.

$ vim :BundleInstall

for example, to allow me to run BundleInstall as part of a script, rather than having to open Vim and run it manually on first running? Is this possible?

标签: bash shell vim zsh
5条回答
冷血范
2楼-- · 2019-03-08 15:34

I think that this is what you need:

You should check vim man page:

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

Hope it helps!

查看更多
不美不萌又怎样
3楼-- · 2019-03-08 15:40

Does

vim --cmd BundleInstall

do what you want?

查看更多
不美不萌又怎样
4楼-- · 2019-03-08 15:41

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
查看更多
三岁会撩人
5楼-- · 2019-03-08 15:49

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.

查看更多
6楼-- · 2019-03-08 15:50

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

查看更多
登录 后发表回答