Home Explore Blog CI



neovim

5th chunk of `runtime/doc/vimeval.txt`
4d36c5d26cf2e258f0920965a61b6eea510a98ec78d5eaec0000000100000fa2

List modification ~
							*list-modification*
To change a specific item of a list use |:let| this way: >
	:let list[4] = "four"
	:let listlist[0][3] = item

To change part of a list you can specify the first and last item to be
modified.  The value must at least have the number of items in the range: >
	:let list[3:5] = [3, 4, 5]

To add items to a List in-place, you can use |:let+=| (|list-concatenation|): >
	:let listA = [1, 2]
	:let listA += [3, 4]
<
When two variables refer to the same List, changing one List in-place will
cause the referenced List to be changed in-place: >
	:let listA = [1, 2]
	:let listB = listA
	:let listB += [3, 4]
	:echo listA
	[1, 2, 3, 4]
<
Adding and removing items from a list is done with functions.  Here are a few
examples: >
	:call insert(list, 'a')		" prepend item 'a'
	:call insert(list, 'a', 3)	" insert item 'a' before list[3]
	:call add(list, "new")		" append String item
	:call add(list, [1, 2])		" append a List as one new item
	:call extend(list, [1, 2])	" extend the list with two more items
	:let i = remove(list, 3)	" remove item 3
	:unlet list[3]			" idem
	:let l = remove(list, 3, -1)	" remove items 3 to last item
	:unlet list[3 : ]		" idem
	:call filter(list, 'v:val !~ "x"')  " remove items with an 'x'

Changing the order of items in a list: >
	:call sort(list)		" sort a list alphabetically
	:call reverse(list)		" reverse the order of items
	:call uniq(sort(list))		" sort and remove duplicates


For loop ~

The |:for| loop executes commands for each item in a |List|, |String| or |Blob|.
A variable is set to each item in sequence.  Example with a List: >
	:for item in mylist
	:   call Doit(item)
	:endfor

This works like: >
	:let index = 0
	:while index < len(mylist)
	:   let item = mylist[index]
	:   :call Doit(item)
	:   let index = index + 1
	:endwhile

If all you want to do is modify each item in the list then the |map()|
function will be a simpler method than a for loop.

Just like the |:let| command, |:for| also accepts a list of variables.  This
requires the argument to be a List of Lists. >
	:for [lnum, col] in [[1, 3], [2, 8], [3, 0]]
	:   call Doit(lnum, col)
	:endfor

This works like a |:let| command is done for each list item.  Again, the types
must remain the same to avoid an error.

It is also possible to put remaining items in a List variable: >
	:for [i, j; rest] in listlist
	:   call Doit(i, j)
	:   if !empty(rest)
	:      echo "remainder: " .. string(rest)
	:   endif
	:endfor

For a Blob one byte at a time is used.

For a String one character, including any composing characters, is used as a
String.  Example: >
	for c in text
	  echo 'This character is ' .. c
	endfor


List functions ~
						*E714*
Functions that are useful with a List: >
	:let r = call(funcname, list)	" call a function with an argument list
	:if empty(list)			" check if list is empty
	:let l = len(list)		" number of items in list
	:let big = max(list)		" maximum value in list
	:let small = min(list)		" minimum value in list
	:let xs = count(list, 'x')	" count nr of times 'x' appears in list
	:let i = index(list, 'x')	" index of first 'x' in list
	:let lines = getline(1, 10)	" get ten text lines from buffer
	:call append('$', lines)	" append text lines in buffer
	:let list = split("a b c")	" create list from items in a string
	:let string = join(list, ', ')	" create string from list items
	:let s = string(list)		" String representation of list
	:call map(list, '">> " .. v:val')  " prepend ">> " to each item

Don't forget that a combination of features can make things simple.  For
example, to add up all the numbers in a list: >
	:exe 'let sum = ' .. join(nrlist, '+')


1.4 Dictionaries ~
				 *Dict* *dict* *Dictionaries* *Dictionary*
A Dictionary is an associative array: Each entry has a key and a value.  The
entry can be located with the key.  The entries are stored without a specific
ordering.


Dictionary creation ~
						*E720* *E721* *E722* *E723*
A Dictionary is created with a comma-separated list of

Title: List Modification, Loops, Functions, and Introduction to Dictionaries
Summary
This section covers various methods for modifying lists, including changing items by index, replacing parts of a list, and in-place concatenation using `+=`. It also explains the behavior of list references and how changes to one reference affect others. The section details functions for adding, removing, and reordering list items (`insert`, `add`, `extend`, `remove`, `sort`, `reverse`, `uniq`, `filter`). The use of `:for` loops for iterating through lists, strings, and blobs is demonstrated. Common list-related functions like `call`, `empty`, `len`, `max`, `min`, `count`, `index`, `getline`, `append`, `split`, `join`, `string`, and `map` are introduced, with an example of summing numbers in a list. Finally, the section begins an introduction to Dictionaries, describing them as associative arrays with key-value pairs.