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

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

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

See the official Vim docs at :help :w_c.


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

Leave a Comment