我想通过命令在Vim中过滤视觉选择.我知道的方式总是过滤视觉选择扩展的完整线条:
a test在行中选择
this is a test
Run Code Online (Sandbox Code Playgroud)
和打字
:'<,'>!echo "the result"
Run Code Online (Sandbox Code Playgroud)
会导致
the result
Run Code Online (Sandbox Code Playgroud)
但我想要:
this is the result
Run Code Online (Sandbox Code Playgroud)
考虑以下遵循逐行过滤命令行为的映射!(请参阅:helpg \*!\*和: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
Run Code Online (Sandbox Code Playgroud)