从命令行运行vim命令

时间:2012-10-11 07:30:16

标签: bash shell vim zsh

从vim运行shell程序有很多SO问题。我想知道的是,是否可以反过来 - 即

$ vim :BundleInstall

例如,允许我作为脚本的一部分运行BundleInstall,而不是必须打开Vim并在首次运行时手动运行它?这可能吗?

5 个答案:

答案 0 :(得分:81)

注意,现在语法已经改变,行应该读取(按照@sheharyar):

vim +PluginInstall +qall

对于后代,以前,正确的行是:

vim +BundleInstall +qall

除了我以外的任何人都应该看!注意:这是vundle的Github README

答案 1 :(得分:23)

我认为这就是你所需要的:

您应该查看vim 手册页

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

或:

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

这实际上取决于你想做什么。另外,如vundle自述文件中所述,如果您按这样启动vim:

    vim +BundleInstall +qall

这将在不打开vim的情况下安装所有捆绑选项。 只是为了澄清,来自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.

希望它有所帮助!

答案 2 :(得分:2)

我会为正在寻找更通用解决方案的人添加另一个答案。

vim +command用于运行一个Vim命令,但是从shell运行几个Vim命令。相反,在Ex模式下启动Vim并使用Here文档提供命令。这是我写的脚本的一个例子。它在文件中搜索模式并在其前面插入一些文本。

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

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

            .
            w
            q
    END

答案 3 :(得分:2)

更复杂的事情怎么样?

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

不确定这是否完全正确,但它对我有用。 谢谢@ jvc26

答案 4 :(得分:0)

确实

vim --cmd BundleInstall

做你想做的事吗?