Expression syntax *expression-syntax*
Expression syntax summary, from least to most significant:
|expr1| expr2
expr2 ? expr1 : expr1 if-then-else
|expr2| expr3
expr3 || expr3 ... logical OR
|expr3| expr4
expr4 && expr4 ... logical AND
|expr4| expr5
expr5 == expr5 equal
expr5 != expr5 not equal
expr5 > expr5 greater than
expr5 >= expr5 greater than or equal
expr5 < expr5 smaller than
expr5 <= expr5 smaller than or equal
expr5 =~ expr5 regexp matches
expr5 !~ expr5 regexp doesn't match
expr5 ==? expr5 equal, ignoring case
expr5 ==# expr5 equal, match case
etc. As above, append ? for ignoring case, # for
matching case
expr5 is expr5 same |List|, |Dictionary| or |Blob| instance
expr5 isnot expr5 different |List|, |Dictionary| or |Blob|
instance
|expr5| expr6
expr6 + expr6 ... number addition, list or blob concatenation
expr6 - expr6 ... number subtraction
expr6 . expr6 ... string concatenation
expr6 .. expr6 ... string concatenation
|expr6| expr7
expr7 * expr7 ... number multiplication
expr7 / expr7 ... number division
expr7 % expr7 ... number modulo
|expr7| expr8
! expr7 logical NOT
- expr7 unary minus
+ expr7 unary plus
|expr8| expr9
expr8[expr1] byte of a String or item of a |List|
expr8[expr1 : expr1] substring of a String or sublist of a |List|
expr8.name entry in a |Dictionary|
expr8(expr1, ...) function call with |Funcref| variable
expr8->name(expr1, ...) |method| call
|expr9| number number constant
"string" string constant, backslash is special
`'string'` string constant, ' is doubled
[expr1, ...] |List|
`{expr1: expr1, ...}` |Dictionary|
#{key: expr1, ...} |Dictionary|
&option option value
(expr1) nested expression
variable internal variable
va{ria}ble internal variable with curly braces
$VAR environment variable
@r contents of register "r"
function(expr1, ...) function call
func{ti}on(expr1, ...) function call with curly braces
`{args -> expr1}` lambda expression
"..." indicates that the operations in this level can be concatenated.
Example: >
&nu || &list && &shell == "csh"
All expressions within one level are parsed from left to right.
Expression nesting is limited to 1000 levels deep (300 when build with MSVC)
to avoid running out of stack and crashing. *E1169*
------------------------------------------------------------------------------
expr1 *expr1* *ternary* *falsy-operator* *??* *E109*
The ternary operator: expr2 ? expr1 : expr1
The falsy operator: expr2 ?? expr1
Ternary operator ~
The expression before the '?' is evaluated to a number. If it evaluates to
|TRUE|, the result is the value of the expression between the '?' and ':',
otherwise the result is the value of the expression after the ':'.
Example: >
:echo lnum == 1 ? "top" : lnum
Since the first expression is an "expr2", it cannot contain another ?:. The
other two expressions can, thus allow for recursive use of ?:.
Example: >
:echo lnum == 1 ? "top" : lnum == 1000 ? "last" : lnum
To keep this readable, using |line-continuation| is suggested: >
:echo lnum == 1
:\ ? "top"
:\ : lnum == 1000
:\ ? "last"
:\ : lnum
You should always put a space before the ':', otherwise it can be mistaken for
use in a variable such as "a:1".
Falsy operator ~
This is also known as the "null coalescing operator", but that's too
complicated, thus we just call it the falsy operator.
The expression before the '??' is evaluated. If it evaluates to
|truthy|, this is used as the result. Otherwise the expression after the '??'
is evaluated and used as the result. This is most useful to have a default
value for an expression that may result in zero or empty: >
echo theList ?? 'list is empty'
echo GetName() ?? 'unknown'
These are similar, but not equal: >
expr2 ?? expr1
expr2 ? expr2 : expr1
In the second line "expr2" is evaluated twice.
------------------------------------------------------------------------------
expr2 and expr3 *expr2*