替代方案
除非您真的需要特殊的 Vim 功能,否则最好使用 非交互式工具,例如 sed、awk 或 Perl/Python/Ruby/您最喜欢的脚本语言在这里。
也就是说,你可以以非交互方式使用 Vim:
静默批处理模式
对于非常简单的文本处理(即像增强的 'sed' 或 'awk' 一样使用 Vim,也许只是受益于 :substitute 命令中增强的正则表达式),请使用 Ex-mode .
REM Windows
call vim -N -u NONE -n -es -S "commands.ex" "filespec"
注意:静默批处理模式 (:help -s-ex) 会弄乱 Windows 控制台,因此您可能需要在 Vim 运行后执行 cls 来清理。
# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
注意:如果"commands.ex" 文件不存在,Vim 将挂起等待输入;最好事先检查它的存在!或者,Vim 可以从标准输入读取命令。如果使用 - 参数,您还可以使用从标准输入读取的文本填充新缓冲区,并从标准错误读取命令。
全自动化
对于涉及多个窗口的更高级处理,以及 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.
-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.