我正在使用shell脚本和vim宏在Unix环境下进行数据处理。以下是步骤:
- 我使用grep从日志文件中获取一些有用的行。
- 使用vim内部的宏来操作数据。
- 再次使用shell脚本来处理数据。
我必须对许多文件执行此操作。所以,我想知道是否有一种通过特殊包装器在shell脚本中调用vim宏的方法。
$ vim -c "normal! @q" file
这是一种脆弱的方法。
除非您真正需要特殊的Vim功能,否则最好使用像sed
、awk
或Perl / Python / Ruby / 您喜欢的脚本语言这样的非交互式工具。
也就是说,您可以在非交互模式下使用Vim:
对于非常简单的文本处理(即使用Vim像加强版的'sed'或'awk',可能仅从:substitute
命令中受益于增强的正则表达式),请使用 Ex-mode 。
REM Windows
call vim -N -u NONE -n -es -S "commands.ex" "filespec"
-s-ex
会破坏Windows控制台,因此您可能需要在Vim运行后执行cls
以清除屏幕。# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
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 可以帮助你!