Vim过滤器只是视觉选择而不是整行

时间:2012-03-09 16:53:51

标签: vim

我想通过命令在Vim中过滤视觉选择。我知道的方式总是过滤视觉选择延伸的完整线:

行中选择a test
this is a test

并输入

:'<,'>!echo "the result"

将导致

the result

但我想:

this is the result

2 个答案:

答案 0 :(得分:3)

您可以使用\%V在可视区域内进行匹配:

:'<,'>s/\%V.*\%V/\=system('echo -n "the result"')

答案 1 :(得分:3)

考虑遵循!行的行为的以下映射 过滤命令(请参阅: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