May 24, 2021 Vim
The last Vimscript type we're talking about will be a dictionary. The Vimscript dictionary is similar to the dict in Python, the hash in Ruby, and the object in Javascript.
The dictionary is created with braces. T he value is heterogeneic, but the key is cast into a string. It's as simple as that, didn't you?
To execute this command:
:echo {'a': 1, 100: 'foo'}
Vim
{'a':1,'100':'foo'}
which means that Vimscript does cast the key to a string while keeping the value unchanged.
Vimscript avoids the stupidity of the Javascript standard, allowing you to leave a comma behind the last element of the dictionary. I n Javascript standards, the last element cannot be followed by a comma. B ut in Firefox, it's allowed to leave that comma, but that's Firefox's problem. Do the following command:
:echo {'a': 1, 100: 'foo',}
Vim again shows
{'a':1,'100':'foo'}
You should always leave an extra comma in the dictionary, especially if the dictionary definition spans multiple lines, so adding new items is not easy to make mistakes.
The syntax for finding a value in a dictionary is the same as in most languages. To execute this command:
:echo {'a': 1, 100: 'foo',}['a']
Vim shows
1
.
Try using an index that is not a string:
:echo {'a': 1, 100: 'foo',}[100]
Vim makes sense to cast the index into a string before looking, because the key can only be a string.
Vimscript also supports Javascript-style "dot" lookups when the keys consist only of letters, numbers, and/or underscores. Try the following command:
:echo {'a': 1, 100: 'foo',}.a
:echo {'a': 1, 100: 'foo',}.100
In both cases, Vim shows the correct elements. The way you use an index dictionary depends on your own preferences.
Assigning items to a dictionary as if they were variables makes it easy to add new items to the dictionary.
:let foo = {'a': 1}
:let foo.a = 100
:let foo.b = 200
:echo foo
Vim
{'a': 100, 'b': 200}
.
The assignment is the same as adding a new item.
There are two ways to remove items from a dictionary. Execute the following command:
:let test = remove(foo, 'a')
:unlet foo.b
:echo foo
:echo test
Vim
{}
and
100
.
remove
function removes the item for a given key in a given dictionary and returns the value that was removed.
unlet
command also removes items from the dictionary, but does not return a value.
You cannot remove items that do not exist in the dictionary. Try this command:
:unlet foo["asdf"]
Vim throws an error.
The choice
remove
or
unlet
largely on personal preference. I
f I have to say, I recommend
remove
is more
unlet
remove
do anything
unlet
which in turn does not hold true.
So choose
remove
can be fresh, eat all over the day.
Like a list, Vim has many built-in dictionary functions. Execute the following command:
:echom get({'a': 100}, 'a', 'default')
:echom get({'a': 100}, 'b', 'default')
Vim displays
100
default
like the list version of
get
function.
You can also check whether a given key is in a given dictionary. To execute this command:
:echom has_key({'a': 100}, 'a')
:echom has_key({'a': 100}, 'b')
Vim shows
1
and
0
Don't forget that Vimscript
0
as a fake and other numbers as true.
You can execute this command by using
items
to get the corresponding key value pair from a dictionary:
:echo items({'a': 100, 'b': 200})
Vim will display such nested lists as
[['a',100],['b',200]]
and so on.
So far, the Vimscript dictionary is not necessarily ordered, so don't expect
items
to return in an orderly manner!
You can use
keys
return all the keys in the dictionary and
values
return all the values.
They work as well as they are - you can check it out.
Read
:help Dictionary
A
fter reading it.
Note capital
D
Read
:help get()
.
Read
:help has_key()
.
Read
:help items()
.
Read
:help keys()
.
Read
:help values()
.