qed
qed

Reputation: 23134

Count() function in vimscript

I was reading the .vimrc from spf13-vim on github, and there was something I didn't understand:

" 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

What does the count() function do here?

Upvotes: 0

Views: 606

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172648

:help count() tells you:

Return the number of times an item with value {expr} appears in |List| or |Dictionary| {comp}.

So this counts how often the value (e.g. 'snipmate') occurs in the List variable g:spf13_bundle_groups. The resulting number is then interpreted as a Boolean (0 = false, everything else = true).

A more canonical (and maybe a little bit faster) way would be to use index() instead:

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

Upvotes: 1

Kent
Kent

Reputation: 195179

check if the item (snipmate or neocomplcache...) exists in the g:spf13_bundle_groups.

Upvotes: 1

Related Questions