目前我正在使用两个不同的键来设置colorscheme
map <F8> :colors wombat256 <cr>
map <F9> :colors dimtag <cr>
Run Code Online (Sandbox Code Playgroud)
我想实现像这样的切换行为
function! ToggleDimTags()
if (g:colors_name == "wombat256")
colors dimtag
else
colors wombat256
endif
endfunction
Run Code Online (Sandbox Code Playgroud)
我的问题是ToogleDimTags()在每次调用时将光标位置重置为第一行,这是不合需要的.任何建议赞赏.
正如评论中所讨论的,问题是您的地图调用:execute
行为略有不同,您可能想要的是:call:
nnoremap <F5> :call ToggleDimTags()
Run Code Online (Sandbox Code Playgroud)
为了澄清@ZyX的说法,:h :exec包含以下文字:
:exe :execute
:exe[cute] {expr1} .. Executes the string that results from the evaluation
of {expr1} as an Ex command.
[...]
Run Code Online (Sandbox Code Playgroud)
那么:execute真正做的是评估表达式,寻找将作为Ex命令执行的字符串(也称为冒号命令).换一种说法:
exec ToggleDimTags() | " <-- ToggleDimTags() is evaluated and returns 0
exec 0
Run Code Online (Sandbox Code Playgroud)
这是:
:0
Run Code Online (Sandbox Code Playgroud)
现在,:h :call:
:cal :call E107 E117
:[range]cal[l] {name}([arguments])
Call a function. The name of the function and its arguments
are as specified with |:function|. Up to 20 arguments can be
used. **The returned value is discarded**.
[...]
Run Code Online (Sandbox Code Playgroud)
我一直在考虑你的功能,使用三元运算符和一些:execute魔法,你可以将它简化到你丢弃额外功能的程度:
nnoremap <silent> <F9> :exec "color " .
\ ((g:colors_name == "wombat256") ? "dimtag" : "wombat256")<CR>
Run Code Online (Sandbox Code Playgroud)
这里,这个nnoremap不会生成output(<silent>)并且基于
:exec,后面跟着这个表达式:
"color " . ((g:colors_name == "wombat256") ? "dimtag" : "wombat256")
Run Code Online (Sandbox Code Playgroud)
当g:colors_name设置为时wombat256,表达式求值为:
"color dimtag"
Run Code Online (Sandbox Code Playgroud)
或者,否则:
"color wombat256"
Run Code Online (Sandbox Code Playgroud)
然后评估任何一个:exec.当然你可以加入这些行(不要忘记删除反斜杠),我这样做只是为了避免过长的行.
| 归档时间: |
|
| 查看次数: |
949 次 |
| 最近记录: |