在vim中切换功能 [英] Toggle function in vim

查看:69
本文介绍了在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 and false; use 1 and 0 instead.
  • Every if needs a closing endif.
  • umap should be unmap; the former does not exist.
  • nnoremap should be used in order to avoid recursive mappings.
  • : is unnecessary before commands in scripts.

这篇关于在vim中切换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆