简体   繁体   English

如何写一个批处理文件来运行VIM命令

[英]How to write a batch file to run vim commands

I'm trying to write a batch file that will replace a couple of lines in a text file, using vim. 我正在尝试编写一个批处理文件,它将使用vim替换文本文件中的几行。 Below is what I have for the batch file: 以下是批处理文件的内容:

vim toedit.txt  
:10  
:R new value  
:11  
:R new value  
:wq

Executed, it will give a message from vim's point of view: 执行后,它将从vim的角度给出一条消息:

Swap file "toedit.txt.swp" already exists!  
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

What is swap file? 什么是交换文件? Anyway, I press D , since it sounds like just a temporary file, then my batch run will stop in vim with the cursor on line 10 in the text file. 无论如何,我按D键,因为它听起来像是一个临时文件,所以我的批处理运行将在vim中停止,光标位于文本文件的第10行。

Does newline (which is produced by pressing enter) in the batch file function as the user pressing enter to run each command? 批处理文件中的换行符(通过按Enter生成)是否与用户按Enter来运行每个命令的功能相同? How do I write up a correct batch file to edit the text file? 如何编写正确的批处理文件来编辑文本文件? Thanks. 谢谢。

:help swap-file

Typically, swap file will be on the disk if Vim crashes, or opens a file that is already open by Vim in another process. 通常,如果Vim崩溃,则交换文件将位于磁盘上,或打开Vim在另一个进程中已经打开的文件。 You can ignore the swap file using the -n switch. 您可以使用-n开关忽略交换文件。

For batch editing, it will be better to use ex , which is the batch version of Vim. 对于批处理编辑,最好使用ex ,这是Vim的批处理版本。 However, there is no :R command in vanilla Vim, so I can't be sure what you are trying to do; 但是,在香草Vim中没有:R命令,因此我不确定您要做什么。 but it would be something like 但这就像

ex -n toedit.txt +10 +"R new value" +11 +"R new value" +wq

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM