bash 从命令行运行 vim 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12834370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run vim command from command line
提问by jvc26
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 运行 shell 程序有很多 SO 问题。我想知道是否有可能做相反的事情 - 即
$ vim :BundleInstall
for example, to allow me to run BundleInstallas part of a script, rather than having to open Vim and run it manually on first running? Is this possible?
例如,允许我将BundleInstall作为脚本的一部分运行,而不必在第一次运行时打开 Vim 并手动运行它?这可能吗?
回答by jvc26
Note, now the syntax has changed, and the line should read (As per @sheharyar):
请注意,现在语法已更改,该行应为(根据@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 READMEfor vundle.
除了我之外还有其他人在看吗!注意:这在vundle的Github README中。
回答by Daniel Noguchi
I think that this is what you need:
我认为这就是你需要的:
You should check vim man page:
您应该检查 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.
-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.
--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 vundlereadme file, if you launch vim like this:
这真的取决于你想做什么。此外,如vundle自述文件中所述,如果您像这样启动 vim:
vim +BundleInstall +qall
vim +BundleInstall +qall
This will install all bundle options without opening vim. And just for clarification, from the vim documentation:
这将在不打开 vim 的情况下安装所有捆绑选项。只是为了澄清,来自vim 文档:
: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.
: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!
希望能帮助到你!
回答by Steve K
I'll add another answer for people who are looking for a more general solution.
我将为正在寻找更通用解决方案的人添加另一个答案。
vim +commandworks 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.
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
回答by graceman9
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
不确定这是否完全正确,但它对我有用。谢谢@jvc26
回答by Colin 't Hart
Does
做
vim --cmd BundleInstall
do what you want?
做你想做的事?

