So I have a plugin that I am writing which should allow the user to set whether they want to enable a feature. I want the feature to be disabled by default, and allow the user to change it by adding something like this to their .vimrc let g:myplugin_enable_feature = 1, though when the user doesn't set the variable in their vimrc, it throws an error, as the variable is undefined. I can fix the error by defining the variable inside of my plugin script, though when I do that, the user loses the ability to change the variable.

What is the proper way to allow the user to decide whether they want to change an option, without vim throwing an error when the user doesn't do anything?

2 Answers 2


The simple way is

if get(g:, 'myplugin_enable_feature', defaultvalue)
   do whatever you want

Now when I need to check a setting in more than one place, I usually prefer to have a dedicated getter in my plugin to be sure I have the same default value everywhere

function! s:enable_feature() abort
    return get(g:, 'myplugin_enable_feature', defaultvalue)
if s:enable_feature()

If the setting can be buffer local, and could default on a global value that could default on some constant, I use dedicated functions.


Usually it suffices to use get() function. Like that

if get(g:, 'myplugin_enable_feature')
    " do something

If the feature should be enabled by default you can change it to get(g:, 'myplugin_enable_feature', 1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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