我正在使用 shell 脚本和 vim 宏在 unix 环境中进行数据处理。以下是步骤:
- 我使用 grep 从日志文件中获取一些有用的行。
- vim 中的宏用于操作数据。
- 同样,使用 shell 脚本来处理数据。
我必须对许多文件执行此操作。所以,我想知道有没有办法通过特殊的包装器在 shell 脚本中调用 vim 的宏。
您可以触发录制的宏,例如
$ vim -c "normal! @q" file
这是一种脆弱的做法。
除非您真的需要特殊的 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"
注意:静默批处理模式-s-ex
会弄乱 Windows 控制台,因此您可能需要cls
在 Vim 运行后进行清理。
# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
注意:如果“commands.ex”文件不存在,Vim 会挂起等待输入;最好事先检查它的存在!或者,Vim 可以从标准输入读取命令。如果使用 - 参数,您还可以使用从 stdin 读取的文本填充新缓冲区,并从 stderr 读取命令。
对于涉及多个窗口的更高级处理,以及 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.
我不清楚你的要求。但也许 vim -s, vim -W, vim -W 可以帮助你!