【发布时间】:2012-03-09 16:53:50
【问题描述】:
我想通过命令过滤 Vim 中的视觉选择。我知道的方式总是过滤视觉选择延伸的完整线条:
在该行中选择a test
this is a test
然后打字
:'<,'>!echo "the result"
将导致
the result
但我想要:
this is the result
【问题讨论】:
标签: vim
我想通过命令过滤 Vim 中的视觉选择。我知道的方式总是过滤视觉选择延伸的完整线条:
在该行中选择a test
this is a test
然后打字
:'<,'>!echo "the result"
将导致
the result
但我想要:
this is the result
【问题讨论】:
标签: vim
考虑以下符合! 行为的映射
过滤命令(参见: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
【讨论】:
:help mapleader);默认情况下,它是反斜杠字符。上面的映射应该与默认的!过滤使用相同的方式:在可视模式下选择一段文本,然后按您的前导键(同样,默认情况下为反斜杠),然后按!;或不选择任何内容,按下此组合键,然后使用运动命令(例如w、W、) 等)。
您可以使用\%V在可视区域内进行匹配:
:'<,'>s/\%V.*\%V/\=system('echo -n "the result"')
【讨论】: