如何将可视块发送到外部命令?
我使用 Ctrl-q 选择我的块,然后按下 !*program_name*
,但 Vim 发送的是整行而不是所选文本块。
我在 Windows 10 上使用 gVim。
如何将可视块发送到外部命令?
我使用 Ctrl-q 选择我的块,然后按下 !*program_name*
,但 Vim 发送的是整行而不是所选文本块。
我在 Windows 10 上使用 gVim。
Ex命令是基于行的,而块状可视模式是Vim的扩展。这就解释了功能不匹配的原因。
vis.vim插件提供了一个:B
命令,允许您将实际选择的块发送到一个Ex命令。它也可以与:!
一起使用,所以您可以做类似下面的事情:
:'<,'>B !tr 'a-z' 'A-Z'
:!sort ...
,但要指定字段(通过 --key=...
)。 - Ingo KarkatVim总是将整行发送给外部命令,但您可以使用romainl在此问题的答案中提供的功能来执行此操作:
Passing a non-linewise selection to an external program is done like this:
- backup the content of a register
- yank the selection in that register
- pass the content of that register to system() and output the result
- restore the register
Here it is, in a function:
function! VisualCountWords() range let n = @n silent! normal gv"ny echo "Word count:" . system("echo '" . @n . "' | wc -w") let @n = n " bonus: restores the visual selection normal! gv endfunction
that you can use in a mapping like this:
xnoremap <F6> :call VisualCountWords()<CR>