Jen*_*ens 2 c vim ctags non-interactive
我使用vim非交互式地将单个C语言函数写入文件.首先tags使用ctags创建一个文件.要将main()函数写入文件func.c,然后使用
vim -R -U NONE -u NONE -c ":ta main" -c ":.,/^}/w!func.c" -c :q
Run Code Online (Sandbox Code Playgroud)
换句话说,它以非交互方式运行3个vim命令:
:ta main 跳转到main():.,/^}/w!func.c 从当前行写入行开头的下一个结束卷曲:q 退出我试图通过不浪费时间阅读用户或系统启动文件(-U NONE -u NONE)并避免.swp文件创建(-R)来提高效率.
有两个障碍我无法摆脱:
Vim: Warning: Output is not to a terminal警告,并且(显然)有一秒钟的延迟."func.c" 58 lines, 1707 characters written正在生成的消息.有没有办法避免这些?
这听起来像无声批处理模式(:help -s-ex)可以适用于您的用例.否则,您无法绕过完全自动化(具有您描述的一些缺点).
对于非常简单的文本处理(即使用Vim,如增强型'sed'或'awk',可能只是受益于:substitute命令中增强的正则表达式),请使用Ex模式.
REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"
Run Code Online (Sandbox Code Playgroud)
注意:静默批处理模式(:help -s-ex)会混淆Windows控制台,因此您可能必须cls在Vim运行后进行清理.
# Unix
vim -T dumb --noplugin -n -i NONE -es -S "commands.ex" "filespec"
Run Code Online (Sandbox Code Playgroud)
注意:如果"commands.ex"文件不存在,Vim将挂起等待输入; 更好地检查它的存在!或者,Vim可以从stdin读取命令.您还可以使用从stdin读取的文本填充新缓冲区,如果使用-参数,则从stderr读取命令.
对于涉及多个窗口的更高级处理,以及Vim的真实自动化(您可以与用户交互或让Vim运行以让用户接管),请使用:
vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
Run Code Online (Sandbox Code Playgroud)
以下是使用的参数的摘要:
-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.
Run Code Online (Sandbox Code Playgroud)