替代方案
除非你真的需要特殊的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可以从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.
-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.