How to pipe visually selected text to a UNIX command and append output to current buffer in Vim

ぃ、小莉子 提交于 2019-12-04 04:23:12

How about copying the selected text to the end of the file, select the copy and run the command? If you do not want to repeat the same commands over and over again, you can record the sequence by using q or add a new command. I have tried the latter as follows:

:com -range C <line1>,<line2>yank | $ | put | .,$ !rev

With it you can select some lines and then type :C. This will first yank the selection, then go to the end of the file, paste the yanked text and run the command (rev in this case) over the new text.

If you prefer more programmatic approach, you can have

:call append(line("$"), system("command", GetSelectedText()))

where GetSelectedText is the reusable function:

func! GetSelectedText()
  normal gv"xy
  let result = getreg("x")
  normal gv
  return result
endfunc
Brad Cox

Try

:r | YourCommand

For example:

:r ! echo foo

adds foo to your buffer.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!