bash 在 shell 脚本中执行 VIM 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18860020/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:36:24  来源:igfitidea点击:

Executing VIM commands in a shell script

macosbashshellvimvi

提问by Nikolay Rodionov

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. 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, ect. from the .sh file after opening my input files in VIM ("vim conf.gro").

我正在编写一个运行命令行程序 (Gromacs) 的 bash 脚本,保存结果,修改输入文件,然后再次循环执行该过程。我正在尝试使用 VIM 来修改输入文本文件,但我一直无法找到执行内部 VIM 命令的方法,例如:1234、w、x、dd 等。在 VIM 中打开我的输入文件后从 .sh 文件(“vim conf.gro”)。

Is there any practical way to execute VIM commands from the shell script?

有什么实用的方法可以从 shell 脚本执行 VIM 命令吗?

回答by Kent

I think vim -w/W and vim -sis what you are looking for.

我想vim -w/W and vim -s这就是你要找的。

The "vim operations/key sequence" you could record with vim -w test.keys input.filealso, you could write the test.keystoo. e.g. save this in the file:

您也可以记录的“vim 操作/按键序列” vim -w test.keys input.file,您也可以编写test.keys。例如将其保存在文件中:

ggwxjddZZ

this will do:

这将做:

move to 1st line, 
move to next word,
delete one char,
move to next line, 
del the line. 
save and quit.

with this test.keysfile, you could do:

使用此test.keys文件,您可以执行以下操作:

vim -s test.keys myInput.file

your "myInput.file" would be processed by the above operations, and saved. you could have that line in your shell script.

您的“myInput.file”将被上述操作处理并保存。您可以在 shell 脚本中包含该行。

vimgolf is using the same way to save users solution.

vimgolf 正在使用相同的方式来保存用户解决方案。

回答by Peter Rincker

You can script Vim via the -cflag.

您可以通过-c标志编写 Vim 脚本。

vim  -c "set ff=dos" -c wq mine.mak

However that only gets you so far.

然而,这只能让你走到这一步。

  • From the commands you gave it looks like you are trying to run some normal commands. You will need to use :normal. e.g. :norm dd
  • Writing all this in the command line is asking for trouble. I suggest you make a vim file (e.g. commands.vim) and then :sourcevia -S.
  • You probably want to get good and conformable vim's ex commands. Take a look at :h ex-cmd-index
  • 从您给出的命令来看,您似乎正在尝试运行一些正常的命令。您将需要使用:normal. 例如:norm dd
  • 在命令行中编写所有这些是自找麻烦。我建议你制作一个 vim 文件(例如commands.vim),然后:source通过-S.
  • 您可能想要获得良好且一致的 vim 的 ex 命令。看一眼:h ex-cmd-index

So you will end up with something like this. With all your vim commands inside of commands.vim.

所以你最终会得到这样的结果。将所有 vim 命令放在commands.vim.

vim -S commands.vim mine.mak

You may also want to look into using sedand/or awkfor text processing.

您可能还想考虑使用sed和/或awk进行文本处理。

回答by Ingo Karkat

Alternatives

备择方案

Unless you really need special Vim capabilities, you're probably better off using non-interactive toolslike sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

除非你真的需要特殊的Vim的功能,你可能关闭使用更好的非交互式工具一样sedawk或Perl / Python的/ Ruby的/你最喜欢的脚本语言在这里

That said, you can use Vim non-interactively:

也就是说,您可以非交互式地使用 Vim:

Silent Batch 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 :substitutecommand), 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 clsto clean up after the Vim run.

注意:静默批处理模式 ( :help -s-ex) 会弄乱 Windows 控制台,因此您可能需要cls在 Vim 运行后进行清理。

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

注意:如果"commands.ex"文件不存在,Vim 将挂起等待输入;最好事先检查它的存在!或者,Vim 可以从标准输入读取命令。如果使用-参数,您还可以使用从 stdin 读取的文本填充新缓冲区,并从 stderr 读取命令。

Full Automation

全自动化

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.