Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 892
ikwyl6 Avatar asked Apr 04 '10 19:04

ikwyl6


2 Answers

For multi line version you can do this after selecting the text:

:'<,'>:w !command<CR> 

You can map it to simple visual mode shortcut like this:

xnoremap <leader>c <esc>:'<,'>:w !command<CR> 

Hit leader key + c in visual mode to send the selected text to a stdin of the command. stdout of the command will be printed below vim's statusbar.

Real world example with CoffeeScript:

https://github.com/epeli/vimconfig/commit/4047839c4e1c294ec7e15682f68563a0dbf0ee6d

like image 183
Epeli Avatar answered Sep 17 '22 15:09

Epeli


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 !

like image 41
Neg_EV Avatar answered Sep 18 '22 15:09

Neg_EV