如何通过shell脚本执行vim命令 [英] How to execute the vim commands through shell script
问题描述
我的情况是
我正在编写一个shell脚本,必须将输入传递给 vim .
I have a writing a shell script where I have to pass input to the vim.
详细解释
在这里,我已经编写了无法更改的shell脚本.这是它的代码.
Here I have already written shell script that I cannot changed. Here is the code for it.
sudo vim /mnt/etc/{hosts,hostname,ports}
由于此主机文件已打开,我们可以通过:n 手动转到主机名文件旁边,对于端口文件也是如此.
due to this hosts file is opened we can go manually next to the hostname file by :n and similar for the ports file.
但是我必须在 .sh 文件中执行相同的操作. 另外,我还必须编辑端口文件,完成后,我必须通过:wq 命令保存并完全保存它.
But I have to perform same operation from my .sh file. Also I have to edit the ports file and after completing it I have to save and quite it through :wq command.
我该怎么办?
推荐答案
替代项
除非真的需要特殊的Vim功能,否则最好使用非交互式工具,例如sed
,awk
或Perl/Python/Ruby/脚本语言.
Alternatives
Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed
, awk
, or Perl / Python / Ruby / your favorite scripting language here.
也就是说,您可以非交互式地使用Vim:
That said, you can use Vim non-interactively:
对于非常简单的文本处理(即像Vim一样使用增强的"sed"或"awk",也许只是受益于:substitute
命令中增强的正则表达式),请使用 Ex-mode
For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute
command), use Ex-mode.
# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
注意:如果"commands.ex"
文件不存在,Vim将挂起等待输入;否则,Vim将挂起.最好事先检查它的存在!另外,Vim可以从stdin中读取命令.您还可以使用从stdin读取的文本填充新的缓冲区,如果使用-
参数,则可以从stderr读取命令.
Attention: Vim will hang waiting for input if the "commands.ex"
file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the -
argument.
要获得涉及多个窗口的更高级处理以及Vim的真正自动化(您可以在其中与用户互动或让Vim运行以让用户接管),请使用:
For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:
vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
以下是所用参数的摘要:
Here's a summary of the used arguments:
-T dumb Avoids errors in case the terminal detection goes wrong.
-N -u NONE Do not load vimrc and plugins, alternatively:
--noplugin Do not load plugins.
-n No swapfile.
-es Ex mode + silent batch mode -s-ex
Attention: Must be given in that order!
-S ... Source script.
-c 'set nomore' Suppress the more-prompt when the screen is filled
with messages or output to avoid blocking.
这篇关于如何通过shell脚本执行vim命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!