Vim: Select text in cmd shell and get output on vim info / command line

I want to pass the selected text to the shell command and get the single line output of this shell command in the vim info / command line file?

What I'm really trying to do is to push the selected text into a shell command of type pastebin, and I want to get the output of the cmd shell (which is the http link to pastebin). Is it possible?

+38
command-line vim shell pipe
Apr 04 '10 at 19:04
source share
6 answers

I would do it like this:

Put 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 this 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:

 :'<,'>Test 

Note. :<','> are range selectors, so that you can simply select the corresponding lines in visual mode, and then go into command mode (pressing the colon key)

+15
Apr 6 '10 at 14:40
source share

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

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

You can match it with a simple graphic shortcut as follows:

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

Press the host key + c in visual mode to send the selected text to the stdin command. The stdout command will be printed below the vim status bar.

Real-world example with CoffeeScript:

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

+74
Mar 21 2018-11-11T00:
source share

Just select the lines using the shift-v visual selection line, hit :! and enter the command for which you want to send commands. Then the result will replace the selected text.

When you enter the command, it will appear below:

 :'<,'>!somecmd 

'<,'> indicates that the range you selected will be passed to the command specified after:

+21
Apr 08 2018-10-14T00:
source share

Maybe you should use something like

 :echo system('echo '.shellescape(@").' | YourCommand') 

Starting with some version of vim-7.4, it is better to use

 :echo system('YourCommand', getreg('"', 1, 1)) 

. This is essentially the only way to keep NUL bytes intact if they are present in the file. Passing @" one way or another converts NUL bytes to NL (newline).

+4
Apr 04 '10 at 19:45
source share

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 the matias function.

+1
Apr 10 '10 at
source share

The @matias solution does not work well for me, because it seems that shellescape will add \ to each line.

So I use sed to accomplish this, and it works great!

 "dump selected lines function! DumpLines() range echo system('sed -n '.a:firstline.','.a:lastline.'p '.expand('%')) endfunction com! -range=% -nargs=0 Dump :<line1>,<line2>call DumpLines() 
+1
Aug 10
source share



All Articles