I would like to filter a visual selection in Vim through a command. The way I know filters always the complete lines over which the visual selection extends:
Selecting a test
in the line
this is a test
and typing
:'<,'>!echo "the result"
will result in
the result
But I want:
this is the result
Consider the following mappings that adhere the behavior of the !
linewise
filtering commands (see :helpg \*!\*
and :help v_!
).
nnoremap <silent> <leader>! :set opfunc=ProgramFilter<cr>g@
vnoremap <silent> <leader>! :<c-u>call ProgramFilter(visualmode(), 1)<cr>
function! ProgramFilter(vt, ...)
let [qr, qt] = [getreg('"'), getregtype('"')]
let [oai, ocin, osi, oinde] = [&ai, &cin, &si, &inde]
setl noai nocin nosi inde=
let [sm, em] = ['[<'[a:0], ']>'[a:0]]
exe 'norm!`' . sm . a:vt . '`' . em . 'x'
call inputsave()
let cmd = input('!')
call inputrestore()
let out = system(cmd, @")
let out = substitute(out, '\n$', '', '')
exe "norm!i\<c-r>=out\r"
let [&ai, &cin, &si, &inde] = [oai, ocin, osi, oinde]
call setreg('"', qr, qt)
endfunction
You can use \%V
to match inside the Visual area:
:'<,'>s/\%V.*\%V/\=system('echo -n "the result"')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With