简体   繁体   English

vim键序列作为函数参数

[英]vim key sequence as function argument

I want to execute the command "yiw:s/\\<<Cr>"\\>/<Cr>"/g<Left><Left>" by key sequence. 我想"yiw:s/\\<<Cr>"\\>/<Cr>"/g<Left><Left>"执行命令"yiw:s/\\<<Cr>"\\>/<Cr>"/g<Left><Left>" So I make a mapping 所以我做了一个映射

    nnoremap <F7> yiw:s/\<<C-r>"\>/<C-r>"/g<Left><Left>

This mapping copy the word under cursor, then the string :s/\\<">/"/g" (where " are substituted by the copied word) appears in the command line and the cursor in the command line is at the end of replacement statement. 此映射复制光标下的单词,然后在命令行中出现字符串:s / \\ <“> /” / g“(其中“被复制的单词替换”),命令行中的光标位于替换声明。

I also want to save cursor position before this command and restore after. 我还想在此命令之前保存光标位置,然后在此之后还原。

  function! SafeCommand(cmd)
let line = line('.')
let col = col('.')
// execute cmd here 
call cursor( line, col )
  endfunction

How to do that? 怎么做?

Normally, you'd just put the entire (complex) command in a function, and invoke that function from the :nnoremap . 通常,您只需将整个(复杂)命令放入函数中,然后从:nnoremap调用该函数。 But that doesn't work for incomplete commands, like the template :substitute that your mapping represents. 但这不适用于不完整的命令,例如映射所代表的template :substitute For that, you need to include the save / restore parts into the command-line (though that's ugly): 为此,您需要在命令行中包含保存/恢复部分(尽管很丑陋):

: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM