简体   繁体   English

在 shell 脚本中执行 Vim 命令

[英]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.然而,这只能让你走到这一步。

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

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 功能,否则最好使用非交互式工具,例如sedawk或 Perl/Python/Ruby/此处您最喜欢的脚本语言

That said, you can use Vim non-interactively:也就是说,您可以非交互式地使用 Vim:

Silent Batch Mode静默批处理模式

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 读取命令。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MacOS上的Shell脚本(osascript)中执行sudo rm命令时出错 - Error when executing sudo rm commands in shell script (osascript) on MacOS MacPorts安装-Shell命令/ Postflight脚本 - MacPorts Installation — Shell Commands/Postflight Script 用于执行sudo命令的交互式shell脚本 - interactive shell script for execution of sudo commands 使用NSTask执行Shell命令-Objective-C可可 - Executing shell commands with NSTask - Objective-C Cocoa 无法在沙盒可可应用程序中执行shell脚本命令 - Can't execute shell script commands in sandboxed cocoa app 如何从Shell脚本执行终端命令? (MacOS 10.12) - How to execute Terminal commands from a shell script? (MacOS 10.12) 如何使用shell脚本在文件对上运行多个命令? - How to run multiple commands on file pairs using a shell script? Cron Job未执行具有要以其他用户身份执行的命令的Shell脚本 - Cron Job not executing the Shell Script that has Command to execute as another user 在jenkins中的执行Shell脚本中编写什么内容以执行自动化脚本 - What to write in Execute shell script in jenkins for executing automation scripts Mac OS 上的 Bash shell 脚本 - 某些命令只回显到 shell,不执行 - Bash shell script on Mac OS - Certain commands only echoed to shell, not executed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM