[英]vim key sequence as function argument
我想"yiw:s/\\<<Cr>"\\>/<Cr>"/g<Left><Left>"
執行命令"yiw:s/\\<<Cr>"\\>/<Cr>"/g<Left><Left>"
。 所以我做了一個映射
nnoremap <F7> yiw:s/\<<C-r>"\>/<C-r>"/g<Left><Left>
此映射復制光標下的單詞,然后在命令行中出現字符串:s / \\ <“> /” / g“(其中“被復制的單詞替換”),命令行中的光標位於替換聲明。
我還想在此命令之前保存光標位置,然后在此之后還原。
function! SafeCommand(cmd)
let line = line('.')
let col = col('.')
// execute cmd here
call cursor( line, col )
endfunction
怎么做?
通常,您只需將整個(復雜)命令放入函數中,然后從:nnoremap
調用該函數。 但這不適用於不完整的命令,例如映射所代表的template :substitute
。 為此,您需要在命令行中包含保存/恢復部分(盡管很丑陋):
:fun! Save()
let s:line = line('.')
let s:col = col('.')
:endfun
:fun! Restore()
call cursor( s:line, s:col )
:endfun
:nnoremap <F7> yiw:call Save()<Bar>s/\<<C-r>"\>/<C-r>"/g<Bar>call Restore()<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.