In Vimscript, how can one check whether the dictionary my_dict has the key abc? Something like this:

echo 'abc' in my_dict
" The above naive approach gives "E121: Undefined variable: in"

1 Answer 1


Use the has_key function:

has_key(my_dict, 'abc')

This will return 1 if the key is found, and 0 if the key is not found.

  • 4
    one can also use get(my_dict, 'abc', 'default_value_if_key_is_not_there')
    – Maxim Kim
    Commented Aug 26, 2021 at 12:41

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.