vimscript中的Count()函数

时间:2013-08-07 10:11:01

标签: vim

我正在github上从spf13-vim读取.vimrc,而且有些事情我不明白:

" Snippets & AutoComplete
    if count(g:spf13_bundle_groups, 'snipmate')
        Bundle 'garbas/vim-snipmate'
        Bundle 'honza/vim-snippets'
        " Source support_function.vim to support vim-snippets.
        if filereadable(expand("~/.vim/bundle/vim-snippets/snippets/support_functions.vim"))
            source ~/.vim/bundle/vim-snippets/snippets/support_functions.vim
        endif
    elseif count(g:spf13_bundle_groups, 'neocomplcache')
        Bundle 'Shougo/neocomplcache'
        Bundle 'Shougo/neosnippet'
        Bundle 'honza/vim-snippets'
    elseif count(g:spf13_bundle_groups, 'neocomplete')
    Bundle 'Shougo/neocomplete.vim.git'
        Bundle 'Shougo/neosnippet'
        Bundle 'honza/vim-snippets'
    endif

count()功能在这里做了什么?

2 个答案:

答案 0 :(得分:1)

检查g:spf13_bundle_groups中是否存在项目(snipmate或neocomplcache ......)。

答案 1 :(得分:1)

:help count()告诉你:

  

返回值为{expr}的项目出现的次数   在|列表|或|字典| {排版}。

因此,这会计算List变量'snipmate'中值(例如g:spf13_bundle_groups)的出现频率。结果数字然后被解释为布尔值(0 =假,其他一切=真)。

更典型(也许更快一点)的方式是使用index()代替:

if index(g:spf13_bundle_groups, 'snipmate') != -1
    ...