我正在使用此代码段(只需删除第一行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 功能,否则您最好使用非交互式工具,例如sed
,awk
或 Perl / Python / Ruby /你最喜欢的脚本语言。
你的例子,有sed
:
$ sed -i -e '1d' $1
也就是说,你可以以非交互方式使用 Vim:
对于非常简单的文本处理(即像增强的 'sed' 或 'awk' 一样使用 Vim,也许只是受益于:substitute
命令中增强的正则表达式),请使用Ex-mode。
REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"
注意:静默批处理模式 ( :help -s-ex
) 会弄乱 Windows 控制台,因此您可能需要cls
在 Vim 运行后进行清理。
# Unix
vim -T dumb --noplugin -n -i NONE -es -S "commands.ex" "filespec"
"commands.ex"
注意:如果文件不存在,Vim 会挂起等待输入;最好事先检查它的存在!或者,Vim 可以从标准输入读取命令。你的例子是这样的:
$ 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.