Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 1 | *vim9.txt* For Vim version 8.2. Last change: 2020 Apr 09 |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 8 | |
| 9 | Vim9 script commands and expressions. |
| 10 | |
| 11 | Most expression help is in |eval.txt|. This file is about the new syntax and |
| 12 | features in Vim9 script. |
| 13 | |
| 14 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 15 | |
| 16 | |
| 17 | 1 What is Vim9 script? |vim9-script| |
| 18 | 2. Differences |vim9-differences| |
| 19 | 3. New style functions |fast-functions| |
| 20 | 4. Types |vim9-types| |
| 21 | 5. Namespace, Import and Export |vim9script| |
| 22 | |
| 23 | 9. Rationale |vim9-rationale| |
| 24 | |
| 25 | ============================================================================== |
| 26 | |
| 27 | 1. What is Vim9 script? *vim9-script* |
| 28 | |
| 29 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 30 | |
| 31 | Vim script has been growing over time, while keeping backwards compatibility. |
| 32 | That means bad choices from the past often can't be changed. Execution is |
| 33 | quite slow, every line is parsed every time it is executed. |
| 34 | |
| 35 | The main goal of Vim9 script is to drastically improve performance. An |
| 36 | increase in execution speed of 10 to 100 times can be expected. A secondary |
| 37 | goal is to avoid Vim-specific constructs and get closer to commonly used |
| 38 | programming languages, such as JavaScript, TypeScript and Java. |
| 39 | |
| 40 | The performance improvements can only be achieved by not being 100% backwards |
| 41 | compatible. For example, in a function the arguments are not available in the |
| 42 | "a:" dictionary, as creating that dictionary adds quite a lot of overhead. |
| 43 | Other differences are more subtle, such as how errors are handled. |
| 44 | |
| 45 | The Vim9 script syntax and semantics are used in: |
| 46 | - a function defined with the `:def` command |
| 47 | - a script file where the first command is `vim9script` |
| 48 | |
| 49 | When using `:function` in a Vim9 script file the legacy syntax is used. |
| 50 | However, this is discouraged. |
| 51 | |
| 52 | Vim9 script and legacy Vim script can be mixed. There is no need to rewrite |
| 53 | old scripts, they keep working as before. |
| 54 | |
| 55 | ============================================================================== |
| 56 | |
| 57 | 2. Differences from legacy Vim script *vim9-differences* |
| 58 | |
| 59 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 60 | |
| 61 | Vim9 functions ~ |
| 62 | |
| 63 | `:def` has no extra arguments like `:function` does: "range", "abort", "dict" |
| 64 | or "closure". A `:def` function always aborts on an error, does not get a |
| 65 | range passed and cannot be a "dict" function. |
| 66 | |
| 67 | In the function body: |
| 68 | - Arguments are accessed by name, without "a:". |
| 69 | - There is no "a:" dictionary or "a:000" list. Variable arguments are defined |
| 70 | with a name and have a list type: > |
| 71 | def MyFunc(...itemlist: list<type>) |
| 72 | for item in itemlist |
| 73 | ... |
| 74 | |
| 75 | |
| 76 | Variable declarations with :let and :const ~ |
| 77 | |
| 78 | Local variables need to be declared with `:let`. Local constants need to be |
| 79 | declared with `:const`. We refer to both as "variables". |
| 80 | |
| 81 | Variables can be local to a script, function or code block: > |
| 82 | vim9script |
| 83 | let script_var = 123 |
| 84 | def SomeFunc() |
| 85 | let func_var = script_var |
| 86 | if cond |
| 87 | let block_var = func_var |
| 88 | ... |
| 89 | |
| 90 | The variables are only visible in the block where they are defined and nested |
| 91 | blocks. Once the block ends the variable is no longer accessible: > |
| 92 | if cond |
| 93 | let inner = 5 |
| 94 | else |
| 95 | let inner = 0 |
| 96 | endif |
| 97 | echo inner " Error! |
| 98 | |
| 99 | The declaration must be done earlier: > |
| 100 | let inner: number |
| 101 | if cond |
| 102 | inner = 5 |
| 103 | else |
| 104 | inner = 0 |
| 105 | endif |
| 106 | echo inner |
| 107 | |
| 108 | To intentionally use a variable that won't be available later, a block can be |
| 109 | used: > |
| 110 | { |
| 111 | let temp = 'temp' |
| 112 | ... |
| 113 | } |
| 114 | echo temp " Error! |
| 115 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 116 | An existing variable cannot be assigned to with `:let`, since that implies a |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 117 | declaration. An exception is global variables: these can be both used with |
| 118 | and without `:let`, because there is no rule about where they are declared. |
| 119 | |
| 120 | Variables cannot shadow previously defined variables. |
| 121 | Variables may shadow Ex commands, rename the variable if needed. |
| 122 | |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 123 | Global variables must be prefixed with "g:", also at the script level. |
| 124 | However, global user defined functions are used without "g:". > |
| 125 | vim9script |
| 126 | let script_local = 'text' |
| 127 | let g:global = 'value' |
| 128 | let Funcref = ThatFunction |
| 129 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 130 | Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be |
| 131 | used to repeat a `:substitute` command. |
| 132 | |
| 133 | |
| 134 | Omitting :call and :eval ~ |
| 135 | |
| 136 | Functions can be called without `:call`: > |
| 137 | writefile(lines, 'file') |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 138 | Using `:call` is still possible, but this is discouraged. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 139 | |
| 140 | A method call without `eval` is possible, so long as the start is an |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 141 | identifier or can't be an Ex command. It does NOT work for string constants: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | myList->add(123) " works |
| 143 | g:myList->add(123) " works |
| 144 | [1, 2, 3]->Process() " works |
| 145 | #{a: 1, b: 2}->Process() " works |
| 146 | {'a': 1, 'b': 2}->Process() " works |
| 147 | "foobar"->Process() " does NOT work |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 148 | ("foobar")->Process() " works |
| 149 | 'foobar'->Process() " does NOT work |
| 150 | ('foobar')->Process() " works |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 152 | In case there is ambiguity between a function name and an Ex command, use ":" |
| 153 | to make clear you want to use the Ex command. For example, there is both the |
| 154 | `:substitute` command and the `substitute()` function. When the line starts |
| 155 | with `substitute(` this will use the function, prepend a colon to use the |
| 156 | command instead: > |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 157 | :substitute(pattern (replacement ( |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 158 | |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 159 | Note that while variables need to be defined before they can be used, |
| 160 | functions can be called before being defined. This is required to be able |
| 161 | have cyclic dependencies between functions. It is slightly less efficient, |
| 162 | since the function has to be looked up by name. And a typo in the function |
| 163 | name will only be found when the call is executed. |
| 164 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 165 | |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 166 | Omitting function() ~ |
| 167 | |
| 168 | A user defined function can be used as a function reference in an expression |
| 169 | without `function()`. The argument types and return type will then be checked. |
| 170 | The function must already have been defined. > |
| 171 | |
| 172 | let Funcref = MyFunction |
| 173 | |
| 174 | When using `function()` the resulting type is "func", a function with any |
| 175 | number of arguments and any return type. The function can be defined later. |
| 176 | |
| 177 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 178 | Automatic line continuation ~ |
| 179 | |
| 180 | In many cases it is obvious that an expression continues on the next line. In |
| 181 | those cases there is no need to prefix the line with a backslash. For |
| 182 | example, when a list spans multiple lines: > |
| 183 | let mylist = [ |
| 184 | 'one', |
| 185 | 'two', |
| 186 | ] |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 187 | And when a dict spans multiple lines: > |
| 188 | let mydict = #{ |
| 189 | one: 1, |
| 190 | two: 2, |
| 191 | } |
| 192 | Function call: > |
| 193 | let result = Func( |
| 194 | arg1, |
| 195 | arg2 |
| 196 | ) |
| 197 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame^] | 198 | For binary operators iin expressions not in [], {} or () a line break is |
| 199 | possible AFTER the operators. For example: > |
| 200 | let text = lead .. |
| 201 | middle .. |
| 202 | end |
| 203 | let total = start + |
| 204 | end - |
| 205 | correction |
| 206 | let result = positive ? |
| 207 | PosFunc(arg) : |
| 208 | NegFunc(arg) |
| 209 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 210 | Note that "enddef" cannot be used at the start of a continuation line, it ends |
| 211 | the current function. |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 212 | |
| 213 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 214 | No curly braces expansion ~ |
| 215 | |
| 216 | |curly-braces-names| cannot be used. |
| 217 | |
| 218 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 219 | No :append, :change or :insert ~ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 220 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 221 | These commands are too quickly confused with local variable names. |
| 222 | |
| 223 | |
| 224 | Comparators ~ |
| 225 | |
| 226 | The 'ignorecase' option is not used for comparators that use strings. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 227 | |
| 228 | |
| 229 | White space ~ |
| 230 | |
| 231 | Vim9 script enforces proper use of white space. This is no longer allowed: > |
| 232 | let var=234 " Error! |
| 233 | let var= 234 " Error! |
| 234 | let var =234 " Error! |
| 235 | There must be white space before and after the "=": > |
| 236 | let var = 234 " OK |
| 237 | |
| 238 | White space is required around most operators. |
| 239 | |
| 240 | White space is not allowed: |
| 241 | - Between a function name and the "(": > |
| 242 | call Func (arg) " Error! |
| 243 | call Func |
| 244 | \ (arg) " Error! |
| 245 | call Func(arg) " OK |
| 246 | call Func( |
| 247 | \ arg) " OK |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 248 | call Func( |
| 249 | \ arg " OK |
| 250 | \ ) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 251 | |
| 252 | |
| 253 | Conditions and expressions ~ |
| 254 | |
| 255 | Conditions and expression are mostly working like they do in JavaScript. A |
| 256 | difference is made where JavaScript does not work like most people expect. |
| 257 | Specifically, an empty list is falsey. |
| 258 | |
| 259 | Any type of variable can be used as a condition, there is no error, not even |
| 260 | for using a list or job. This is very much like JavaScript, but there are a |
| 261 | few exceptions. |
| 262 | |
| 263 | type TRUE when ~ |
| 264 | bool v:true |
| 265 | number non-zero |
| 266 | float non-zero |
| 267 | string non-empty |
| 268 | blob non-empty |
| 269 | list non-empty (different from JavaScript) |
| 270 | dictionary non-empty (different from JavaScript) |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 271 | func when there is a function name |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 272 | special v:true |
| 273 | job when not NULL |
| 274 | channel when not NULL |
| 275 | class when not NULL |
| 276 | object when not NULL (TODO: when isTrue() returns v:true) |
| 277 | |
| 278 | The boolean operators "||" and "&&" do not change the value: > |
| 279 | 8 || 2 == 8 |
| 280 | 0 || 2 == 2 |
| 281 | 0 || '' == '' |
| 282 | 8 && 2 == 2 |
| 283 | 0 && 2 == 0 |
| 284 | [] && 2 == [] |
| 285 | |
| 286 | When using `..` for string concatenation the arguments are always converted to |
| 287 | string. > |
| 288 | 'hello ' .. 123 == 'hello 123' |
| 289 | 'hello ' .. v:true == 'hello true' |
| 290 | |
| 291 | In Vim9 script one can use "true" for v:true and "false" for v:false. |
| 292 | |
| 293 | |
| 294 | ============================================================================== |
| 295 | |
| 296 | 3. New style functions *fast-functions* |
| 297 | |
| 298 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 299 | |
| 300 | *:def* |
| 301 | :def[!] {name}([arguments])[: {return-type} |
| 302 | Define a new function by the name {name}. The body of |
| 303 | the function follows in the next lines, until the |
| 304 | matching `:enddef`. |
| 305 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 306 | When {return-type} is omitted or is "void" the |
| 307 | function is not expected to return anything. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 308 | |
| 309 | {arguments} is a sequence of zero or more argument |
| 310 | declarations. There are three forms: |
| 311 | {name}: {type} |
| 312 | {name} = {value} |
| 313 | {name}: {type} = {value} |
| 314 | The first form is a mandatory argument, the caller |
| 315 | must always provide them. |
| 316 | The second and third form are optional arguments. |
| 317 | When the caller omits an argument the {value} is used. |
| 318 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 319 | NOTE: It is possible to nest `:def` inside another |
| 320 | `:def`, but it is not possible to nest `:def` inside |
| 321 | `:function`, for backwards compatibility. |
| 322 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 323 | [!] is used as with `:function`. |
| 324 | |
| 325 | *:enddef* |
| 326 | :enddef End of a function defined with `:def`. |
| 327 | |
| 328 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 329 | If the script the function is defined in is Vim9 script, then script-local |
| 330 | variables can be accessed without the "s:" prefix. They must be defined |
| 331 | before the function. If the script the function is defined in is legacy |
| 332 | script, then script-local variables must be accessed with the "s:" prefix. |
| 333 | |
| 334 | |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 335 | *:disa* *:disassemble* |
| 336 | :disa[ssemble] {func} Show the instructions generated for {func}. |
| 337 | This is for debugging and testing. |
Bram Moolenaar | cc390ff | 2020-02-29 22:06:30 +0100 | [diff] [blame] | 338 | Note that for command line completion of {func} you |
| 339 | can prepend "s:" to find script-local functions. |
Bram Moolenaar | ebdf3c9 | 2020-02-15 21:41:42 +0100 | [diff] [blame] | 340 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 341 | ============================================================================== |
| 342 | |
| 343 | 4. Types *vim9-types* |
| 344 | |
| 345 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 346 | |
| 347 | The following builtin types are supported: |
| 348 | bool |
| 349 | number |
| 350 | float |
| 351 | string |
| 352 | blob |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 353 | list<{type}> |
| 354 | dict<{type}> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 355 | job |
| 356 | channel |
Bram Moolenaar | b17893a | 2020-03-14 08:19:51 +0100 | [diff] [blame] | 357 | func |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 358 | func: {type} |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 359 | func({type}, ...) |
| 360 | func({type}, ...): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 361 | |
| 362 | Not supported yet: |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 363 | tuple<a: {type}, b: {type}, ...> |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 364 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 365 | These types can be used in declarations, but no value will have this type: |
| 366 | {type}|{type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 367 | void |
| 368 | any |
| 369 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 370 | There is no array type, use list<{type}> instead. For a list constant an |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 371 | efficient implementation is used that avoids allocating lot of small pieces of |
| 372 | memory. |
| 373 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 374 | A partial and function can be declared in more or less specific ways: |
| 375 | func any kind of function reference, no type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 376 | checking for arguments or return value |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 377 | func: {type} any number and type of arguments with specific |
| 378 | return type |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 379 | func({type}) function with argument type, does not return |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 380 | a value |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 381 | func({type}): {type} function with argument type and return type |
| 382 | func(?{type}) function with type of optional argument, does |
| 383 | not return a value |
| 384 | func(...{type}) function with type of variable number of |
| 385 | arguments, does not return a value |
| 386 | func({type}, ?{type}, ...{type}): {type} |
| 387 | function with: |
| 388 | - type of mandatory argument |
| 389 | - type of optional argument |
| 390 | - type of variable number of arguments |
| 391 | - return type |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 392 | |
| 393 | If the return type is "void" the function does not return a value. |
| 394 | |
| 395 | The reference can also be a |Partial|, in which case it stores extra arguments |
| 396 | and/or a dictionary, which are not visible to the caller. Since they are |
| 397 | called in the same way the declaration is the same. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 398 | |
| 399 | Custom types can be defined with `:type`: > |
| 400 | :type MyList list<string> |
| 401 | {not implemented yet} |
| 402 | |
| 403 | And classes and interfaces can be used as types: > |
| 404 | :class MyClass |
| 405 | :let mine: MyClass |
| 406 | |
| 407 | :interface MyInterface |
| 408 | :let mine: MyInterface |
| 409 | |
| 410 | :class MyTemplate<Targ> |
| 411 | :let mine: MyTemplate<number> |
| 412 | :let mine: MyTemplate<string> |
| 413 | |
| 414 | :class MyInterface<Targ> |
| 415 | :let mine: MyInterface<number> |
| 416 | :let mine: MyInterface<string> |
| 417 | {not implemented yet} |
| 418 | |
| 419 | |
| 420 | Type inference *type-inference* |
| 421 | |
| 422 | In general: Whenever the type is clear it can be omitted. For example, when |
| 423 | declaring a variable and giving it a value: > |
| 424 | let var = 0 " infers number type |
| 425 | let var = 'hello' " infers string type |
| 426 | |
| 427 | |
| 428 | ============================================================================== |
| 429 | |
| 430 | 5. Namespace, Import and Export |
| 431 | *vim9script* *vim9-export* *vim9-import* |
| 432 | |
| 433 | THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE |
| 434 | |
| 435 | A Vim9 script can be written to be imported. This means that everything in |
| 436 | the script is local, unless exported. Those exported items, and only those |
| 437 | items, can then be imported in another script. |
| 438 | |
| 439 | |
| 440 | Namespace ~ |
| 441 | *:vim9script* *:vim9* |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 442 | To recognize a file that can be imported the `vim9script` statement must |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 443 | appear as the first statement in the file. It tells Vim to interpret the |
| 444 | script in its own namespace, instead of the global namespace. If a file |
| 445 | starts with: > |
| 446 | vim9script |
| 447 | let myvar = 'yes' |
| 448 | Then "myvar" will only exist in this file. While without `vim9script` it would |
| 449 | be available as `g:myvar` from any other script and function. |
| 450 | |
| 451 | The variables at the file level are very much like the script-local "s:" |
| 452 | variables in legacy Vim script, but the "s:" is omitted. |
| 453 | |
| 454 | In Vim9 script the global "g:" namespace can still be used as before. |
| 455 | |
| 456 | A side effect of `:vim9script` is that the 'cpoptions' option is set to the |
| 457 | Vim default value, like with: > |
| 458 | :set cpo&vim |
| 459 | One of the effects is that |line-continuation| is always enabled. |
| 460 | The original value of 'cpoptions' is restored at the end of the script. |
| 461 | |
| 462 | |
| 463 | Export ~ |
| 464 | *:export* *:exp* |
| 465 | Exporting one item can be written as: > |
| 466 | export const EXPORTED_CONST = 1234 |
| 467 | export let someValue = ... |
| 468 | export def MyFunc() ... |
| 469 | export class MyClass ... |
| 470 | |
| 471 | As this suggests, only constants, variables, `:def` functions and classes can |
| 472 | be exported. |
| 473 | |
| 474 | Alternatively, an export statement can be used to export several already |
| 475 | defined (otherwise script-local) items: > |
| 476 | export {EXPORTED_CONST, someValue, MyFunc, MyClass} |
| 477 | |
| 478 | |
| 479 | Import ~ |
| 480 | *:import* *:imp* |
| 481 | The exported items can be imported individually in another Vim9 script: > |
| 482 | import EXPORTED_CONST from "thatscript.vim" |
| 483 | import MyClass from "myclass.vim" |
| 484 | |
| 485 | To import multiple items at the same time: > |
| 486 | import {someValue, MyClass} from "thatscript.vim" |
| 487 | |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 488 | In case the name is ambiguous, another name can be specified: > |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 489 | import MyClass as ThatClass from "myclass.vim" |
| 490 | import {someValue, MyClass as ThatClass} from "myclass.vim" |
| 491 | |
| 492 | To import all exported items under a specific identifier: > |
| 493 | import * as That from 'thatscript.vim' |
| 494 | |
| 495 | Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free |
| 496 | to choose the name "That", but it is highly recommended to use the name of the |
| 497 | script file to avoid confusion. |
| 498 | |
| 499 | The script name after `import` can be: |
| 500 | - A relative path, starting "." or "..". This finds a file relative to the |
| 501 | location of the script file itself. This is useful to split up a large |
| 502 | plugin into several files. |
| 503 | - An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This |
| 504 | will be rarely used. |
| 505 | - A path not being relative or absolute. This will be found in the |
| 506 | "import" subdirectories of 'runtimepath' entries. The name will usually be |
| 507 | longer and unique, to avoid loading the wrong file. |
| 508 | |
| 509 | Once a vim9 script file has been imported, the result is cached and used the |
| 510 | next time the same script is imported. It will not be read again. |
| 511 | *:import-cycle* |
| 512 | The `import` commands are executed when encountered. If that script (directly |
| 513 | or indirectly) imports the current script, then items defined after the |
| 514 | `import` won't be processed yet. Therefore cyclic imports can exist, but may |
| 515 | result in undefined items. |
| 516 | |
| 517 | |
| 518 | Import in an autoload script ~ |
| 519 | |
| 520 | For optimal startup speed, loading scripts should be postponed until they are |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 521 | actually needed. A recommended mechanism: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 522 | |
| 523 | 1. In the plugin define user commands, functions and/or mappings that refer to |
| 524 | an autoload script. > |
| 525 | command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>) |
| 526 | |
| 527 | < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. |
| 528 | |
| 529 | 2. In the autocommand script do the actual work. You can import items from |
| 530 | other files to split up functionality in appropriate pieces. > |
| 531 | vim9script |
| 532 | import FilterFunc from "../import/someother.vim" |
| 533 | def searchfor#Stuff(arg: string) |
| 534 | let filtered = FilterFunc(arg) |
| 535 | ... |
| 536 | < This goes in .../autoload/searchfor.vim. "searchfor" in the file name |
| 537 | must be exactly the same as the prefix for the function name, that is how |
| 538 | Vim finds the file. |
| 539 | |
| 540 | 3. Other functionality, possibly shared between plugins, contains the exported |
| 541 | items and any private items. > |
| 542 | vim9script |
| 543 | let localVar = 'local' |
| 544 | export def FilterFunc(arg: string): string |
| 545 | ... |
| 546 | < This goes in .../import/someother.vim. |
| 547 | |
| 548 | |
| 549 | Import in legacy Vim script ~ |
| 550 | |
| 551 | If an `import` statement is used in legacy Vim script, for identifier the |
| 552 | script-local "s:" namespace will be used, even when "s:" is not specified. |
| 553 | |
| 554 | |
| 555 | ============================================================================== |
| 556 | |
| 557 | 9. Rationale *vim9-rationale* |
| 558 | |
| 559 | The :def command ~ |
| 560 | |
| 561 | Plugin writers have asked for a much faster Vim script. Investigation have |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 562 | shown that keeping the existing semantics of function calls make this close to |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 563 | impossible, because of the overhead involved with calling a function, setting |
| 564 | up the local function scope and executing lines. There are many details that |
| 565 | need to be handled, such as error messages and exceptions. The need to create |
| 566 | a dictionary for a: and l: scopes, the a:000 list and several others add too |
| 567 | much overhead that cannot be avoided. |
| 568 | |
| 569 | Therefore the `:def` method to define a new-style function had to be added, |
| 570 | which allows for a function with different semantics. Most things still work |
| 571 | as before, but some parts do not. A new way to define a function was |
| 572 | considered the best way to separate the old-style code from Vim9 script code. |
| 573 | |
| 574 | Using "def" to define a function comes from Python. Other languages use |
| 575 | "function" which clashes with legacy Vim script. |
| 576 | |
| 577 | |
| 578 | Type checking ~ |
| 579 | |
| 580 | When compiling lines of Vim commands into instructions as much as possible |
| 581 | should be done at compile time. Postponing it to runtime makes the execution |
| 582 | slower and means mistakes are found only later. For example, when |
| 583 | encountering the "+" character and compiling this into a generic add |
| 584 | instruction, at execution time the instruction would have to inspect the type |
| 585 | of the arguments and decide what kind of addition to do. And when the |
| 586 | type is dictionary throw an error. If the types are known to be numbers then |
| 587 | an "add number" instruction can be used, which is faster. The error can be |
| 588 | given at compile time, no error handling is needed at runtime. |
| 589 | |
| 590 | The syntax for types is similar to Java, since it is easy to understand and |
| 591 | widely used. The type names are what was used in Vim before, with some |
| 592 | additions such as "void" and "bool". |
| 593 | |
| 594 | |
| 595 | JavaScript/TypeScript syntax and semantics ~ |
| 596 | |
| 597 | Script writers have complained that the Vim script syntax is unexpectedly |
| 598 | different from what they are used to. To reduce this complaint popular |
| 599 | languages will be used as an example. At the same time, we do not want to |
Bram Moolenaar | 560979e | 2020-02-04 22:53:05 +0100 | [diff] [blame] | 600 | abandon the well-known parts of legacy Vim script. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 601 | |
| 602 | Since Vim already uses `:let` and `:const` and optional type checking is |
| 603 | desirable, the JavaScript/TypeScript syntax fits best for variable |
| 604 | declarations. > |
| 605 | const greeting = 'hello' " string type is inferred |
| 606 | let name: string |
| 607 | ... |
| 608 | name = 'John' |
| 609 | |
| 610 | Expression evaluation was already close to what JavaScript and other languages |
| 611 | are doing. Some details are unexpected and can be fixed. For example how the |
| 612 | || and && operators work. Legacy Vim script: > |
| 613 | let result = 44 |
| 614 | ... |
| 615 | return result || 0 " returns 1 |
| 616 | |
| 617 | Vim9 script works like JavaScript, keep the value: > |
| 618 | let result = 44 |
| 619 | ... |
| 620 | return result || 0 " returns 44 |
| 621 | |
| 622 | On the other hand, overloading "+" to use both for addition and string |
| 623 | concatenation goes against legacy Vim script and often leads to mistakes. |
| 624 | For that reason we will keep using ".." for string concatenation. Lua also |
| 625 | uses ".." this way. |
| 626 | |
| 627 | |
| 628 | Import and Export ~ |
| 629 | |
| 630 | A problem of legacy Vim script is that by default all functions and variables |
| 631 | are global. It is possible to make them script-local, but then they are not |
| 632 | available in other scripts. |
| 633 | |
| 634 | In Vim9 script a mechanism very similar to the Javascript import and export |
| 635 | mechanism is supported. It is a variant to the existing `:source` command |
| 636 | that works like one would expect: |
| 637 | - Instead of making everything global by default, everything is script-local, |
| 638 | unless exported. |
| 639 | - When importing a script the symbols that are imported are listed, avoiding |
| 640 | name conflicts and failures if later functionality is added. |
| 641 | - The mechanism allows for writing a big, long script with a very clear API: |
| 642 | the exported function(s) and class(es). |
| 643 | - By using relative paths loading can be much faster for an import inside of a |
| 644 | package, no need to search many directories. |
| 645 | - Once an import has been used, it can be cached and loading it again can be |
| 646 | avoided. |
| 647 | - The Vim-specific use of "s:" to make things script-local can be dropped. |
| 648 | |
| 649 | |
| 650 | Classes ~ |
| 651 | |
| 652 | Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But |
| 653 | these have never become widespread. When Vim 9 was designed a decision was |
| 654 | made to phase out these interfaces and concentrate on Vim script, while |
| 655 | encouraging plugin authors to write code in any language and run it as an |
| 656 | external tool, using jobs and channels. |
| 657 | |
| 658 | Still, using an external tool has disadvantages. An alternative is to convert |
| 659 | the tool into Vim script. For that to be possible without too much |
| 660 | translation, and keeping the code fast at the same time, the constructs of the |
| 661 | tool need to be supported. Since most languages support classes the lack of |
| 662 | class support in Vim is then a problem. |
| 663 | |
| 664 | Previously Vim supported a kind-of object oriented programming by adding |
| 665 | methods to a dictionary. With some care this could be made to work, but it |
| 666 | does not look like real classes. On top of that, it's very slow, because of |
| 667 | the use of dictionaries. |
| 668 | |
| 669 | The support of classes in Vim9 script is a "minimal common functionality" of |
| 670 | class support in most languages. It works mostly like Java, which is the most |
| 671 | popular programming language. |
| 672 | |
| 673 | |
| 674 | |
| 675 | vim:tw=78:ts=8:noet:ft=help:norl: |