oli*_*ver 8 vim shell scripting
对我来说,VIM的一个常用功能是通过外部命令过滤文件(或文本选择),并将选择替换为结果,例如:
:'<,>'!sort
Run Code Online (Sandbox Code Playgroud)
所以
c
b
a
Run Code Online (Sandbox Code Playgroud)
将被排序并将导致
a
b
c
Run Code Online (Sandbox Code Playgroud)
也可以用外部命令的返回值替换当前行,例如:
:,!ls | wc -l
Run Code Online (Sandbox Code Playgroud)
将插入当前目录中的文件数量(在我的情况下例如:)
41
Run Code Online (Sandbox Code Playgroud)
但有没有办法将字符串传递给shell的命令?例如,这可能是我的视觉选择的内容:
line_x
line_y
line_z
Run Code Online (Sandbox Code Playgroud)
我需要的是执行一些shell命令并将每个选定的行作为一个shell脚本参数,例如:
my_bash_command line_x -c -e -f
my_bash_command line_y -c -e -f
my_bash_command line_z -c -e -f
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?
Pet*_*ker 11
我建议你用 xargs -L1
例:
:%!xargs -L1 wc -l
Run Code Online (Sandbox Code Playgroud)
基本上xargs [cmd]
将[cmd]
使用多行中的多个参数运行以下内容.但是使用-L1
参数[cmd]
将为每一行执行命令.
如果需要为命令的参数提供某个顺序,可以使用xargs
's -I
选项提供要由参数列表替换的模式.
例:
:%!xargs -L1 -I {} rake {} --trace
Run Code Online (Sandbox Code Playgroud)
如果你觉得非常冒险,你可以像这样直接执行代码:
:%!bash
Run Code Online (Sandbox Code Playgroud)