views:

471

answers:

5

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?

+1  A: 

Maybe you should use something like

:echo system('echo '.shellescape(@").' | YourCommand')
ZyX
This seems good. One minor thing, is after selecting text you have to 'yank' with 'y', before running that command. Just selecting text in visual mode and running that command gives `E481: No range allowed`
davr
A: 

Use :!<shell cmd>

arcanex
Doesn't fully answer question on how to pass data to command
davr
+3  A: 

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)

matias
Great answer and it works. The only thing is it doesn't interpret new lines in vim on the pastebin. So if I have multiple selected lines in vim, it shows up as one big line on paste bin. Is there a way to fix that?
charding
And for me it produces this: http://pastebin.com/8XuqTr1K («\» at the end of each line). I'm using pastebin-0.6.1 from Gentoo repository and zsh.
ZyX
charding
Maybe you should use `"$paste\n$data"`?
ZyX
I fixed my problem with the newline: I changed the '\n' to '\r' in the Test() function above. Now it shows actual new lines on the pastebin.
charding
Ok, to add another question to this: Do you know how I can get the http link that is shown on the vim system command line by this script to be copied to the system buffer? (ie: "*y)
charding
A: 

Simply highlight the lines using visual line select shift-v, the hit :! and type the command you wish to send the commands to. The resulting output will then replace your selected text.

When you type your command it will appear at the bottom as:

:'<,'>!somecmd

the '<,'> is indicating that the range you have visually selected will be passed to the command specified after the !

Neg_EV
You may also provide arguments to the command as well
Neg_EV
This just replaces the selected text with the output from the shell cmd.
charding
Sorry, misread the question. I thought that was what you wanted to do.
Neg_EV
A: 

Another answer:

function Pastebin() range
    let savedreg=@"
    silent execute a:firstline.",".a:lastline."yank"
    python import vim, subprocess
    python p=subprocess.Popen(["pastebin"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    python p.stdin.write(vim.eval('@"'))
    let @"=savedreg
    python p.stdin.close()
    python retstatus=p.poll()
    python print p.stdout.read()
endfunction

Requires python support. Use it just like matias' function.

ZyX