Vim: Pipe selected text to shell cmd and receive output on vim info/command line

前端 未结 6 773
时光说笑
时光说笑 2020-12-04 07:57

I want to pipe the selected text to a shell command and receive the one-line output from this shell command on the vim info/command line?

What I\'m really trying to

6条回答
  •  失恋的感觉
    2020-12-04 08:15

    I would do it like this:

    Place this function in your vimrc:

    function Test() range
      echo system('echo '.shellescape(join(getline(a:firstline, a:lastline), "\n")).'| pbcopy')
    endfunction
    

    This will allow you to call this function by doing:

    :'<,'>call Test()
    

    Then you can also map that like this (just under the function declaration in your vimrc):

    com -range=% -nargs=0 Test :,call Test()
    

    So you can call the function doing this:

    :'<,'>Test
    

    Note: :<','> are range selectors, in order to produce them just select the pertinent lines in visual mode and then go to command mode (pressing the colon key)

提交回复
热议问题