blob: fa7febc4c382e14b3112f65cc707eaab2451039f [file] [log] [blame]
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02001*vim9.txt* For Vim version 8.2. Last change: 2020 Aug 15
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
8
Bram Moolenaar7ceefb32020-05-01 16:07:38 +02009Vim9 script commands and expressions. *vim9*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010010
11Most expression help is in |eval.txt|. This file is about the new syntax and
12features in Vim9 script.
13
14THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
15
16
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200171. What is Vim9 script? |vim9-script|
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182. Differences |vim9-differences|
193. New style functions |fast-functions|
204. Types |vim9-types|
215. Namespace, Import and Export |vim9script|
22
239. Rationale |vim9-rationale|
24
25==============================================================================
26
271. What is Vim9 script? *vim9-script*
28
29THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
30
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020031Vim script has been growing over time, while preserving backwards
32compatibility. That means bad choices from the past often can't be changed
Bram Moolenaar73fef332020-06-21 22:12:03 +020033and compatibility with Vi restricts possible solutions. Execution is quite
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020034slow, each line is parsed every time it is executed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010035
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020036The main goal of Vim9 script is to drastically improve performance. This is
37accomplished by compiling commands into instructions that can be efficiently
38executed. An increase in execution speed of 10 to 100 times can be expected.
39
40A secondary goal is to avoid Vim-specific constructs and get closer to
41commonly used programming languages, such as JavaScript, TypeScript and Java.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042
43The performance improvements can only be achieved by not being 100% backwards
Bram Moolenaar388a5d42020-05-26 21:20:45 +020044compatible. For example, making function arguments available in the
45"a:" dictionary adds quite a lot of overhead. In a Vim9 function this
46dictionary is not available. Other differences are more subtle, such as how
47errors are handled.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010048
49The Vim9 script syntax and semantics are used in:
50- a function defined with the `:def` command
51- a script file where the first command is `vim9script`
52
53When using `:function` in a Vim9 script file the legacy syntax is used.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020054However, this can be confusing and is therefore discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010055
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020056Vim9 script and legacy Vim script can be mixed. There is no requirement to
57rewrite old scripts, they keep working as before.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058
59==============================================================================
60
612. Differences from legacy Vim script *vim9-differences*
62
63THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
64
Bram Moolenaar2c330432020-04-13 14:41:35 +020065Comments starting with # ~
66
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020067In legacy Vim script comments start with double quote. In Vim9 script
68comments start with #. >
69 # declarations
Bram Moolenaar73fef332020-06-21 22:12:03 +020070 let count = 0 # number of occurrences
Bram Moolenaar2c330432020-04-13 14:41:35 +020071
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020072The reason is that a double quote can also be the start of a string. In many
Bram Moolenaar3d1cde82020-08-15 18:55:18 +020073places, especially halfway through an expression with a line break, it's hard
74to tell what the meaning is, since both a string and a comment can be followed
75by arbitrary text. To avoid confusion only # comments are recognized. This
76is the same as in shell scripts and Python programs.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020077
78In Vi # is a command to list text with numbers. In Vim9 script you can use
79`:number` for that. >
Bram Moolenaarae616492020-07-28 20:07:27 +020080 101 number
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +020081
82To improve readability there must be a space between a command and the #
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020083that starts a comment. Note that #{ is the start of a dictionary, therefore
Bram Moolenaarae616492020-07-28 20:07:27 +020084it does not start a comment.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020085
Bram Moolenaar2c330432020-04-13 14:41:35 +020086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087Vim9 functions ~
88
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020089A function defined with `:def` is compiled. Execution is many times faster,
90often 10x to 100x times.
91
Bram Moolenaar388a5d42020-05-26 21:20:45 +020092Many errors are already found when compiling, before the function is executed.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +020093The syntax is strict, to enforce code that is easy to read and understand.
94
Bram Moolenaar388a5d42020-05-26 21:20:45 +020095Compilation is done when the function is first called, or when the
96`:defcompile` command is encountered in the script where the function was
Bram Moolenaarae616492020-07-28 20:07:27 +020097defined. `:disassemble` also compiles the function.
Bram Moolenaar388a5d42020-05-26 21:20:45 +020098
99`:def` has no options like `:function` does: "range", "abort", "dict" or
100"closure". A `:def` function always aborts on an error, does not get a range
101passed and cannot be a "dict" function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100102
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200103The argument types and return type need to be specified. The "any" type can
104be used, type checking will then be done at runtime, like with legacy
105functions.
106
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200107Arguments are accessed by name, without "a:", just like any other language.
108There is no "a:" dictionary or "a:000" list.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200109
110Variable arguments are defined as the last argument, with a name and have a
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200111list type, similar to TypeScript. For example, a list of numbers: >
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200112 def MyFunc(...itemlist: list<number>)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 for item in itemlist
114 ...
115
116
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200117Functions and variables are script-local by default ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200118 *vim9-scopes*
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200119When using `:function` or `:def` to specify a new function at the script level
120in a Vim9 script, the function is local to the script, as if "s:" was
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200121prefixed. Using the "s:" prefix is optional. To define or use a global
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200122function or variable the "g:" prefix should be used. For functions in an
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200123autoload script the "name#" prefix is sufficient. >
124 def ThisFunction() # script-local
125 def s:ThisFunction() # script-local
126 def g:ThatFunction() # global
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200127 def ThatFunction() # global if no local ThatFunction()
Bram Moolenaarea2d8d22020-07-29 22:11:05 +0200128 def scriptname#function() # autoload
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200129
130When using `:function` or `:def` to specify a new function inside a function,
131the function is local to the function. It is not possible to define a
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200132script-local function inside a function. It is possible to define a global
133function, using the "g:" prefix.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200134
135When referring to a function and no "s:" or "g:" prefix is used, Vim will
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200136prefer using a local function (in the function scope, script scope or
137imported) before looking for a global function.
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200138In all cases the function must be defined before used. That is when it is
139first called or when `:defcompile` causes the call to be compiled.
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200140
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200141The result is that functions and variables without a namespace can usually be
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200142found in the script, either defined there or imported. Global functions and
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200143variables could be defined anywhere (good luck finding out where!).
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200144
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200145Global functions can still be defined and deleted at nearly any time. In
Bram Moolenaar2cfb4a22020-05-07 18:56:00 +0200146Vim9 script script-local functions are defined once when the script is sourced
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200147and cannot be deleted or replaced.
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200148
149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150Variable declarations with :let and :const ~
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200151 *vim9-declaration*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152Local variables need to be declared with `:let`. Local constants need to be
153declared with `:const`. We refer to both as "variables".
154
155Variables can be local to a script, function or code block: >
156 vim9script
157 let script_var = 123
158 def SomeFunc()
159 let func_var = script_var
160 if cond
161 let block_var = func_var
162 ...
163
164The variables are only visible in the block where they are defined and nested
165blocks. Once the block ends the variable is no longer accessible: >
166 if cond
167 let inner = 5
168 else
169 let inner = 0
170 endif
171 echo inner " Error!
172
173The declaration must be done earlier: >
174 let inner: number
175 if cond
176 inner = 5
177 else
178 inner = 0
179 endif
180 echo inner
181
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200182To intentionally avoid a variable being available later, a block can be used:
183>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184 {
185 let temp = 'temp'
186 ...
187 }
188 echo temp " Error!
189
Bram Moolenaar560979e2020-02-04 22:53:05 +0100190An existing variable cannot be assigned to with `:let`, since that implies a
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200191declaration. Global, window, tab, buffer and Vim variables can only be used
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200192without `:let`, because they are not really declared, they can also be deleted
193with `:unlet`.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100194
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200195Variables and functions cannot shadow previously defined or imported variables
196and functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197Variables may shadow Ex commands, rename the variable if needed.
198
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200199Global variables and user defined functions must be prefixed with "g:", also
200at the script level. >
Bram Moolenaard1caa942020-04-10 22:10:56 +0200201 vim9script
202 let script_local = 'text'
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200203 g:global = 'value'
Bram Moolenaar7ceefb32020-05-01 16:07:38 +0200204 let Funcref = g:ThatFunction
Bram Moolenaard1caa942020-04-10 22:10:56 +0200205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206Since "&opt = value" is now assigning a value to option "opt", ":&" cannot be
207used to repeat a `:substitute` command.
208
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200209 *E1092*
210Declaring more than one variable at a time, using the unpack notation, is
211currently not supported: >
212 let [v1, v2] = GetValues() # Error!
213That is because the type needs to be inferred from the list item type, which
214isn't that easy.
215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
217Omitting :call and :eval ~
218
219Functions can be called without `:call`: >
220 writefile(lines, 'file')
Bram Moolenaar560979e2020-02-04 22:53:05 +0100221Using `:call` is still possible, but this is discouraged.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100222
223A method call without `eval` is possible, so long as the start is an
Bram Moolenaarae616492020-07-28 20:07:27 +0200224identifier or can't be an Ex command. Examples: >
225 myList->add(123)
226 g:myList->add(123)
227 [1, 2, 3]->Process()
228 #{a: 1, b: 2}->Process()
229 {'a': 1, 'b': 2}->Process()
230 "foobar"->Process()
231 ("foobar")->Process()
232 'foobar'->Process()
233 ('foobar')->Process()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200235In the rare case there is ambiguity between a function name and an Ex command,
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200236prepend ":" to make clear you want to use the Ex command. For example, there
237is both the `:substitute` command and the `substitute()` function. When the
238line starts with `substitute(` this will use the function. Prepend a colon to
239use the command instead: >
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +0100240 :substitute(pattern (replacement (
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100241
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100242Note that while variables need to be defined before they can be used,
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200243functions can be called before being defined. This is required to allow
244for cyclic dependencies between functions. It is slightly less efficient,
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100245since the function has to be looked up by name. And a typo in the function
Bram Moolenaarae616492020-07-28 20:07:27 +0200246name will only be found when the function is called.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248
Bram Moolenaard1caa942020-04-10 22:10:56 +0200249Omitting function() ~
250
251A user defined function can be used as a function reference in an expression
252without `function()`. The argument types and return type will then be checked.
253The function must already have been defined. >
254
255 let Funcref = MyFunction
256
257When using `function()` the resulting type is "func", a function with any
258number of arguments and any return type. The function can be defined later.
259
260
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200261Automatic line continuation ~
262
263In many cases it is obvious that an expression continues on the next line. In
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200264those cases there is no need to prefix the line with a backslash
265|line-continuation|. For example, when a list spans multiple lines: >
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200266 let mylist = [
267 'one',
268 'two',
269 ]
Bram Moolenaare6085c52020-04-12 20:19:16 +0200270And when a dict spans multiple lines: >
271 let mydict = #{
272 one: 1,
273 two: 2,
274 }
275Function call: >
276 let result = Func(
277 arg1,
278 arg2
279 )
280
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200281For binary operators in expressions not in [], {} or () a line break is
282possible just before or after the operator. For example: >
283 let text = lead
284 .. middle
285 .. end
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200286 let total = start +
287 end -
288 correction
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200289 let result = positive
290 ? PosFunc(arg)
291 : NegFunc(arg)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200292
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200293For a method call using "->" and a member using a dot, a line break is allowed
294before it: >
Bram Moolenaar73fef332020-06-21 22:12:03 +0200295 let result = GetBuilder()
296 ->BuilderSetWidth(333)
297 ->BuilderSetHeight(777)
298 ->BuilderBuild()
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200299 let result = MyDict
300 .member
Bram Moolenaar73fef332020-06-21 22:12:03 +0200301
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200302< *E1050*
303To make it possible for the operator at the start of the line to be
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200304recognized, it is required to put a colon before a range. This will add
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200305"start" and print: >
306 let result = start
307 + print
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200308Like this: >
309 let result = start + print
310
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200311This will assign "start" and print a line: >
312 let result = start
313 :+ print
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200314
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200315It is also possible to split a function header over multiple lines, in between
316arguments: >
317 def MyFunc(
318 text: string,
319 separator = '-'
320 ): string
321
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200322Notes:
323- "enddef" cannot be used at the start of a continuation line, it ends the
324 current function.
325- No line break is allowed in the LHS of an assignment. Specifically when
326 unpacking a list |:let-unpack|. This is OK: >
327 [var1, var2] =
328 Func()
329< This does not work: >
330 [var1,
331 var2] =
332 Func()
333- No line break is allowed in between arguments of an `:echo`, `:execute` and
334 similar commands. This is OK: >
335 echo [1,
336 2] [3,
337 4]
338< This does not work: >
339 echo [1, 2]
340 [3, 4]
341- No line break is allowed in the arguments of a lambda, between the "{" and
342 "->". This is OK: >
343 filter(list, {k, v ->
344 v > 0})
345< This does not work: >
346 filter(list, {k,
347 v -> v > 0})
Bram Moolenaardf069ee2020-06-22 23:02:51 +0200348
Bram Moolenaar4fdae992020-04-12 16:38:57 +0200349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100350No curly braces expansion ~
351
352|curly-braces-names| cannot be used.
353
354
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200355No :xit, :t, :append, :change or :insert ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356
Bram Moolenaarf5a48012020-08-01 17:00:03 +0200357These commands are too easily confused with local variable names.
358Instead of `:x` or `:xit` you can use `:exit`.
359Instead of `:t` you can use `:copy`.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100360
361
362Comparators ~
363
364The 'ignorecase' option is not used for comparators that use strings.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365
366
367White space ~
368
369Vim9 script enforces proper use of white space. This is no longer allowed: >
370 let var=234 " Error!
371 let var= 234 " Error!
372 let var =234 " Error!
373There must be white space before and after the "=": >
374 let var = 234 " OK
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200375White space must also be put before the # that starts a comment after a
376command: >
Bram Moolenaar2c330432020-04-13 14:41:35 +0200377 let var = 234# Error!
378 let var = 234 # OK
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379
380White space is required around most operators.
381
382White space is not allowed:
383- Between a function name and the "(": >
384 call Func (arg) " Error!
385 call Func
386 \ (arg) " Error!
387 call Func(arg) " OK
388 call Func(
389 \ arg) " OK
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100390 call Func(
391 \ arg " OK
392 \ )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393
394
395Conditions and expressions ~
396
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200397Conditions and expressions are mostly working like they do in JavaScript. A
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398difference is made where JavaScript does not work like most people expect.
399Specifically, an empty list is falsey.
400
401Any type of variable can be used as a condition, there is no error, not even
402for using a list or job. This is very much like JavaScript, but there are a
403few exceptions.
404
405 type TRUE when ~
406 bool v:true
407 number non-zero
408 float non-zero
409 string non-empty
410 blob non-empty
411 list non-empty (different from JavaScript)
412 dictionary non-empty (different from JavaScript)
Bram Moolenaard1caa942020-04-10 22:10:56 +0200413 func when there is a function name
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 special v:true
415 job when not NULL
416 channel when not NULL
417 class when not NULL
418 object when not NULL (TODO: when isTrue() returns v:true)
419
420The boolean operators "||" and "&&" do not change the value: >
421 8 || 2 == 8
422 0 || 2 == 2
423 0 || '' == ''
424 8 && 2 == 2
425 0 && 2 == 0
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200426 2 && 0 == 0
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100427 [] && 2 == []
428
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200429When using `..` for string concatenation arguments of simple types are always
430converted to string. >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431 'hello ' .. 123 == 'hello 123'
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200432 'hello ' .. v:true == 'hello v:true'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200434Simple types are string, float, special and bool. For other types |string()|
435can be used.
436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437In Vim9 script one can use "true" for v:true and "false" for v:false.
438
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200439Indexing a string with [idx] or [idx, idx] uses character indexes instead of
440byte indexes. Example: >
441 echo 'bár'[1]
442In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
443script this results in the string 'á'.
444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100445
Bram Moolenaare46a4402020-06-30 20:38:27 +0200446What to watch out for ~
447 *vim9-gotchas*
448Vim9 was designed to be closer to often used programming languages, but at the
449same time tries to support the legacy Vim commands. Some compromises had to
450be made. Here is a summary of what might be unexpected.
451
452Ex command ranges need to be prefixed with a colon. >
453 -> " legacy Vim: shifts the previous line to the right
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200454 ->func() " Vim9: method call in continuation line
Bram Moolenaare46a4402020-06-30 20:38:27 +0200455 :-> " Vim9: shifts the previous line to the right
456
457 %s/a/b " legacy Vim: substitute on all lines
458 x = alongname
459 % another " Vim9: line continuation without a backslash
460 :%s/a/b " Vim9: substitute on all lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200461 'text'->func() " Vim9: method call
462 :'t " legacy Vim: jump to mark m
Bram Moolenaare46a4402020-06-30 20:38:27 +0200463
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200464Some Ex commands can be confused with assignments in Vim9 script: >
465 g:name = value # assignment
466 g:pattern:cmd # invalid command - ERROR
467 :g:pattern:cmd # :global command
468
Bram Moolenaare46a4402020-06-30 20:38:27 +0200469Functions defined with `:def` compile the whole function. Legacy functions
470can bail out, and the following lines are not parsed: >
471 func Maybe()
472 if !has('feature')
473 return
474 endif
475 use-feature
476 endfunc
477Vim9 functions are compiled as a whole: >
478 def Maybe()
479 if !has('feature')
480 return
481 endif
482 use-feature " May give compilation error
483 enddef
484For a workaround, split it in two functions: >
485 func Maybe()
486 if has('feature')
487 call MaybyInner()
488 endif
489 endfunc
490 if has('feature')
491 def MaybeInner()
492 use-feature
493 enddef
494 endif
495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496==============================================================================
497
4983. New style functions *fast-functions*
499
500THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
501
502 *:def*
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200503:def[!] {name}([arguments])[: {return-type}]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 Define a new function by the name {name}. The body of
505 the function follows in the next lines, until the
506 matching `:enddef`.
507
Bram Moolenaard77a8522020-04-03 21:59:57 +0200508 When {return-type} is omitted or is "void" the
509 function is not expected to return anything.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510
511 {arguments} is a sequence of zero or more argument
512 declarations. There are three forms:
513 {name}: {type}
514 {name} = {value}
515 {name}: {type} = {value}
516 The first form is a mandatory argument, the caller
517 must always provide them.
518 The second and third form are optional arguments.
519 When the caller omits an argument the {value} is used.
520
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200521 The function will be compiled into instructions when
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200522 called, or when `:disassemble` or `:defcompile` is
523 used. Syntax and type errors will be produced at that
524 time.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200525
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200526 It is possible to nest `:def` inside another `:def` or
527 `:function` up to about 50 levels deep.
Bram Moolenaar560979e2020-02-04 22:53:05 +0100528
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200529 [!] is used as with `:function`. Note that in Vim9
530 script script-local functions cannot be deleted or
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200531 redefined later in the same script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532
533 *:enddef*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200534:enddef End of a function defined with `:def`. It should be on
535 a line by its own.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536
537
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100538If the script the function is defined in is Vim9 script, then script-local
539variables can be accessed without the "s:" prefix. They must be defined
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200540before the function is compiled. If the script the function is defined in is
541legacy script, then script-local variables must be accessed with the "s:"
542prefix.
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100543
Bram Moolenaar388a5d42020-05-26 21:20:45 +0200544 *:defc* *:defcompile*
545:defc[ompile] Compile functions defined in the current script that
546 were not compiled yet.
547 This will report errors found during the compilation.
Bram Moolenaar5b1c8fe2020-02-21 18:42:43 +0100548
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100549 *:disa* *:disassemble*
550:disa[ssemble] {func} Show the instructions generated for {func}.
551 This is for debugging and testing.
Bram Moolenaarcc390ff2020-02-29 22:06:30 +0100552 Note that for command line completion of {func} you
553 can prepend "s:" to find script-local functions.
Bram Moolenaarebdf3c92020-02-15 21:41:42 +0100554
Bram Moolenaar7ff78462020-07-10 22:00:53 +0200555Limitations ~
556
557Local variables will not be visible to string evaluation. For example: >
558 def EvalString(): list<string>
559 let list = ['aa', 'bb', 'cc', 'dd']
560 return range(1, 2)->map('list[v:val]')
561 enddef
562
563The map argument is a string expression, which is evaluated without the
564function scope. Instead, use a lambda: >
565 def EvalString(): list<string>
566 let list = ['aa', 'bb', 'cc', 'dd']
567 return range(1, 2)->map({ _, v -> list[v] })
568 enddef
569
570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571==============================================================================
572
5734. Types *vim9-types*
574
575THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
576
577The following builtin types are supported:
578 bool
579 number
580 float
581 string
582 blob
Bram Moolenaard77a8522020-04-03 21:59:57 +0200583 list<{type}>
584 dict<{type}>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 job
586 channel
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100587 func
Bram Moolenaard1caa942020-04-10 22:10:56 +0200588 func: {type}
Bram Moolenaard77a8522020-04-03 21:59:57 +0200589 func({type}, ...)
590 func({type}, ...): {type}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591
592Not supported yet:
Bram Moolenaard77a8522020-04-03 21:59:57 +0200593 tuple<a: {type}, b: {type}, ...>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
Bram Moolenaard77a8522020-04-03 21:59:57 +0200595These types can be used in declarations, but no value will have this type:
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200596 {type}|{type} {not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 void
598 any
599
Bram Moolenaard77a8522020-04-03 21:59:57 +0200600There is no array type, use list<{type}> instead. For a list constant an
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601efficient implementation is used that avoids allocating lot of small pieces of
602memory.
603
Bram Moolenaard77a8522020-04-03 21:59:57 +0200604A partial and function can be declared in more or less specific ways:
605func any kind of function reference, no type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200606 checking for arguments or return value
Bram Moolenaard77a8522020-04-03 21:59:57 +0200607func: {type} any number and type of arguments with specific
608 return type
Bram Moolenaard1caa942020-04-10 22:10:56 +0200609func({type}) function with argument type, does not return
Bram Moolenaard77a8522020-04-03 21:59:57 +0200610 a value
Bram Moolenaard1caa942020-04-10 22:10:56 +0200611func({type}): {type} function with argument type and return type
612func(?{type}) function with type of optional argument, does
613 not return a value
614func(...{type}) function with type of variable number of
615 arguments, does not return a value
616func({type}, ?{type}, ...{type}): {type}
617 function with:
618 - type of mandatory argument
619 - type of optional argument
620 - type of variable number of arguments
621 - return type
Bram Moolenaard77a8522020-04-03 21:59:57 +0200622
623If the return type is "void" the function does not return a value.
624
625The reference can also be a |Partial|, in which case it stores extra arguments
626and/or a dictionary, which are not visible to the caller. Since they are
627called in the same way the declaration is the same.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628
629Custom types can be defined with `:type`: >
630 :type MyList list<string>
Bram Moolenaar127542b2020-08-09 17:22:04 +0200631Custom types must start with a capital letter, to avoid name clashes with
632builtin types added later, similarly to user functions.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633{not implemented yet}
634
635And classes and interfaces can be used as types: >
636 :class MyClass
637 :let mine: MyClass
638
639 :interface MyInterface
640 :let mine: MyInterface
641
642 :class MyTemplate<Targ>
643 :let mine: MyTemplate<number>
644 :let mine: MyTemplate<string>
645
646 :class MyInterface<Targ>
647 :let mine: MyInterface<number>
648 :let mine: MyInterface<string>
649{not implemented yet}
650
651
Bram Moolenaar64d662d2020-08-09 19:02:50 +0200652Variable types and type casting *variable-types*
653
654Variables declared in Vim9 script or in a `:def` function have a type, either
655specified explicitly or inferred from the initialization.
656
657Global, buffer, window and tab page variables do not have a specific type, the
658value can be changed at any time, possibly changing the type. Therefore, in
659compiled code the "any" type is assumed.
660
661This can be a problem when the "any" type is undesired and the actual type is
662expected to always be the same. For example, when declaring a list: >
663 let l: list<number> = [1, g:two]
664This will give an error, because "g:two" has type "any". To avoid this, use a
665type cast: >
666 let l: list<number> = [1, <number>g:two]
667< *type-casting*
668The compiled code will then check that "g:two" is a number at runtime and give
669an error if it isn't. This is called type casting.
670
671The syntax of a type cast is: "<" {type} ">". There cannot be white space
672after the "<" or before the ">" (to avoid them being confused with
673smaller-than and bigger-than operators).
674
675The semantics is that, if needed, a runtime type check is performed. The
676value is not actually changed. If you need to change the type, e.g. to change
677it to a string, use the |string()| function. Or use |str2nr()| to convert a
678string to a number.
679
680
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681Type inference *type-inference*
682
683In general: Whenever the type is clear it can be omitted. For example, when
684declaring a variable and giving it a value: >
685 let var = 0 " infers number type
686 let var = 'hello' " infers string type
687
Bram Moolenaar127542b2020-08-09 17:22:04 +0200688The type of a list and dictionary comes from the common type of the values.
689If the values all have the same type, that type is used for the list or
690dictionary. If there is a mix of types, the "any" type is used. >
691 [1, 2, 3] list<number>
692 ['a', 'b', 'c'] list<string>
693 [1, 'x', 3] list<any>
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694
695==============================================================================
696
6975. Namespace, Import and Export
698 *vim9script* *vim9-export* *vim9-import*
699
700THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
701
702A Vim9 script can be written to be imported. This means that everything in
703the script is local, unless exported. Those exported items, and only those
704items, can then be imported in another script.
705
706
707Namespace ~
708 *:vim9script* *:vim9*
Bram Moolenaar560979e2020-02-04 22:53:05 +0100709To recognize a file that can be imported the `vim9script` statement must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710appear as the first statement in the file. It tells Vim to interpret the
711script in its own namespace, instead of the global namespace. If a file
712starts with: >
713 vim9script
714 let myvar = 'yes'
715Then "myvar" will only exist in this file. While without `vim9script` it would
716be available as `g:myvar` from any other script and function.
717
718The variables at the file level are very much like the script-local "s:"
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200719variables in legacy Vim script, but the "s:" is omitted. And they cannot be
720deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +0200722In Vim9 script the global "g:" namespace can still be used as before. And the
723"w:", "b:" and "t:" namespaces. These have in common that variables are not
724declared and they can be deleted.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725
726A side effect of `:vim9script` is that the 'cpoptions' option is set to the
727Vim default value, like with: >
728 :set cpo&vim
729One of the effects is that |line-continuation| is always enabled.
730The original value of 'cpoptions' is restored at the end of the script.
731
732
733Export ~
734 *:export* *:exp*
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200735Exporting an item can be written as: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 export const EXPORTED_CONST = 1234
737 export let someValue = ...
738 export def MyFunc() ...
739 export class MyClass ...
740
741As this suggests, only constants, variables, `:def` functions and classes can
Bram Moolenaar2547aa92020-07-26 17:00:44 +0200742be exported. {classes are not implemented yet}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200744 *E1042*
745`:export` can only be used in Vim9 script, at the script level.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746
747
748Import ~
Bram Moolenaar73fef332020-06-21 22:12:03 +0200749 *:import* *:imp* *E1094*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750The exported items can be imported individually in another Vim9 script: >
751 import EXPORTED_CONST from "thatscript.vim"
752 import MyClass from "myclass.vim"
753
754To import multiple items at the same time: >
755 import {someValue, MyClass} from "thatscript.vim"
756
Bram Moolenaar560979e2020-02-04 22:53:05 +0100757In case the name is ambiguous, another name can be specified: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 import MyClass as ThatClass from "myclass.vim"
759 import {someValue, MyClass as ThatClass} from "myclass.vim"
760
761To import all exported items under a specific identifier: >
762 import * as That from 'thatscript.vim'
763
764Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free
765to choose the name "That", but it is highly recommended to use the name of the
766script file to avoid confusion.
767
768The script name after `import` can be:
769- A relative path, starting "." or "..". This finds a file relative to the
770 location of the script file itself. This is useful to split up a large
771 plugin into several files.
772- An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This
773 will be rarely used.
774- A path not being relative or absolute. This will be found in the
775 "import" subdirectories of 'runtimepath' entries. The name will usually be
776 longer and unique, to avoid loading the wrong file.
777
778Once a vim9 script file has been imported, the result is cached and used the
779next time the same script is imported. It will not be read again.
780 *:import-cycle*
781The `import` commands are executed when encountered. If that script (directly
782or indirectly) imports the current script, then items defined after the
783`import` won't be processed yet. Therefore cyclic imports can exist, but may
784result in undefined items.
785
786
787Import in an autoload script ~
788
789For optimal startup speed, loading scripts should be postponed until they are
Bram Moolenaar560979e2020-02-04 22:53:05 +0100790actually needed. A recommended mechanism:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791
7921. In the plugin define user commands, functions and/or mappings that refer to
793 an autoload script. >
794 command -nargs=1 SearchForStuff call searchfor#Stuff(<f-args>)
795
796< This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen.
797
Bram Moolenaar3d1cde82020-08-15 18:55:18 +02007982. In the autoload script do the actual work. You can import items from
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 other files to split up functionality in appropriate pieces. >
800 vim9script
801 import FilterFunc from "../import/someother.vim"
802 def searchfor#Stuff(arg: string)
803 let filtered = FilterFunc(arg)
804 ...
805< This goes in .../autoload/searchfor.vim. "searchfor" in the file name
806 must be exactly the same as the prefix for the function name, that is how
807 Vim finds the file.
808
8093. Other functionality, possibly shared between plugins, contains the exported
810 items and any private items. >
811 vim9script
812 let localVar = 'local'
813 export def FilterFunc(arg: string): string
814 ...
815< This goes in .../import/someother.vim.
816
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200817When compiling a `:def` function and a function in an autoload script is
818encountered, the script is not loaded until the `:def` function is called.
819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820
821Import in legacy Vim script ~
822
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200823If an `import` statement is used in legacy Vim script, the script-local "s:"
824namespace will be used for the imported item, even when "s:" is not specified.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825
826
827==============================================================================
828
8299. Rationale *vim9-rationale*
830
831The :def command ~
832
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200833Plugin writers have asked for a much faster Vim script. Investigations have
Bram Moolenaar560979e2020-02-04 22:53:05 +0100834shown that keeping the existing semantics of function calls make this close to
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835impossible, because of the overhead involved with calling a function, setting
836up the local function scope and executing lines. There are many details that
837need to be handled, such as error messages and exceptions. The need to create
838a dictionary for a: and l: scopes, the a:000 list and several others add too
839much overhead that cannot be avoided.
840
841Therefore the `:def` method to define a new-style function had to be added,
842which allows for a function with different semantics. Most things still work
843as before, but some parts do not. A new way to define a function was
844considered the best way to separate the old-style code from Vim9 script code.
845
846Using "def" to define a function comes from Python. Other languages use
847"function" which clashes with legacy Vim script.
848
849
850Type checking ~
851
852When compiling lines of Vim commands into instructions as much as possible
853should be done at compile time. Postponing it to runtime makes the execution
854slower and means mistakes are found only later. For example, when
855encountering the "+" character and compiling this into a generic add
856instruction, at execution time the instruction would have to inspect the type
857of the arguments and decide what kind of addition to do. And when the
858type is dictionary throw an error. If the types are known to be numbers then
859an "add number" instruction can be used, which is faster. The error can be
860given at compile time, no error handling is needed at runtime.
861
862The syntax for types is similar to Java, since it is easy to understand and
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200863widely used. The type names are what were used in Vim before, with some
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864additions such as "void" and "bool".
865
866
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200867Compiling functions early ~
868
869Functions are compiled when called or when `:defcompile` is used. Why not
870compile them early, so that syntax and type errors are reported early?
871
872The functions can't be compiled right away when encountered, because there may
873be forward references to functions defined later. Consider defining functions
874A, B and C, where A calls B, B calls C, and C calls A again. It's impossible
875to reorder the functions to avoid forward references.
876
877An alternative would be to first scan through the file to locate items and
Bram Moolenaar73fef332020-06-21 22:12:03 +0200878figure out their type, so that forward references are found, and only then
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200879execute the script and compile the functions. This means the script has to be
880parsed twice, which is slower, and some conditions at the script level, such
881as checking if a feature is supported, are hard to use. An attempt was made
882to see if it works, but it turned out to be impossible to make work nicely.
883
884It would be possible to compile all the functions at the end of the script.
885The drawback is that if a function never gets called, the overhead of
886compiling it counts anyway. Since startup speed is very important, in most
887cases it's better to do it later and accept that syntax and type errors are
888only reported then. In case these errors should be found early, e.g. when
889testing, the `:defcompile` command will help out.
890
891
892TypeScript syntax and semantics ~
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893
894Script writers have complained that the Vim script syntax is unexpectedly
895different from what they are used to. To reduce this complaint popular
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200896languages are used as an example. At the same time, we do not want to abandon
897the well-known parts of legacy Vim script.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
899Since Vim already uses `:let` and `:const` and optional type checking is
900desirable, the JavaScript/TypeScript syntax fits best for variable
901declarations. >
902 const greeting = 'hello' " string type is inferred
903 let name: string
904 ...
905 name = 'John'
906
907Expression evaluation was already close to what JavaScript and other languages
908are doing. Some details are unexpected and can be fixed. For example how the
909|| and && operators work. Legacy Vim script: >
910 let result = 44
911 ...
912 return result || 0 " returns 1
913
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200914Vim9 script works like JavaScript/TypeScript, keep the value: >
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 let result = 44
916 ...
917 return result || 0 " returns 44
918
919On the other hand, overloading "+" to use both for addition and string
920concatenation goes against legacy Vim script and often leads to mistakes.
921For that reason we will keep using ".." for string concatenation. Lua also
922uses ".." this way.
923
924
925Import and Export ~
926
927A problem of legacy Vim script is that by default all functions and variables
928are global. It is possible to make them script-local, but then they are not
929available in other scripts.
930
Bram Moolenaar3d1cde82020-08-15 18:55:18 +0200931In Vim9 script a mechanism very similar to the JavaScript import and export
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932mechanism is supported. It is a variant to the existing `:source` command
933that works like one would expect:
934- Instead of making everything global by default, everything is script-local,
935 unless exported.
936- When importing a script the symbols that are imported are listed, avoiding
937 name conflicts and failures if later functionality is added.
938- The mechanism allows for writing a big, long script with a very clear API:
939 the exported function(s) and class(es).
940- By using relative paths loading can be much faster for an import inside of a
941 package, no need to search many directories.
942- Once an import has been used, it can be cached and loading it again can be
943 avoided.
944- The Vim-specific use of "s:" to make things script-local can be dropped.
945
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200946When sourcing a Vim9 script from a legacy script, only the items defined
947globally can be used, not the exported items. Alternatives considered:
948- All the exported items become available as script-local items. This makes
949 it uncontrollable what items get defined.
950- Use the exported items and make them global. Disadvantage is that it's then
951 not possible to avoid name clashes in the global namespace.
952- Completely disallow sourcing a Vim9 script, require using `:import`. That
953 makes it difficult to use scripts for testing, or sourcing them from the
954 command line to try them out.
955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956
957Classes ~
958
959Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
960these have never become widespread. When Vim 9 was designed a decision was
961made to phase out these interfaces and concentrate on Vim script, while
962encouraging plugin authors to write code in any language and run it as an
963external tool, using jobs and channels.
964
965Still, using an external tool has disadvantages. An alternative is to convert
966the tool into Vim script. For that to be possible without too much
967translation, and keeping the code fast at the same time, the constructs of the
968tool need to be supported. Since most languages support classes the lack of
969class support in Vim is then a problem.
970
971Previously Vim supported a kind-of object oriented programming by adding
972methods to a dictionary. With some care this could be made to work, but it
973does not look like real classes. On top of that, it's very slow, because of
974the use of dictionaries.
975
976The support of classes in Vim9 script is a "minimal common functionality" of
977class support in most languages. It works mostly like Java, which is the most
978popular programming language.
979
980
981
982 vim:tw=78:ts=8:noet:ft=help:norl: