3

I apologise if this seems very basic but I wasn't able to find a satisfactory answer. In my vimrc I want to check if the lightline status bar is loaded and set options accordingly.

call plug#begin('~/.vim/plugged')
Plug 'itchyny/lightline.vim'
call plug#end()
...
...
if exists('g:loaded_lightline') && g:loaded_lightline 
    echom "loaded"
    set noshowmode
else 
    echom "not loaded"
    set showmode
    call MinimalStatusLine()
endif 

I get not loaded when I open up vim. What am I doing wrong ?

On a related note, I have a function called MinimalStatusLine() containing a bunch of set statusline+=... commands that I want to invoke as a backup when I don't have lightline installed. I currently have it in a separate .vim file inside ~/.vim/plugin directory (that vim automatically sources). But vim gives me an E117: Unknown function error.

4

Plugins are loaded after the .vimrc has been parsed. See :h startup.

Either check with globpath() whether the file you're interested in is installed where it should be -- but I guess it won't tell anything if it refused to load before setting the variable. (This is the same idea as the one described in this Q/A on SO: In my .vimrc, how can I check for the existence of a color scheme?)

Or in case the lightline plugin doesn't set the variable everytime it's sourced, you'll need to react to VimEnter instead.

Regarding your fallback function, put it in an autoload plugin in your 'runtimepath' (aka 'rtp') -- plugin managers main job is to maintain 'rtp' value.

A third option as suggested by D. Ben Knobble consists in putting the check in an after plugin. This should be perfect in most cases (as most plugins seldom haves file in the after/ tree or we rarely need to use commands like system() can cannot work before VimEnter step))

" ~/.vim/after/check_lightline.vim

if get(g:, 'loaded_lightline', 0)
    echom "loaded"
    set noshowmode
else 
    echom "not loaded"
    set showmode
    call s:MinimalStatusLine() " you no longer need an autoload plugin with this approach as the function could be in the same file
endif 
3
  • 1
    An alternative is ~/.vim/after/plugin/ files which are late enough that checks can work.
    – D. Ben Knoble
    Nov 30 '20 at 14:55
  • Just to clarify, you mean to put the check in an after-plugin Nov 30 '20 at 14:58
  • 1
    Yes; I do that in my dotfiles in ~/.vim/after/plugin/config/{plugin-name}.vim, for example, when I want to add mappings or what have you.
    – D. Ben Knoble
    Nov 30 '20 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.