To do this, you can use the following Vim-script function.
function! FeedVisualCmd(cmdpat)
let [qr, qt] = [getreg('"'), getregtype('"')]
silent norm! gvy
let cmd = printf(a:cmdpat, shellescape(@"))
call setreg('"', qr, qt)
echo system(cmd)
if v:shell_error
echohl ErrorMsg | echom 'Failed to run ' . cmd | echohl NONE
endif
endfunction
It copies the selected text to an unnamed register (see :help ""), launches the specified command template through a function printf, and then executes the resulting command, echoes its output.
If the only part of the command that changes is a template, it’s convenient to define a mapping,
vnoremap <leader>g :<c-u>call FeedVisualCmd('"C:\Programme\WinGrep\grep32.exe" %s *.sql D:\MyProject\build')<cr>
source
share