有没有办法在shell脚本中调用vim宏?

时间:2013-03-28 08:22:06

标签: shell vim

我正在使用shell脚本和vim宏在unix环境中进行数据处理。以下是步骤:

  1. 我使用grep从日志文件中获取一些有用的行。
  2. vim中的宏用于操作数据。
  3. 再次使用shell脚本处理数据。
  4. 我必须为许多文件执行此操作。所以,我想知道有没有办法通过特殊的包装器在shell脚本中调用vim的宏。

2 个答案:

答案 0 :(得分:7)

您可以触发录制的宏,例如

$ vim -c "normal! @q" file

这是一种脆弱的方法。

替代

除非你真的需要特殊的Vim功能,否则最好使用非交互式工具,如sedawk或Perl / Python / Ruby / < em>您最喜欢的脚本语言。

也就是说,你可以非交互式地使用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"

注意:如果“commands.ex”文件没有,Vim将等待输入 存在;更好地检查它的存在!或者,Vim可以阅读 来自stdin的命令。您还可以使用读取的文本填充新缓冲区 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.

答案 1 :(得分:0)

我不清楚你的要求。但也许vim -s,vim -W,vim -W可以帮到你!

相关问题