3

Given a dictionary:

let g:dict = {
    \ 'FOO/BAR': 'BAZ',
    \ 'FIZZ': 'BUZZ'
}

Vim is fine with this:

:echo g:dict.FIZZ

But chokes on this:

:echo g:dict.FOO/BAR

E716: Key not present in Dictionary: HCL/HCL_0127
E15: Invalid expression: g:dict.FOO/BAR

I've also tried escaping the forward slash as \/, which doesn't work. However, it has no problem with g:dict['FOO/BAR'].

Is there any way to access these entries using the dot syntax? Are there other characters I have to watch out for?

1 Answer 1

3

You cannot use the dot form in your case because your key has something other than a letter, number, underscore:

:help dict

Accessing entries

The normal way to access an entry is by putting the key in square brackets:

:let val = mydict["one"]
:let mydict["four"] = 4

You can add new entries to an existing Dictionary this way, unlike Lists.

For keys that consist entirely of letters, digits and underscore the following form can be used |expr-entry|:

:let val = mydict.one
:let mydict.four = 4

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.