marcelocra
marcelocra

Reputation: 2493

Vimscript variables not set or not being detected when set with autocmd

I want to set variables only if certain plugins are available. To do that, I use:

augroup plugin_initialize
    autocmd!
    autocmd BufEnter * call LoadPluginSettings()
augroup END

function! LoadPluginSettings()
    if exists(':NERDTree')
        let NERDTreeIgnore = ['\.pyc$', '\.class$']
        nnoremap <silent> <Leader><Leader>d :NERDTreeCWD<CR>
    endif
    if has('python')
        if exists(':UltiSnipsEdit')
            let g:UltiSnipsExpandTrigger="<C-l>"
            let g:UltiSnipsJumpForwardTrigger="<C-l>"
            let g:UltiSnipsJumpBackwardTrigger="<C-h>"
        endif
    endif
endfunction

For NERDTree I get the mapping but not the variable (I believe because of the scope - is there an alternative?). The strangest thing is that for UltiSnips I get all the variables set correctly but they don't work as trigger (the trigger is the default, ).

Any idea? Thanks!

Upvotes: 0

Views: 194

Answers (1)

FDinoff
FDinoff

Reputation: 31429

You didn't add a scope modifier to NERDTreeIgnore. It defaults to a local variable inside functions. To make it a global variable you need to prefix it with g:. So it would be g:NERDTreeIgnore

function! LoadPluginSettings()
    if exists(':NERDTree')
        let g:NERDTreeIgnore = ['\.pyc$', '\.class$']
        nnoremap <silent> <Leader><Leader>d :NERDTreeCWD<CR>
    endif
    if has('python')
        if exists(':UltiSnipsEdit')
            let g:UltiSnipsExpandTrigger="<C-l>"
            let g:UltiSnipsJumpForwardTrigger="<C-l>"
            let g:UltiSnipsJumpBackwardTrigger="<C-h>"
        endif
    endif
endfunction

As for why the settings don't work. BufEnter happens after plugins have been loaded. When the plugin is loaded it checks the variables and sets the values appropriately. Changing the variable after the fact does nothing.

I think you should just leave the variables inside the vimrc. A couple of extra variables are not going to slow vim down. You can still conditionally load the mapping if you want.

Upvotes: 3

Related Questions