Lists

Vim also supports more complex data structures, such as lists and dictionaries. Here's an example of a list:

let animals = ['cat', 'dog', 'parrot']

Operations to modify lists are similar to the ones in Python. Let's take a look at some common operations.

You can get elements by index using the [n] syntax. Here are some examples:

let cat = animals[0]      " get first element
let dog = animals[1] " get second element
let parrot = animals[-1] " get last element

Slices work in a similar way to Python, for instance:

let slice = animals[1:]

The value of slice would be ['dog, 'parrot']. The main difference from Python is that the end of the range is inclusive:


let slice = animals[0:1]

The value of slice would be ['cat', 'dog'].

To append to the list, use add:

call add(animals, 'octopus')
Something to pay attention to is this: in Vimscript, we explicitly call functions with call unless they're a part of an expression. Don't worry too much about it yet, we'll get into more detail in a bit.

This will turn our list into ['cat', 'dog', 'parrot', 'octopus']. While it's an in-place operation, it also returns the modified list, so you can assign to it as well:

let animals = add(animals, 'octopus')

You can also prepend to the list using insert:

call insert(animals, 'bobcat')

This will modify the list to be ['bobcat', 'cat', 'dog', 'parrot', 'octopus'].

You can also provide an optional index argument. For instance, if you wanted to insert 'raven' at index 2 (where the 'dog' currently is) in the previous list, you would do the following:

call insert(animals, 'raven', 2)

The list will become ['bobcat', 'cat', 'raven', 'dog', 'parrot', 'octopus'].

There are a few ways to remove the elements. For example, you can use unlet to remove an element at index 2 ('raven'):

unlet animals[2]

The list will be back to ['bobcat', 'cat', 'dog', 'parrot', 'octopus'].

You can also use remove:

call remove(animals, -1)

This will leave us with ['bobcat', 'cat', 'dog', 'parrot'].

Additionally, remove also returns the item itself:

let bobcat = remove(animals, 0)

You can also use ranges with both unlet and remove. Here's an example of deleting everything up to and including the second element:

unlet animals[:1]

If you were to do this with remove, you'll have to specify boundaries explicitly:

call remove(animals, 0, 1)

You can concatenate the lists using + or extend. For example, given the lists mammals and birds:

let mammals = ['dog', 'cat']
let birds = ['raven', 'parrot']

We could create a new list:

let animals = mammals + birds

Here, animals will contain ['dog', 'cat', 'raven', 'parrot']. We could also extend the existing list:

call extend(mammals, birds)

This will extend mammals to contain ['dog', 'cat', 'raven', 'parrot'].

You can sort the list in place using sort. If we to use sort on a previous example, we would write:

call sort(animals)

The result would be ['cat', 'dog', 'parrot', 'raven'] (sorted alphabetically).

You can get an index of an element using index. For instance, if you wanted to get an index of parrot from the previous list, you would run this:

let i = index(animals, 'parrot')

In this case, i would be equal to 2.

You can check whether a list is empty using (an aptly named) empty:

if empty(animals)
echo 'There aren''t any animals!'
endif

The length of a list is retrieved using len:

echo 'There are ' . len(animals) . ' animals.'

Finally, Vim lets you count the number of elements in a list:

echo 'There are ' . count(animals, 'cat') . ' cats here.'
You can get a full list of operations by checking out the help page: :help list.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.105.124