如何在编写vimscript时使用选项?

时间:2012-04-04 17:11:15

标签: options vim

[已解决]似乎解决方案是:

if exists('g:nameOfMyOption') && g:nameOfMyOption
     ...
endif

这很简单,但我无法在网上找到答案。我想在插件文件中做这样的事情:

" MyChecker.vim
"
" Uncomment the following line to set the autochecker option
"set autochecker=1

if ISSET(autochecker)
   autocmd InsertChange * :call MyAutoCheckerFunction()
endif

我如何进行ISSET线路?我宁愿不必明确设置autochecker = 0,我只想检查autochecker是否存在。

编辑:当我尝试以下操作时:

if &autochecker == 1
    ...
endif

我收到此错误消息:

Error detected while processing MyChecker.vim:
line   32:
E113: Unknown option: autochecker
E15: Invalid expression: &autochecker == 1

2 个答案:

答案 0 :(得分:6)

您无法在vim中创建自定义选项。您需要创建一个全局变量,如:

let g:AutoChecker = 1

....

if g:AutoChecker == 1
   " ...
endif

答案 1 :(得分:1)

对于一般选项,我使用以下函数:lh#option#get()。 它首先搜索b:{varname}是否存在以返回其值,然后g:{varname}。否则,如果不存在任何变量,则返回提供的默认值。

对于编程相关选项(即可以为特定文件类型覆盖选项),我使用另一个函数:lh#dev#option#get()

C++ Options文档的 lh-cpp 部分间接解释了这两个功能背后的基本原理。

相关问题