Home Explore Blog CI



neovim

14th chunk of `runtime/doc/vimeval.txt`
c6b4f12da5c64ff7ac526d3984627aec32f5b75e9f435a6c0000000100000fa0
	expr8.name[expr1]
	expr8(expr1, ...)[expr1].name
	expr8->(expr1, ...)[expr1]
Evaluation is always from left to right.


expr8[expr1]		item of String or |List|	*expr-[]* *E111*
							*subscript*
In legacy Vim script:
If expr8 is a Number or String this results in a String that contains the
expr1'th single byte from expr8.  expr8 is used as a String (a number is
automatically converted to a String), expr1 as a Number.  This doesn't
recognize multibyte encodings, see `byteidx()` for an alternative, or use
`split()` to turn the string into a list of characters.  Example, to get the
byte under the cursor: >
	:let c = getline(".")[col(".") - 1]

Index zero gives the first byte.  This is like it works in C.  Careful:
text column numbers start with one!  Example, to get the byte under the
cursor: >
	:let c = getline(".")[col(".") - 1]

Index zero gives the first byte.  Careful: text column numbers start with one!

If the length of the String is less than the index, the result is an empty
String.  A negative index always results in an empty string (reason: backward
compatibility).  Use [-1:] to get the last byte.

If expr8 is a |List| then it results the item at index expr1.  See |list-index|
for possible index values.  If the index is out of range this results in an
error.  Example: >
	:let item = mylist[-1]		" get last item

Generally, if a |List| index is equal to or higher than the length of the
|List|, or more negative than the length of the |List|, this results in an
error.


expr8[expr1a : expr1b]	substring or |sublist|		*expr-[:]* *substring*

If expr8 is a String this results in the substring with the bytes or
characters from expr1a to and including expr1b.  expr8 is used as a String,
expr1a and expr1b are used as a Number.

In legacy Vim script the indexes are byte indexes.  This doesn't recognize
multibyte encodings, see |byteidx()| for computing the indexes.  If expr8 is
a Number it is first converted to a String.

The item at index expr1b is included, it is inclusive.  For an exclusive index
use the |slice()| function.

If expr1a is omitted zero is used.  If expr1b is omitted the length of the
string minus one is used.

A negative number can be used to measure from the end of the string.  -1 is
the last character, -2 the last but one, etc.

If an index goes out of range for the string characters are omitted.  If
expr1b is smaller than expr1a the result is an empty string.

Examples: >
	:let c = name[-1:]		" last byte of a string
	:let c = name[0:-1]		" the whole string
	:let c = name[-2:-2]		" last but one byte of a string
	:let s = line(".")[4:]		" from the fifth byte to the end
	:let s = s[:-3]			" remove last two bytes
<
							*slice*
If expr8 is a |List| this results in a new |List| with the items indicated by
the indexes expr1a and expr1b.  This works like with a String, as explained
just above. Also see |sublist| below.  Examples: >
	:let l = mylist[:3]		" first four items
	:let l = mylist[4:4]		" List with one item
	:let l = mylist[:]		" shallow copy of a List

If expr8 is a |Blob| this results in a new |Blob| with the bytes in the
indexes expr1a and expr1b, inclusive.  Examples: >
	:let b = 0zDEADBEEF
	:let bs = b[1:2]		" 0zADBE
	:let bs = b[]			" copy of 0zDEADBEEF

Using expr8[expr1] or expr8[expr1a : expr1b] on a |Funcref| results in an
error.

Watch out for confusion between a namespace and a variable followed by a colon
for a sublist: >
	mylist[n:]     " uses variable n
	mylist[s:]     " uses namespace s:, error!


expr8.name		entry in a |Dictionary|		*expr-entry*

If expr8 is a |Dictionary| and it is followed by a dot, then the following
name will be used as a key in the |Dictionary|.  This is just like:
expr8[name].

The name must consist of alphanumeric characters, just like a variable name,
but it may start with a number.  Curly braces cannot be used.

There must not be white space before or after the dot.

Examples: >
	:let dict = {"one": 1, 2: "two"}
	:echo dict.one		" shows "1"
	:echo dict.2		" shows

Title: Vim Expressions: String/List Indexing, Substrings/Sublists, and Dictionary Access
Summary
This section details how to access elements within strings and lists using indexing (`expr8[expr1]`) and how to extract substrings/sublists using slicing (`expr8[expr1a : expr1b]`). It covers both positive and negative indices, out-of-range behavior, and how these operations differ for strings and lists. It also explains how to access dictionary entries using the `.name` notation, which is equivalent to `expr8[name]`.