插入模式下的VIM Single Compile命令
我正在关注在Vim中映射键-教程(第1部分) -6.2插入模式图,并显示:
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()
,例如:
I'm trying to use this to call SingleCompile#Compile()
like:
map! <F5> <C-R>=SingleCompile#Compile()<CR>
它正在工作,但是问题是当我回到插入模式时,会插入0
字符作为副作用.
It's working, but the problem is that when I get back to insert mode, a 0
character is inserted as a side-effect.
为什么会这样,我该如何避免呢?
Why is this and how can I avoid it?
我正在使用<C-R>
,因为它不会创建撤消点,并且具有调用函数的目的,而不是像<C-O>
那样输入命令的目的.我不想创建一个撤消点.
I'm using <C-R>
because it doesn't create a undo point and has the purpose of calling a function instead of entering a command like <C-O>
does. I don't want to create a undo point.
我已经根据Ingo Karkat提供的三元运算符技巧更新了VIM Wiki.
I've updated the VIM wiki based on the ternary operator trick provided by Ingo Karkat.
函数的隐式返回值为0
.您需要修改SingleCompile#Compile()
或编写返回空字符串的包装器:
The implicit return value of a function is 0
. You need to either modify SingleCompile#Compile()
or write a wrapper that returns the empty string:
function! SingleCompileWrapper()
call SingleCompile#Compile()
return ''
endfunction
map! <F5> <C-R>=SingleCompileWrapper()<CR>
另一种巧妙的技巧是在?:
三元运算符内部评估函数:
An alternative clever trick is to evaluate the function inside the ?:
ternary operator:
map! <F5> <C-R>=SingleCompile#Compile()?'':''<CR>