Search code examples
vimshellcommand-linepipe

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


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 do: Pipe the selected text to a pastebin-type shell command and I want to receive the output of the shell cmd (which is the http link to the pastebin). Is this possible?


Solution

  • 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 :<line1>,<line2>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)