这听起来像是您的用例可以使用“静默批处理模式”(
:help -s-ex
)进行操作。否则,您无法避免全自动化(并且其中一些缺点正如您所描述的那样)。
静默批处理模式
针对非常简单的文本处理(即使用Vim像增强版的“sed”或“awk”,也许仅从一个
:substitute
命令中受益于增强的正则表达式),请使用
Ex模式。
REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"
注意:静默批处理模式(
:help -s-ex
)会破坏Windows控制台,因此您可能需要在Vim运行后执行
cls
来清理。
# Unix
vim -T dumb --noplugin -n -i NONE -es -S "commands.ex" "filespec"
注意:如果"commands.ex"
文件不存在,Vim将挂起等待输入;最好事先检查其是否存在!或者可以从stdin读取命令。您还可以使用-
参数从stderr读取命令,并用从stdin读取的文本填充新缓冲区。
完全自动化
对于涉及多个窗口的更高级处理以及Vim的实际自动化(您可能需要与用户交互或让Vim运行以让用户接管),请使用:
vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
以下是使用的参数摘要:
-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.
-i NONE Ignore the |viminfo| file (to avoid disturbing the
user's settings).
-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.
*
。此外,需要告诉它们文件,而 vim/ctags 已经内置了所有这些知识。 - Jens