在vim中切换功能 [英] Toggle function in vim
问题描述
基本上我想做的是
map ,e :call ToggleEssayMode()<CR>
function! ToggleEssayMode()
if toggle==true
:map j gj
:map k gk
toggle=false
else
:umap j
:umap k
toggle=true
enndfunction
我已经看了一阵子了,但是我能找到的所有使用的人都是保留的vim变量.我可以自己设定变量吗?有更正确的方法吗?
I've looked around for a while, but all i could find people using were the reserved vim variables. Can I make my own variable? Is there a more correct way of doing this?
推荐答案
除非您使用单独的函数来启用和禁用,否则将需要一个flag变量,为了将其与其余配置隔离,我建议编写一个小插件.例如,在〜/.vim/plugin/中创建文件
具有以下内容: essay.vim
(实际名称无关紧要,只要它以 .vim
结尾即可).
Unless you use separate functions for enabling and disabling, you will need a flag variable, and in order to isolate this from the rest of your configuration, I would recommend writing a small plugin. For example, create a file essay.vim
(the actual name is irrelevant, as long as it ends in .vim
) in ~/.vim/plugin/
with the following content:
let s:enabled = 0
function! ToggleEssayMode()
if s:enabled
unmap j
unmap k
let s:enabled = 0
else
nnoremap j gj
nnoremap k gk
let s:enabled = 1
endif
endfunction
然后,调用 ToggleEssayMode()
的映射可以在同一文件中,也可以在您的 .vimrc
中.
The mapping to call ToggleEssayMode()
can then be in the same file or in your .vimrc
.
关于您的代码的一些评论:
Some remarks about your code:
- 使用
let
来分配变量(有关选项,请参见set
). - Vim不支持
true
和false
;改用1
和0
. - 每个
if
需要结束符endif
. -
umap
应该为unmap
;前者不存在. 为了避免递归映射,应使用 -
nnoremap
. 在脚本中的命令之前不需要 -
:
.
- Use
let
in order to assign variables (cf.set
for options). - Vim does not support
true
andfalse
; use1
and0
instead. - Every
if
needs a closingendif
. umap
should beunmap
; the former does not exist.nnoremap
should be used in order to avoid recursive mappings.:
is unnecessary before commands in scripts.
这篇关于在vim中切换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!