我正在关注Mapping keys in Vim - Tutorial (Part 1) - 6.2 Insert mode maps,并在那里说:
The <C-R>= command doesn't create a new undo point.
You can also call Vim functions using the <C-R>= command:
:inoremap <F2> <C-R>=MyVimFunc()<CR>
我正在尝试使用它来调用SingleCompile#Compile()
,如:
map! <F5> <C-R>=SingleCompile#Compile()<CR>
它有效,但问题是当我回到插入模式时,会插入一个0
字符作为副作用。
为什么会这样,我该如何避免呢?
修改
我正在使用<C-R>
因为它没有创建撤销点,并且目的是调用函数而不是像<C-O>
那样输入命令。我不想创建一个撤销点。
修改
我根据Ingo Karkat提供的三元操作技巧更新了VIM wiki。
答案 0 :(得分:2)
函数的隐式返回值为0
。您需要修改SingleCompile#Compile()
或编写一个返回空字符串的包装器:
function! SingleCompileWrapper()
call SingleCompile#Compile()
return ''
endfunction
map! <F5> <C-R>=SingleCompileWrapper()<CR>
另一个聪明的技巧是评估?:
三元运算符中的函数:
map! <F5> <C-R>=SingleCompile#Compile()?'':''<CR>
答案 1 :(得分:1)
'0'是函数的返回值,在插入模式下调用时自然插入到缓冲区中
使用&lt; C-O&gt;而不是&lt; C-R&gt;离开命令的插入模式
答案 2 :(得分:0)
我不建议采用这种方法,但是对于乡下人的解决方案如何(只是在事后删除0
):
map! <F5> <C-R>=SingleCompile#Compile()<CR><BS>
说真的,对于那些<C-R>
无法使用的情况,您必须退出插入模式,:undojoin
可能会有所帮助。