comparison runtime/doc/eval.txt @ 26769:30972227ac8d v8.2.3913

patch 8.2.3913: help for expressions does not mention Vim9 syntax Commit: https://github.com/vim/vim/commit/5da36052a4bb0f3a9747ec3a8ab9d85e058e39fa Author: Bram Moolenaar <Bram@vim.org> Date: Mon Dec 27 15:39:57 2021 +0000 patch 8.2.3913: help for expressions does not mention Vim9 syntax Problem: Help for expressions does not mention Vim9 syntax. Solution: Add the rules for Vim9 to the expression help. Rename functions to match the help.
author Bram Moolenaar <Bram@vim.org>
date Mon, 27 Dec 2021 16:45:03 +0100
parents f0d7cb510ce3
children 629e7046ef63
comparison
equal deleted inserted replaced
26768:cdea9da4aeb3 26769:30972227ac8d
1 *eval.txt* For Vim version 8.2. Last change: 2021 Dec 24 1 *eval.txt* For Vim version 8.2. Last change: 2021 Dec 27
2 2
3 3
4 VIM REFERENCE MANUAL by Bram Moolenaar 4 VIM REFERENCE MANUAL by Bram Moolenaar
5 5
6 6
95 *octal* 95 *octal*
96 Conversion from a String to a Number only happens in legacy Vim script, not in 96 Conversion from a String to a Number only happens in legacy Vim script, not in
97 Vim9 script. It is done by converting the first digits to a number. 97 Vim9 script. It is done by converting the first digits to a number.
98 Hexadecimal "0xf9", Octal "017" or "0o17", and Binary "0b10" 98 Hexadecimal "0xf9", Octal "017" or "0o17", and Binary "0b10"
99 numbers are recognized 99 numbers are recognized
100 NOTE: when using |scriptversion-4| octal with a leading "0" is not recognized. 100 NOTE: when using |Vim9| script or |scriptversion-4| octal with a leading "0"
101 The 0o notation requires patch 8.2.0886. 101 is not recognized. The 0o notation requires patch 8.2.0886.
102 If the String doesn't start with digits, the result is zero. 102 If the String doesn't start with digits, the result is zero.
103 Examples: 103 Examples:
104 String "456" --> Number 456 ~ 104 String "456" --> Number 456 ~
105 String "6bar" --> Number 6 ~ 105 String "6bar" --> Number 6 ~
106 String "foo" --> Number 0 ~ 106 String "foo" --> Number 0 ~
521 A key is always a String. You can use a Number, it will be converted to a 521 A key is always a String. You can use a Number, it will be converted to a
522 String automatically. Thus the String '4' and the number 4 will find the same 522 String automatically. Thus the String '4' and the number 4 will find the same
523 entry. Note that the String '04' and the Number 04 are different, since the 523 entry. Note that the String '04' and the Number 04 are different, since the
524 Number will be converted to the String '4'. The empty string can also be used 524 Number will be converted to the String '4'. The empty string can also be used
525 as a key. 525 as a key.
526
527 In |Vim9| script literaly keys can be used if the key consists of alphanumeric
528 characters, underscore and dash, see |vim9-literal-dict|.
526 *literal-Dict* *#{}* 529 *literal-Dict* *#{}*
527 To avoid having to put quotes around every key the #{} form can be used. This 530 To avoid having to put quotes around every key the #{} form can be used in
528 does require the key to consist only of ASCII letters, digits, '-' and '_'. 531 legacy script. This does require the key to consist only of ASCII letters,
529 Example: > 532 digits, '-' and '_'. Example: >
530 :let mydict = #{zero: 0, one_key: 1, two-key: 2, 333: 3} 533 :let mydict = #{zero: 0, one_key: 1, two-key: 2, 333: 3}
531 Note that 333 here is the string "333". Empty keys are not possible with #{}. 534 Note that 333 here is the string "333". Empty keys are not possible with #{}.
535 In |Vim9| script the #{} form cannot be used.
532 536
533 A value can be any expression. Using a Dictionary for a value creates a 537 A value can be any expression. Using a Dictionary for a value creates a
534 nested Dictionary: > 538 nested Dictionary: >
535 :let nestdict = {1: {11: 'a', 12: 'b'}, 2: {21: 'c'}} 539 :let nestdict = {1: {11: 'a', 12: 'b'}, 2: {21: 'c'}}
536 540
823 my_var_6 not 827 my_var_6 not
824 My_Var_6 session file 828 My_Var_6 session file
825 MY_VAR_6 viminfo file 829 MY_VAR_6 viminfo file
826 830
827 831
828 It's possible to form a variable name with curly braces, see 832 In legacy script it is possible to form a variable name with curly braces, see
829 |curly-braces-names|. 833 |curly-braces-names|.
830 834
831 ============================================================================== 835 ==============================================================================
832 2. Expression syntax *expression-syntax* 836 2. Expression syntax *expression-syntax*
833 837
871 expr7 * expr7 ... number multiplication 875 expr7 * expr7 ... number multiplication
872 expr7 / expr7 ... number division 876 expr7 / expr7 ... number division
873 expr7 % expr7 ... number modulo 877 expr7 % expr7 ... number modulo
874 878
875 |expr7| expr8 879 |expr7| expr8
876 ! expr7 logical NOT 880 <type>expr8 type check and conversion (|Vim9| only)
877 - expr7 unary minus
878 + expr7 unary plus
879 881
880 |expr8| expr9 882 |expr8| expr9
881 expr8[expr1] byte of a String or item of a |List| 883 ! expr8 logical NOT
882 expr8[expr1 : expr1] substring of a String or sublist of a |List| 884 - expr8 unary minus
883 expr8.name entry in a |Dictionary| 885 + expr8 unary plus
884 expr8(expr1, ...) function call with |Funcref| variable 886
885 expr8->name(expr1, ...) |method| call 887 |expr9| expr10
886 888 expr9[expr1] byte of a String or item of a |List|
887 |expr9| number number constant 889 expr9[expr1 : expr1] substring of a String or sublist of a |List|
890 expr9.name entry in a |Dictionary|
891 expr9(expr1, ...) function call with |Funcref| variable
892 expr9->name(expr1, ...) |method| call
893
894 |expr10| number number constant
888 "string" string constant, backslash is special 895 "string" string constant, backslash is special
889 'string' string constant, ' is doubled 896 'string' string constant, ' is doubled
890 [expr1, ...] |List| 897 [expr1, ...] |List|
891 {expr1: expr1, ...} |Dictionary| 898 {expr1: expr1, ...} |Dictionary|
892 #{key: expr1, ...} |Dictionary| 899 #{key: expr1, ...} legacy |Dictionary|
893 &option option value 900 &option option value
894 (expr1) nested expression 901 (expr1) nested expression
895 variable internal variable 902 variable internal variable
896 va{ria}ble internal variable with curly braces 903 va{ria}ble internal variable with curly braces
897 $VAR environment variable 904 $VAR environment variable
898 @r contents of register 'r' 905 @r contents of register 'r'
899 function(expr1, ...) function call 906 function(expr1, ...) function call
900 func{ti}on(expr1, ...) function call with curly braces 907 func{ti}on(expr1, ...) function call with curly braces
901 {args -> expr1} lambda expression 908 {args -> expr1} legacy lambda expression
909 (args) => expr1 Vim9 lambda expression
902 910
903 911
904 "..." indicates that the operations in this level can be concatenated. 912 "..." indicates that the operations in this level can be concatenated.
905 Example: > 913 Example: >
906 &nu || &list && &shell == "csh" 914 &nu || &list && &shell == "csh"
914 The trinary operator: expr2 ? expr1 : expr1 922 The trinary operator: expr2 ? expr1 : expr1
915 The falsy operator: expr2 ?? expr1 923 The falsy operator: expr2 ?? expr1
916 924
917 Trinary operator ~ 925 Trinary operator ~
918 926
919 The expression before the '?' is evaluated to a number. If it evaluates to 927 In legacy script the expression before the '?' is evaluated to a number. If
920 |TRUE|, the result is the value of the expression between the '?' and ':', 928 it evaluates to |TRUE|, the result is the value of the expression between the
921 otherwise the result is the value of the expression after the ':'. 929 '?' and ':', otherwise the result is the value of the expression after the
930 ':'.
931
932 In |Vim9| script the first expression must evaluate to a boolean, see
933 |vim9-boolean|.
934
922 Example: > 935 Example: >
923 :echo lnum == 1 ? "top" : lnum 936 :echo lnum == 1 ? "top" : lnum
924 937
925 Since the first expression is an "expr2", it cannot contain another ?:. The 938 Since the first expression is an "expr2", it cannot contain another ?:. The
926 other two expressions can, thus allow for recursive use of ?:. 939 other two expressions can, thus allow for recursive use of ?:.
950 echo GetName() ?? 'unknown' 963 echo GetName() ?? 'unknown'
951 964
952 These are similar, but not equal: > 965 These are similar, but not equal: >
953 expr2 ?? expr1 966 expr2 ?? expr1
954 expr2 ? expr2 : expr1 967 expr2 ? expr2 : expr1
955 In the second line "expr2" is evaluated twice. 968 In the second line "expr2" is evaluated twice. And in |Vim9| script the type
969 of expr2 before "?" must be a boolean.
956 970
957 971
958 expr2 and expr3 *expr2* *expr3* 972 expr2 and expr3 *expr2* *expr3*
959 --------------- 973 ---------------
960 974
961 expr3 || expr3 .. logical OR *expr-barbar* 975 expr3 || expr3 .. logical OR *expr-barbar*
962 expr4 && expr4 .. logical AND *expr-&&* 976 expr4 && expr4 .. logical AND *expr-&&*
963 977
964 The "||" and "&&" operators take one argument on each side. The arguments 978 The "||" and "&&" operators take one argument on each side.
965 are (converted to) Numbers. The result is: 979
966 980 In legacy script the arguments are (converted to) Numbers.
981
982 In |Vim9| script the values must be boolean, see |vim9-boolean|. Use "!!" to
983 convert any type to a boolean.
984
985 The result is:
967 input output ~ 986 input output ~
968 n1 n2 n1 || n2 n1 && n2 ~ 987 n1 n2 n1 || n2 n1 && n2 ~
969 |FALSE| |FALSE| |FALSE| |FALSE| 988 |FALSE| |FALSE| |FALSE| |FALSE|
970 |FALSE| |TRUE| |TRUE| |FALSE| 989 |FALSE| |TRUE| |TRUE| |FALSE|
971 |TRUE| |FALSE| |TRUE| |FALSE| 990 |TRUE| |FALSE| |TRUE| |FALSE|
997 expr4 *expr4* 1016 expr4 *expr4*
998 ----- 1017 -----
999 1018
1000 expr5 {cmp} expr5 1019 expr5 {cmp} expr5
1001 1020
1002 Compare two expr5 expressions, resulting in a 0 if it evaluates to false, or 1 1021 Compare two expr5 expressions. In legacy script the result is a 0 if it
1003 if it evaluates to true. 1022 evaluates to false, or 1 if it evaluates to true. In |Vim9| script the result
1023 is |true| or |false|.
1004 1024
1005 *expr-==* *expr-!=* *expr->* *expr->=* 1025 *expr-==* *expr-!=* *expr->* *expr->=*
1006 *expr-<* *expr-<=* *expr-=~* *expr-!~* 1026 *expr-<* *expr-<=* *expr-=~* *expr-!~*
1007 *expr-==#* *expr-!=#* *expr->#* *expr->=#* 1027 *expr-==#* *expr-!=#* *expr->#* *expr->=#*
1008 *expr-<#* *expr-<=#* *expr-=~#* *expr-!~#* 1028 *expr-<#* *expr-<=#* *expr-=~#* *expr-!~#*
1024 1044
1025 Examples: 1045 Examples:
1026 "abc" ==# "Abc" evaluates to 0 1046 "abc" ==# "Abc" evaluates to 0
1027 "abc" ==? "Abc" evaluates to 1 1047 "abc" ==? "Abc" evaluates to 1
1028 "abc" == "Abc" evaluates to 1 if 'ignorecase' is set, 0 otherwise 1048 "abc" == "Abc" evaluates to 1 if 'ignorecase' is set, 0 otherwise
1049 NOTE: In |Vim9| script 'ignorecase' is not used.
1029 1050
1030 *E691* *E692* 1051 *E691* *E692*
1031 A |List| can only be compared with a |List| and only "equal", "not equal", 1052 A |List| can only be compared with a |List| and only "equal", "not equal",
1032 "is" and "isnot" can be used. This compares the values of the list, 1053 "is" and "isnot" can be used. This compares the values of the list,
1033 recursively. Ignoring case means case is ignored when comparing item values. 1054 recursively. Ignoring case means case is ignored when comparing item values.
1062 0 1083 0
1063 echo 0 is [] 1084 echo 0 is []
1064 0 1085 0
1065 "is#"/"isnot#" and "is?"/"isnot?" can be used to match and ignore case. 1086 "is#"/"isnot#" and "is?"/"isnot?" can be used to match and ignore case.
1066 1087
1067 When comparing a String with a Number, the String is converted to a Number, 1088 In legacy script, when comparing a String with a Number, the String is
1068 and the comparison is done on Numbers. This means that: > 1089 converted to a Number, and the comparison is done on Numbers. This means
1090 that: >
1069 echo 0 == 'x' 1091 echo 0 == 'x'
1070 1 1092 1
1071 because 'x' converted to a Number is zero. However: > 1093 because 'x' converted to a Number is zero. However: >
1072 echo [0] == ['x'] 1094 echo [0] == ['x']
1073 0 1095 0
1074 Inside a List or Dictionary this conversion is not used. 1096 Inside a List or Dictionary this conversion is not used.
1097
1098 In |Vim9| script the types must match.
1075 1099
1076 When comparing two Strings, this is done with strcmp() or stricmp(). This 1100 When comparing two Strings, this is done with strcmp() or stricmp(). This
1077 results in the mathematical difference (comparing byte values), not 1101 results in the mathematical difference (comparing byte values), not
1078 necessarily the alphabetical difference in the local language. 1102 necessarily the alphabetical difference in the local language.
1079 1103
1108 For |Lists| only "+" is possible and then both expr6 must be a list. The 1132 For |Lists| only "+" is possible and then both expr6 must be a list. The
1109 result is a new list with the two lists Concatenated. 1133 result is a new list with the two lists Concatenated.
1110 1134
1111 For String concatenation ".." is preferred, since "." is ambiguous, it is also 1135 For String concatenation ".." is preferred, since "." is ambiguous, it is also
1112 used for |Dict| member access and floating point numbers. 1136 used for |Dict| member access and floating point numbers.
1113 When |vimscript-version| is 2 or higher, using "." is not allowed. 1137 In |Vim9| script and when |vimscript-version| is 2 or higher, using "." is not
1138 allowed.
1139
1140 In |Vim9| script the arguments of ".." are converted to String for simple
1141 types: Number, Float, Special and Bool. For other types |string()| should be
1142 used.
1114 1143
1115 expr7 * expr7 Number multiplication *expr-star* 1144 expr7 * expr7 Number multiplication *expr-star*
1116 expr7 / expr7 Number division *expr-/* 1145 expr7 / expr7 Number division *expr-/*
1117 expr7 % expr7 Number modulo *expr-%* 1146 expr7 % expr7 Number modulo *expr-%*
1118 1147
1119 For all, except "." and "..", Strings are converted to Numbers. 1148 In legacy script, for all operators except "." and "..", Strings are converted
1149 to Numbers.
1150
1120 For bitwise operators see |and()|, |or()| and |xor()|. 1151 For bitwise operators see |and()|, |or()| and |xor()|.
1121 1152
1122 Note the difference between "+" and ".": 1153 Note the difference between "+" and ".." in legacy script:
1123 "123" + "456" = 579 1154 "123" + "456" = 579
1124 "123" . "456" = "123456" 1155 "123" .. "456" = "123456"
1125 1156
1126 Since '.' has the same precedence as '+' and '-', you need to read: > 1157 Since '..' has the same precedence as '+' and '-', you need to read: >
1127 1 . 90 + 90.0 1158 1 .. 90 + 90.0
1128 As: > 1159 As: >
1129 (1 . 90) + 90.0 1160 (1 .. 90) + 90.0
1130 That works, since the String "190" is automatically converted to the Number 1161 That works in legacy script, since the String "190" is automatically converted
1131 190, which can be added to the Float 90.0. However: > 1162 to the Number 190, which can be added to the Float 90.0. However: >
1132 1 . 90 * 90.0 1163 1 .. 90 * 90.0
1133 Should be read as: > 1164 Should be read as: >
1134 1 . (90 * 90.0) 1165 1 .. (90 * 90.0)
1135 Since '.' has lower precedence than '*'. This does NOT work, since this 1166 Since '..' has lower precedence than '*'. This does NOT work, since this
1136 attempts to concatenate a Float and a String. 1167 attempts to concatenate a Float and a String.
1137 1168
1138 When dividing a Number by zero the result depends on the value: 1169 When dividing a Number by zero the result depends on the value:
1139 0 / 0 = -0x80000000 (like NaN for Float) 1170 0 / 0 = -0x80000000 (like NaN for Float)
1140 >0 / 0 = 0x7fffffff (like positive infinity) 1171 >0 / 0 = 0x7fffffff (like positive infinity)
1148 1179
1149 When the righthand side of '%' is zero, the result is 0. 1180 When the righthand side of '%' is zero, the result is 0.
1150 1181
1151 None of these work for |Funcref|s. 1182 None of these work for |Funcref|s.
1152 1183
1153 . and % do not work for Float. *E804* 1184 ".", ".." and "%" do not work for Float. *E804*
1154 1185
1155 1186
1156 expr7 *expr7* 1187 expr7 *expr7*
1157 ----- 1188 -----
1158 ! expr7 logical NOT *expr-!* 1189 <type>expr8
1159 - expr7 unary minus *expr-unary--* 1190
1160 + expr7 unary plus *expr-unary-+* 1191 This is only available in |Vim9| script, see |type-casting|.
1192
1193
1194 expr8 *expr8*
1195 -----
1196 ! expr8 logical NOT *expr-!*
1197 - expr8 unary minus *expr-unary--*
1198 + expr8 unary plus *expr-unary-+*
1161 1199
1162 For '!' |TRUE| becomes |FALSE|, |FALSE| becomes |TRUE| (one). 1200 For '!' |TRUE| becomes |FALSE|, |FALSE| becomes |TRUE| (one).
1163 For '-' the sign of the number is changed. 1201 For '-' the sign of the number is changed.
1164 For '+' the number is unchanged. Note: "++" has no effect. 1202 For '+' the number is unchanged. Note: "++" has no effect.
1165 1203
1166 A String will be converted to a Number first. 1204 In legacy script a String will be converted to a Number first. Note that if
1205 the string does not start with a digit you likely don't get what you expect.
1206
1207 In |Vim9| script an error is given when "-" or "+" is used and the type is not
1208 a number.
1209
1210 In |Vim9| script "!" can be used for any type and the result is always a
1211 boolean. Use "!!" to convert any type to a boolean, according to whether the
1212 value is |falsy|.
1167 1213
1168 These three can be repeated and mixed. Examples: 1214 These three can be repeated and mixed. Examples:
1169 !-1 == 0 1215 !-1 == 0
1170 !!8 == 1 1216 !!8 == 1
1171 --9 == 9 1217 --9 == 9
1172 1218
1173 1219
1174 expr8 *expr8* 1220 expr9 *expr9*
1175 ----- 1221 -----
1176 This expression is either |expr9| or a sequence of the alternatives below, 1222 This expression is either |expr10| or a sequence of the alternatives below,
1177 in any order. E.g., these are all possible: 1223 in any order. E.g., these are all possible:
1178 expr8[expr1].name 1224 expr9[expr1].name
1179 expr8.name[expr1] 1225 expr9.name[expr1]
1180 expr8(expr1, ...)[expr1].name 1226 expr9(expr1, ...)[expr1].name
1181 expr8->(expr1, ...)[expr1] 1227 expr9->(expr1, ...)[expr1]
1182 Evaluation is always from left to right. 1228 Evaluation is always from left to right.
1183 1229
1184 expr8[expr1] item of String or |List| *expr-[]* *E111* 1230 expr9[expr1] item of String or |List| *expr-[]* *E111*
1185 *E909* *subscript* 1231 *E909* *subscript*
1186 In legacy Vim script: 1232 In legacy Vim script:
1187 If expr8 is a Number or String this results in a String that contains the 1233 If expr9 is a Number or String this results in a String that contains the
1188 expr1'th single byte from expr8. expr8 is used as a String (a number is 1234 expr1'th single byte from expr9. expr9 is used as a String (a number is
1189 automatically converted to a String), expr1 as a Number. This doesn't 1235 automatically converted to a String), expr1 as a Number. This doesn't
1190 recognize multibyte encodings, see `byteidx()` for an alternative, or use 1236 recognize multibyte encodings, see `byteidx()` for an alternative, or use
1191 `split()` to turn the string into a list of characters. Example, to get the 1237 `split()` to turn the string into a list of characters. Example, to get the
1192 byte under the cursor: > 1238 byte under the cursor: >
1193 :let c = getline(".")[col(".") - 1] 1239 :let c = getline(".")[col(".") - 1]
1194 1240
1195 In Vim9 script: 1241 In |Vim9| script:
1196 If expr8 is a String this results in a String that contains the expr1'th 1242 If expr9 is a String this results in a String that contains the expr1'th
1197 single character (including any composing characters) from expr8. To use byte 1243 single character (including any composing characters) from expr9. To use byte
1198 indexes use |strpart()|. 1244 indexes use |strpart()|.
1199 1245
1200 Index zero gives the first byte or character. Careful: text column numbers 1246 Index zero gives the first byte or character. Careful: text column numbers
1201 start with one! 1247 start with one!
1202 1248
1203 If the length of the String is less than the index, the result is an empty 1249 If the length of the String is less than the index, the result is an empty
1204 String. A negative index always results in an empty string (reason: backward 1250 String. A negative index always results in an empty string (reason: backward
1205 compatibility). Use [-1:] to get the last byte or character. 1251 compatibility). Use [-1:] to get the last byte or character.
1206 In Vim9 script a negative index is used like with a list: count from the end. 1252 In Vim9 script a negative index is used like with a list: count from the end.
1207 1253
1208 If expr8 is a |List| then it results the item at index expr1. See |list-index| 1254 If expr9 is a |List| then it results the item at index expr1. See |list-index|
1209 for possible index values. If the index is out of range this results in an 1255 for possible index values. If the index is out of range this results in an
1210 error. Example: > 1256 error. Example: >
1211 :let item = mylist[-1] " get last item 1257 :let item = mylist[-1] " get last item
1212 1258
1213 Generally, if a |List| index is equal to or higher than the length of the 1259 Generally, if a |List| index is equal to or higher than the length of the
1214 |List|, or more negative than the length of the |List|, this results in an 1260 |List|, or more negative than the length of the |List|, this results in an
1215 error. 1261 error.
1216 1262
1217 1263
1218 expr8[expr1a : expr1b] substring or sublist *expr-[:]* 1264 expr9[expr1a : expr1b] substring or sublist *expr-[:]*
1219 1265
1220 If expr8 is a String this results in the substring with the bytes or 1266 If expr9 is a String this results in the substring with the bytes or
1221 characters from expr1a to and including expr1b. expr8 is used as a String, 1267 characters from expr1a to and including expr1b. expr9 is used as a String,
1222 expr1a and expr1b are used as a Number. 1268 expr1a and expr1b are used as a Number.
1223 1269
1224 In legacy Vim script the indexes are byte indexes. This doesn't recognize 1270 In legacy Vim script the indexes are byte indexes. This doesn't recognize
1225 multibyte encodings, see |byteidx()| for computing the indexes. If expr8 is 1271 multibyte encodings, see |byteidx()| for computing the indexes. If expr9 is
1226 a Number it is first converted to a String. 1272 a Number it is first converted to a String.
1227 1273
1228 In Vim9 script the indexes are character indexes and include composing 1274 In Vim9 script the indexes are character indexes and include composing
1229 characters. To use byte indexes use |strpart()|. To use character indexes 1275 characters. To use byte indexes use |strpart()|. To use character indexes
1230 without including composing characters use |strcharpart()|. 1276 without including composing characters use |strcharpart()|.
1247 :let c = name[-2:-2] " last but one byte of a string 1293 :let c = name[-2:-2] " last but one byte of a string
1248 :let s = line(".")[4:] " from the fifth byte to the end 1294 :let s = line(".")[4:] " from the fifth byte to the end
1249 :let s = s[:-3] " remove last two bytes 1295 :let s = s[:-3] " remove last two bytes
1250 < 1296 <
1251 *slice* 1297 *slice*
1252 If expr8 is a |List| this results in a new |List| with the items indicated by 1298 If expr9 is a |List| this results in a new |List| with the items indicated by
1253 the indexes expr1a and expr1b. This works like with a String, as explained 1299 the indexes expr1a and expr1b. This works like with a String, as explained
1254 just above. Also see |sublist| below. Examples: > 1300 just above. Also see |sublist| below. Examples: >
1255 :let l = mylist[:3] " first four items 1301 :let l = mylist[:3] " first four items
1256 :let l = mylist[4:4] " List with one item 1302 :let l = mylist[4:4] " List with one item
1257 :let l = mylist[:] " shallow copy of a List 1303 :let l = mylist[:] " shallow copy of a List
1258 1304
1259 If expr8 is a |Blob| this results in a new |Blob| with the bytes in the 1305 If expr9 is a |Blob| this results in a new |Blob| with the bytes in the
1260 indexes expr1a and expr1b, inclusive. Examples: > 1306 indexes expr1a and expr1b, inclusive. Examples: >
1261 :let b = 0zDEADBEEF 1307 :let b = 0zDEADBEEF
1262 :let bs = b[1:2] " 0zADBE 1308 :let bs = b[1:2] " 0zADBE
1263 :let bs = b[:] " copy of 0zDEADBEEF 1309 :let bs = b[:] " copy of 0zDEADBEEF
1264 1310
1265 Using expr8[expr1] or expr8[expr1a : expr1b] on a |Funcref| results in an 1311 Using expr9[expr1] or expr9[expr1a : expr1b] on a |Funcref| results in an
1266 error. 1312 error.
1267 1313
1268 Watch out for confusion between a namespace and a variable followed by a colon 1314 Watch out for confusion between a namespace and a variable followed by a colon
1269 for a sublist: > 1315 for a sublist: >
1270 mylist[n:] " uses variable n 1316 mylist[n:] " uses variable n
1271 mylist[s:] " uses namespace s:, error! 1317 mylist[s:] " uses namespace s:, error!
1272 1318
1273 1319
1274 expr8.name entry in a |Dictionary| *expr-entry* 1320 expr9.name entry in a |Dictionary| *expr-entry*
1275 1321
1276 If expr8 is a |Dictionary| and it is followed by a dot, then the following 1322 If expr9 is a |Dictionary| and it is followed by a dot, then the following
1277 name will be used as a key in the |Dictionary|. This is just like: 1323 name will be used as a key in the |Dictionary|. This is just like:
1278 expr8[name]. 1324 expr9[name].
1279 1325
1280 The name must consist of alphanumeric characters, just like a variable name, 1326 The name must consist of alphanumeric characters, just like a variable name,
1281 but it may start with a number. Curly braces cannot be used. 1327 but it may start with a number. Curly braces cannot be used.
1282 1328
1283 There must not be white space before or after the dot. 1329 There must not be white space before or after the dot.
1290 1336
1291 Note that the dot is also used for String concatenation. To avoid confusion 1337 Note that the dot is also used for String concatenation. To avoid confusion
1292 always put spaces around the dot for String concatenation. 1338 always put spaces around the dot for String concatenation.
1293 1339
1294 1340
1295 expr8(expr1, ...) |Funcref| function call 1341 expr9(expr1, ...) |Funcref| function call
1296 1342
1297 When expr8 is a |Funcref| type variable, invoke the function it refers to. 1343 When expr9 is a |Funcref| type variable, invoke the function it refers to.
1298 1344
1299 1345
1300 expr8->name([args]) method call *method* *->* 1346 expr9->name([args]) method call *method* *->*
1301 expr8->{lambda}([args]) 1347 expr9->{lambda}([args])
1302 *E276* 1348 *E276*
1303 For methods that are also available as global functions this is the same as: > 1349 For methods that are also available as global functions this is the same as: >
1304 name(expr8 [, args]) 1350 name(expr9 [, args])
1305 There can also be methods specifically for the type of "expr8". 1351 There can also be methods specifically for the type of "expr9".
1306 1352
1307 This allows for chaining, passing the value that one method returns to the 1353 This allows for chaining, passing the value that one method returns to the
1308 next method: > 1354 next method: >
1309 mylist->filter(filterexpr)->map(mapexpr)->sort()->join() 1355 mylist->filter(filterexpr)->map(mapexpr)->sort()->join()
1310 < 1356 <
1311 Example of using a lambda: > 1357 Example of using a lambda: >
1312 GetPercentage()->{x -> x * 100}()->printf('%d%%') 1358 GetPercentage()->{x -> x * 100}()->printf('%d%%')
1313 < 1359 <
1314 When using -> the |expr7| operators will be applied first, thus: > 1360 When using -> the |expr8| operators will be applied first, thus: >
1315 -1.234->string() 1361 -1.234->string()
1316 Is equivalent to: > 1362 Is equivalent to: >
1317 (-1.234)->string() 1363 (-1.234)->string()
1318 And NOT: > 1364 And NOT: >
1319 -(1.234->string()) 1365 -(1.234->string())
1329 1375
1330 When using the lambda form there must be no white space between the } and the 1376 When using the lambda form there must be no white space between the } and the
1331 (. 1377 (.
1332 1378
1333 1379
1334 *expr9* 1380 *expr10*
1335 number 1381 number
1336 ------ 1382 ------
1337 number number constant *expr-number* 1383 number number constant *expr-number*
1338 1384
1339 *0x* *hex-number* *0o* *octal-number* *binary-number* 1385 *0x* *hex-number* *0o* *octal-number* *binary-number*
1533 See below |functions|. 1579 See below |functions|.
1534 1580
1535 1581
1536 lambda expression *expr-lambda* *lambda* 1582 lambda expression *expr-lambda* *lambda*
1537 ----------------- 1583 -----------------
1538 {args -> expr1} lambda expression 1584 {args -> expr1} legacy lambda expression
1585 (args) => expr1 |Vim9| lambda expression
1539 1586
1540 A lambda expression creates a new unnamed function which returns the result of 1587 A lambda expression creates a new unnamed function which returns the result of
1541 evaluating |expr1|. Lambda expressions differ from |user-functions| in 1588 evaluating |expr1|. Lambda expressions differ from |user-functions| in
1542 the following ways: 1589 the following ways:
1543 1590
1551 The arguments are optional. Example: > 1598 The arguments are optional. Example: >
1552 :let F = {-> 'error function'} 1599 :let F = {-> 'error function'}
1553 :echo F('ignored') 1600 :echo F('ignored')
1554 < error function 1601 < error function
1555 1602
1556 Note that in Vim9 script another kind of lambda can be used: |vim9-lambda|. 1603 The |Vim9| lambda does not only use a different syntax, it also adds type
1604 checking and can be split over multiple lines, see |vim9-lambda|.
1557 1605
1558 *closure* 1606 *closure*
1559 Lambda expressions can access outer scope variables and arguments. This is 1607 Lambda expressions can access outer scope variables and arguments. This is
1560 often called a closure. Example where "i" and "a:arg" are used in a lambda 1608 often called a closure. Example where "i" and "a:arg" are used in a lambda
1561 while they already exist in the function scope. They remain valid even after 1609 while they already exist in the function scope. They remain valid even after
1608 1656
1609 ============================================================================== 1657 ==============================================================================
1610 3. Internal variable *internal-variables* *E461* 1658 3. Internal variable *internal-variables* *E461*
1611 1659
1612 An internal variable name can be made up of letters, digits and '_'. But it 1660 An internal variable name can be made up of letters, digits and '_'. But it
1613 cannot start with a digit. It's also possible to use curly braces, see 1661 cannot start with a digit. In legacy script it also possible to use curly
1614 |curly-braces-names|. 1662 braces, see |curly-braces-names|.
1615 1663
1616 An internal variable is created with the ":let" command |:let|. 1664 In legacy script ann internal variable is created with the ":let" command
1617 An internal variable is explicitly destroyed with the ":unlet" command 1665 |:let|. An internal variable is explicitly destroyed with the ":unlet"
1618 |:unlet|. 1666 command |:unlet|.
1619 Using a name that is not an internal variable or refers to a variable that has 1667 Using a name that is not an internal variable or refers to a variable that has
1620 been destroyed results in an error. 1668 been destroyed results in an error.
1669
1670 In |Vim9| script `:let` is not used and variables work differently, see |:var|.
1621 1671
1622 *variable-scope* 1672 *variable-scope*
1623 There are several name spaces for variables. Which one is to be used is 1673 There are several name spaces for variables. Which one is to be used is
1624 specified by what is prepended: 1674 specified by what is prepended:
1625 1675
1626 (nothing) In a function: local to a function; otherwise: global 1676 (nothing) In a function: local to the function;
1677 in a legacy script: global;
1678 in a |Vim9| script: local to the script
1627 |buffer-variable| b: Local to the current buffer. 1679 |buffer-variable| b: Local to the current buffer.
1628 |window-variable| w: Local to the current window. 1680 |window-variable| w: Local to the current window.
1629 |tabpage-variable| t: Local to the current tab page. 1681 |tabpage-variable| t: Local to the current tab page.
1630 |global-variable| g: Global. 1682 |global-variable| g: Global.
1631 |local-variable| l: Local to a function. 1683 |local-variable| l: Local to a function (only in a legacy function)
1632 |script-variable| s: Local to a |:source|'ed Vim script. 1684 |script-variable| s: Local to a |:source|'ed Vim script.
1633 |function-argument| a: Function argument (only inside a function). 1685 |function-argument| a: Function argument (only in a legacy function).
1634 |vim-variable| v: Global, predefined by Vim. 1686 |vim-variable| v: Global, predefined by Vim.
1635 1687
1636 The scope name by itself can be used as a |Dictionary|. For example, to 1688 The scope name by itself can be used as a |Dictionary|. For example, to
1637 delete all script-local variables: > 1689 delete all script-local variables: >
1638 :for k in keys(s:) 1690 :for k in keys(s:)
1639 : unlet s:[k] 1691 : unlet s:[k]
1640 :endfor 1692 :endfor
1641 1693
1642 Note: in Vim9 script this is different, see |vim9-scopes|. 1694 Note: in Vim9 script variables can also be local to a block of commands, see
1643 1695 |vim9-scopes|.
1644 *buffer-variable* *b:var* *b:* 1696 *buffer-variable* *b:var* *b:*
1645 A variable name that is preceded with "b:" is local to the current buffer. 1697 A variable name that is preceded with "b:" is local to the current buffer.
1646 Thus you can have several "b:foo" variables, one for each buffer. 1698 Thus you can have several "b:foo" variables, one for each buffer.
1647 This kind of variable is deleted when the buffer is wiped out or deleted with 1699 This kind of variable is deleted when the buffer is wiped out or deleted with
1648 |:bdelete|. 1700 |:bdelete|.
1980 When used as a string this evaluates to "v:false". > 2032 When used as a string this evaluates to "v:false". >
1981 echo v:false 2033 echo v:false
1982 < v:false ~ 2034 < v:false ~
1983 That is so that eval() can parse the string back to the same 2035 That is so that eval() can parse the string back to the same
1984 value. Read-only. 2036 value. Read-only.
2037 In |Vim9| script "false" can be used which has a boolean type.
1985 2038
1986 *v:fcs_reason* *fcs_reason-variable* 2039 *v:fcs_reason* *fcs_reason-variable*
1987 v:fcs_reason The reason why the |FileChangedShell| event was triggered. 2040 v:fcs_reason The reason why the |FileChangedShell| event was triggered.
1988 Can be used in an autocommand to decide what to do and/or what 2041 Can be used in an autocommand to decide what to do and/or what
1989 to set v:fcs_choice to. Possible values: 2042 to set v:fcs_choice to. Possible values:
2143 When used as a string this evaluates to "v:null". > 2196 When used as a string this evaluates to "v:null". >
2144 echo v:null 2197 echo v:null
2145 < v:null ~ 2198 < v:null ~
2146 That is so that eval() can parse the string back to the same 2199 That is so that eval() can parse the string back to the same
2147 value. Read-only. 2200 value. Read-only.
2201 In |Vim9| script "null" can be used without "v:".
2148 2202
2149 *v:numbermax* *numbermax-variable* 2203 *v:numbermax* *numbermax-variable*
2150 v:numbermax Maximum value of a number. 2204 v:numbermax Maximum value of a number.
2151 2205
2152 *v:numbermin* *numbermin-variable* 2206 *v:numbermin* *numbermin-variable*
2427 When used as a string this evaluates to "v:true". > 2481 When used as a string this evaluates to "v:true". >
2428 echo v:true 2482 echo v:true
2429 < v:true ~ 2483 < v:true ~
2430 That is so that eval() can parse the string back to the same 2484 That is so that eval() can parse the string back to the same
2431 value. Read-only. 2485 value. Read-only.
2486 In |Vim9| script "true" can be used which has a boolean type.
2432 *v:val* *val-variable* 2487 *v:val* *val-variable*
2433 v:val Value of the current item of a |List| or |Dictionary|. Only 2488 v:val Value of the current item of a |List| or |Dictionary|. Only
2434 valid while evaluating the expression used with |map()| and 2489 valid while evaluating the expression used with |map()| and
2435 |filter()|. Read-only. 2490 |filter()|. Read-only.
2436 2491
4921 < It is shorter when using a |lambda|: > 4976 < It is shorter when using a |lambda|: >
4922 call filter(myList, {idx, val -> idx * val <= 42}) 4977 call filter(myList, {idx, val -> idx * val <= 42})
4923 < If you do not use "val" you can leave it out: > 4978 < If you do not use "val" you can leave it out: >
4924 call filter(myList, {idx -> idx % 2 == 1}) 4979 call filter(myList, {idx -> idx % 2 == 1})
4925 < 4980 <
4981 In |Vim9| script the result must be true, false, zero or one.
4982 Other values will result in a type error.
4983
4926 For a |List| and a |Dictionary| the operation is done 4984 For a |List| and a |Dictionary| the operation is done
4927 in-place. If you want it to remain unmodified make a copy 4985 in-place. If you want it to remain unmodified make a copy
4928 first: > 4986 first: >
4929 :let l = filter(copy(mylist), 'v:val =~ "KEEP"') 4987 :let l = filter(copy(mylist), 'v:val =~ "KEEP"')
4930 4988
12577 The function name must start with an uppercase letter, to avoid confusion with 12635 The function name must start with an uppercase letter, to avoid confusion with
12578 builtin functions. To prevent from using the same name in different scripts 12636 builtin functions. To prevent from using the same name in different scripts
12579 avoid obvious, short names. A good habit is to start the function name with 12637 avoid obvious, short names. A good habit is to start the function name with
12580 the name of the script, e.g., "HTMLcolor()". 12638 the name of the script, e.g., "HTMLcolor()".
12581 12639
12582 It's also possible to use curly braces, see |curly-braces-names|. And the 12640 In legacy script it is also possible to use curly braces, see
12583 |autoload| facility is useful to define a function only when it's called. 12641 |curly-braces-names|.
12642 The |autoload| facility is useful to define a function only when it's called.
12584 12643
12585 *local-function* 12644 *local-function*
12586 A function local to a script must start with "s:". A local script function 12645 A function local to a legacy script must start with "s:". A local script
12587 can only be called from within the script and from functions, user commands 12646 function can only be called from within the script and from functions, user
12588 and autocommands defined in the script. It is also possible to call the 12647 commands and autocommands defined in the script. It is also possible to call
12589 function from a mapping defined in the script, but then |<SID>| must be used 12648 the function from a mapping defined in the script, but then |<SID>| must be
12590 instead of "s:" when the mapping is expanded outside of the script. 12649 used instead of "s:" when the mapping is expanded outside of the script.
12591 There are only script-local functions, no buffer-local or window-local 12650 There are only script-local functions, no buffer-local or window-local
12592 functions. 12651 functions.
12652
12653 In |Vim9| script functions are local to the script by default, prefix "g:" to
12654 define a global function.
12593 12655
12594 *:fu* *:function* *E128* *E129* *E123* 12656 *:fu* *:function* *E128* *E129* *E123*
12595 :fu[nction] List all functions and their arguments. 12657 :fu[nction] List all functions and their arguments.
12596 12658
12597 :fu[nction] {name} List function {name}. 12659 :fu[nction] {name} List function {name}.
12850 *:cal* *:call* *E107* *E117* 12912 *:cal* *:call* *E107* *E117*
12851 :[range]cal[l] {name}([arguments]) 12913 :[range]cal[l] {name}([arguments])
12852 Call a function. The name of the function and its arguments 12914 Call a function. The name of the function and its arguments
12853 are as specified with `:function`. Up to 20 arguments can be 12915 are as specified with `:function`. Up to 20 arguments can be
12854 used. The returned value is discarded. 12916 used. The returned value is discarded.
12855 Without a range and for functions that accept a range, the 12917 In |Vim9| script using `:call` is optional, these two lines do
12918 the same thing: >
12919 call SomeFunc(arg)
12920 SomeFunc(arg)
12921 < Without a range and for functions that accept a range, the
12856 function is called once. When a range is given the cursor is 12922 function is called once. When a range is given the cursor is
12857 positioned at the start of the first line before executing the 12923 positioned at the start of the first line before executing the
12858 function. 12924 function.
12859 When a range is given and the function doesn't handle it 12925 When a range is given and the function doesn't handle it
12860 itself, the function is executed for each line in the range, 12926 itself, the function is executed for each line in the range,
12995 In most places where you can use a variable, you can use a "curly braces name" 13061 In most places where you can use a variable, you can use a "curly braces name"
12996 variable. This is a regular variable name with one or more expressions 13062 variable. This is a regular variable name with one or more expressions
12997 wrapped in braces {} like this: > 13063 wrapped in braces {} like this: >
12998 my_{adjective}_variable 13064 my_{adjective}_variable
12999 13065
13066 This only works in legacy Vim script, not in |Vim9| script.
13067
13000 When Vim encounters this, it evaluates the expression inside the braces, puts 13068 When Vim encounters this, it evaluates the expression inside the braces, puts
13001 that in place of the expression, and re-interprets the whole as a variable 13069 that in place of the expression, and re-interprets the whole as a variable
13002 name. So in the above example, if the variable "adjective" was set to 13070 name. So in the above example, if the variable "adjective" was set to
13003 "noisy", then the reference would be to "my_noisy_variable", whereas if 13071 "noisy", then the reference would be to "my_noisy_variable", whereas if
13004 "adjective" was set to "quiet", then it would be to "my_quiet_variable". 13072 "adjective" was set to "quiet", then it would be to "my_quiet_variable".
13036 :echo @{i} " error 13104 :echo @{i} " error
13037 13105
13038 ============================================================================== 13106 ==============================================================================
13039 7. Commands *expression-commands* 13107 7. Commands *expression-commands*
13040 13108
13041 Note: in Vim9 script `:let` is used for variable declaration, not assignment. 13109 Note: in |Vim9| script `:let` is not used. `:var` is used for variable
13042 An assignment leaves out the `:let` command. |vim9-declaration| 13110 declarations and assignments do not use a command. |vim9-declaration|
13043 13111
13044 :let {var-name} = {expr1} *:let* *E18* 13112 :let {var-name} = {expr1} *:let* *E18*
13045 Set internal variable {var-name} to the result of the 13113 Set internal variable {var-name} to the result of the
13046 expression {expr1}. The variable will get the type 13114 expression {expr1}. The variable will get the type
13047 from the {expr}. If {var-name} didn't exist yet, it 13115 from the {expr}. If {var-name} didn't exist yet, it
13455 13523
13456 :for {var} in {object} *:for* *E690* *E732* 13524 :for {var} in {object} *:for* *E690* *E732*
13457 :endfo[r] *:endfo* *:endfor* 13525 :endfo[r] *:endfo* *:endfor*
13458 Repeat the commands between ":for" and ":endfor" for 13526 Repeat the commands between ":for" and ":endfor" for
13459 each item in {object}. {object} can be a |List| or 13527 each item in {object}. {object} can be a |List| or
13460 a |Blob|. Variable {var} is set to the value of each 13528 a |Blob|.
13461 item. When an error is detected for a command inside 13529
13462 the loop, execution continues after the "endfor". 13530 Variable {var} is set to the value of each item.
13531 In |Vim9| script the loop variable must not have been
13532 declared yet, unless when it is a
13533 global/window/tab/buffer variable.
13534
13535 When an error is detected for a command inside the
13536 loop, execution continues after the "endfor".
13463 Changing {object} inside the loop affects what items 13537 Changing {object} inside the loop affects what items
13464 are used. Make a copy if this is unwanted: > 13538 are used. Make a copy if this is unwanted: >
13465 :for item in copy(mylist) 13539 :for item in copy(mylist)
13466 < 13540 <
13467 When {object} is a |List| and not making a copy, Vim 13541 When {object} is a |List| and not making a copy, in
13468 stores a reference to the next item in the |List| 13542 legacy script Vim stores a reference to the next item
13469 before executing the commands with the current item. 13543 in the |List| before executing the commands with the
13470 Thus the current item can be removed without effect. 13544 current item. Thus the current item can be removed
13471 Removing any later item means it will not be found. 13545 without effect. Removing any later item means it will
13472 Thus the following example works (an inefficient way 13546 not be found. Thus the following example works (an
13473 to make a |List| empty): > 13547 inefficient way to make a |List| empty): >
13474 for item in mylist 13548 for item in mylist
13475 call remove(mylist, 0) 13549 call remove(mylist, 0)
13476 endfor 13550 endfor
13477 < Note that reordering the |List| (e.g., with sort() or 13551 < Note that reordering the |List| (e.g., with sort() or
13478 reverse()) may have unexpected effects. 13552 reverse()) may have unexpected effects.
13553 In |Vim9| script the index is used. If an item before
13554 the current one is deleted the next item will be
13555 skipped.
13479 13556
13480 When {object} is a |Blob|, Vim always makes a copy to 13557 When {object} is a |Blob|, Vim always makes a copy to
13481 iterate over. Unlike with |List|, modifying the 13558 iterate over. Unlike with |List|, modifying the
13482 |Blob| does not affect the iteration. 13559 |Blob| does not affect the iteration.
13483 13560