Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript list


May 24, 2021 Vim


Table of contents


So far we're familiar with the variable types in Vimscript, but we haven't talked about aggregates at all! Vim has two main collection types, and now we'll talk about the first one: the list.

The Vimscript list is an ordered, heterogeneic collection of elements. Execute the following command:

:echo ['foo', 3, 'bar']

Vim outputs this list. L ists can of course be nested. Execute the following command:

:echo ['foo', [3, 'bar']]

Vim will happily output this list.

Index

The index of the Vimscript list starts at 0, and you can get the corresponding element by undersscripting. To execute this command:

:echo [0, [1, 2]][1]

Vim [1,2] Y ou can also index from the end of the list, much like Python. To execute this command:

:echo [0, [1, 2]][-2]

Vim shows 0 . Index -1 to the last element of the list, -2 corresponds to the penultimate, and so on.

Cutting

Vim lists can also be cut. T his looks familiar to Python programmers, but it doesn't always behave like Python! To execute this command:

:echo ['a', 'b', 'c', 'd', 'e'][0:2]

Vim shows ['a','b','c'] (1, 2, 3 elements). I t is also safe to cross the upper bound of the list index. Try this command:

:echo ['a', 'b', 'c', 'd', 'e'][0:100000]

Vim only displays the entire list.

You can cut with a negative number. Try this command:

:echo ['a', 'b', 'c', 'd', 'e'][-2:-1]

Vim ['d','e'] (elements -2 and -1).

You can ignore the first index to represent "start" and/or the last index to represent "end." Execute the following command:

:echo ['a', 'b', 'c', 'd', 'e'][:1]
:echo ['a', 'b', 'c', 'd', 'e'][3:]

Vim shows ['a','b'] and ['d','e']

Like Python, Vimscript also allows you to index and cut strings. Follow these commands:

:echo "abcd"[0:2]

Vim shows abc . H owever, you cannot index strings with negative numbers. Y ou can cut strings with negative numbers! Execute the following command:

:echo "abcd"[-1] . "abcd"[-2:]

Vim cd (using a negative index does not correctly get an empty string).

Connection

You can + Vim list by using . Try this command:

:echo ['a', 'b'] + ['c']

Vim, not surprisingly, shows ['a','b','c'] There's nothing to say - in the strange World of Vimscript, the list is so normal that it's strange.

List function

Vim has many built-in list functions. To execute this command:

:let foo = ['a']
:call add(foo, 'b')
:echo foo

Vim places foo the end 'b' and ['a','b']

:echo len(foo)

Vim 2 which is the length of the list. Try the following command:

:echo get(foo, 0, 'default')
:echo get(foo, 100, 'default')

Vim shows a and default . get function returns the items of a given list for a given index, and returns a given default value if the index exceeds the list range.

To execute this command:

:echo index(foo, 'b')
:echo index(foo, 'nope')

Vim shows 1 and -1 index function returns the first index of a given item in a given list, or -1 if not in the list.

Now execute this command:

:echo join(foo)
:echo join(foo, '---')
:echo join([1, 2, 3], '')

Vim shows a b a---b and 123 . join first casts each item of a given list into a string, then splits it with a given split string (or a space, if not given) and connects it to a string.

Follow these commands:

:call reverse(foo)
:echo foo
:call reverse(foo)
:echo foo

Vim first shows ['b','a'] followed by ['a','b'] reverse transverts a given list.

Practice

Read :help List A fter reading it. Note capital L

Read :help add() .

Read: :help len() .

Read :help get() .

Read :help index() .

Read :help join() .

Read :help reverse() .

Browse :help functions other list functions that I haven't talked about. Perform :match Keyword /\clist/ highlight the word list in an insensitive list to make it easier for you to find.