gvim命令行中的正则表达式

时间:2017-09-30 08:18:01

标签: regex vim vi

我想在命令行中将一些gvim正则表达式应用于文件。我知道我们必须使用命令gvim -c '<regexp>' $filename。但我用视觉打开文件,用户可以看到,有没有办法在后台实现,我的意思是没有直观地打开文件?

问候 keerthan

2 个答案:

答案 0 :(得分:2)

替代

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

也就是说,您可以非交互式使用Vim(终端版本,而不是仅限GUI的GVIM):

无声批处理模式

对于非常简单的文本处理(即使用Vim就像增强的&#39;或者&#39; awk&#39;,可能只是受益于:substitute命令中增强的正则表达式) ,使用 Ex-mode

REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"

注意:静默批处理模式(:help -s-ex)会混淆Windows控制台,因此您可能需要在Vim运行后执行cls清理。

# Unix
vim -T dumb --noplugin -n -i NONE -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.
-i NONE           Ignore the |viminfo| file (to avoid disturbing the
                user's settings).
-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 :(得分:1)

要使用正则表达式修改文件,写入并立即退出,您可以使用类似vim -c <command> -c x <file>的内容。从技术角度来看,可以通过重定向标准流来抑制输出,并在后台运行 - 如果这是您想要的。