1

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

3

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.

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

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.