[英]Executing Vim commands in a shell script
I am writing a Bash script that runs a command-line program (Gromacs), saves the results, modifies the input files, and then loops through the process again.我正在编写一个运行命令行程序 (Gromacs) 的 Bash 脚本,保存结果,修改输入文件,然后再次循环执行该过程。
I am trying to use Vim to modify the input text files, but I have not been able to find a way to execute internal Vim commands like :1234, w, x, dd, etc. from the .sh file after opening my input files in Vim ("vim conf.gro").我正在尝试使用 Vim 来修改输入文本文件,但是在打开我的输入文件后,我一直无法找到一种方法来从 .sh 文件中执行内部 Vim 命令,例如:1234、w、x、dd 等在 Vim ("vim conf.gro") 中。
Is there a practical way to execute Vim commands from the shell script?有没有一种实用的方法可以从 shell 脚本执行 Vim 命令?
I think vim -w/W and vim -s
is what you are looking for.我认为
vim -w/W and vim -s
就是你要找的。
The "Vim operations/key sequence" you could also record with vim -w test.keys input.file
.您还可以使用
vim -w test.keys input.file
记录“Vim 操作/按键序列”。 You could write the test.keys
too.您也可以编写
test.keys
。 For example, save this in the file:例如,将其保存在文件中:
ggwxjddZZ
This will do:这将:
Move to the first line,
move to the next word,
delete one character,
move to the next line,
delete the line, and
save and quit.
With this test.keys
file, you could do:使用此
test.keys
文件,您可以执行以下操作:
vim -s test.keys myInput.file
Your "myInput.file" would be processed by the above operations, and saved.您的“myInput.file”将通过上述操作进行处理并保存。 You could have that line in your shell script.
您可以在 shell 脚本中包含该行。
VimGolf is using the same way to save the user's solution. VimGolf正在使用相同的方式来保存用户的解决方案。
You can script Vim via the -c
flag.您可以通过
-c
标志编写 Vim 脚本。
vim -c "set ff=dos" -c wq mine.mak
However that only gets you so far.然而,这只能让你走到这一步。
:normal
.:normal
。 eg :norm dd
:norm dd
commands.vim
) and then :source
via -S
.commands.vim
),然后:source
通过-S
。:h ex-cmd-index
:h ex-cmd-index
So you will end up with something like this.所以你最终会得到这样的结果。 With all your Vim commands inside of
commands.vim
.在 commands.vim 中使用所有 Vim
commands.vim
。
vim -S commands.vim mine.mak
You may also want to look into using sed
and/or awk
for text processing.您可能还想考虑使用
sed
和/或awk
进行文本处理。
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 功能,否则最好使用非交互式工具,例如
sed
、 awk
或 Perl/Python/Ruby/此处您最喜欢的脚本语言。
That said, you can use Vim non-interactively:也就是说,您可以非交互式地使用 Vim:
For very simple text processing (ie using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute
command), use Ex-mode .对于非常简单的文本处理(即使用 Vim 就像增强的 'sed' 或 'awk',也许只是受益于
:substitute
命令中增强的正则表达式),使用Ex-mode 。
REM Windows
call vim -N -u NONE -n -es -S "commands.ex" "filespec"
Note: silent batch mode ( :help -s-ex
) messes up the Windows console, so you may have to do a cls
to clean up after the Vim run.注意:静默批处理模式 (
:help -s-ex
) 会弄乱 Windows 控制台,因此您可能需要在 Vim 运行后执行cls
进行清理。
# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
Attention: Vim will hang waiting for input if the "commands.ex"
file doesn't exist;注意:如果
"commands.ex"
文件不存在,Vim 将挂起等待输入; better check beforehand for its existence!最好事先检查它的存在! Alternatively, Vim can read the commands from stdin.
或者,Vim 可以从标准输入读取命令。 You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the
-
argument.如果使用
-
参数,您还可以使用从 stdin 读取的文本填充新缓冲区,并从 stderr 读取命令。
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 的真正自动化(您可以与用户交互或让 Vim 运行以让用户接管),请使用:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.