这里的文档用类似vim的ed从文件中删除第一行



我正在使用这段代码片段(只需删除带有ed的第一行)。我想知道我是否可以在vim中制作这样的东西。我编写了脚本并将文件作为参数传递。

文件:

# This is a comment #
foo bar

ed:编辑

ed $1 << EOF
1/^[ ]*$/d
w 
q
EOF

我试过vim:

vi $1 << EOF
dd
w 
q
EOF
> Vim: Warning: Input is not from a terminal

您可以在"ex"模式下启动vim并向其提供命令:

vim -E -s yourfile <<EOF
:1d
:update
:quit
EOF

但在这种情况下使用sed会更合适:

sed '1d' yourfile

备选方案

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

您的示例,使用sed:

$ sed -i -e '1d' $1

也就是说,你可以非交互式地使用Vim:

静默批处理模式

对于非常简单的文本处理(即,像使用增强的"sed"或"awk"一样使用Vim,可能只是受益于:substitute命令中的增强正则表达式),请使用Ex模式

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读取命令。你的例子是这样的:

$ vim -e -s $1 << EOF
1delete
wq!
EOF

全自动化

对于涉及多个窗口的更高级处理,以及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.

最新更新