Vim 9
Vim 9
Most expression help is in |eval.txt|. This file is about the new syntax and
features in Vim9 script.
9. Rationale |vim9-rationale|
==============================================================================
Vim script has been growing over time, while preserving backwards
compatibility. That means bad choices from the past often can't be changed
and compatibility with Vi restricts possible solutions. Execution is quite
slow, each line is parsed every time it is executed.
The performance improvements can only be achieved by not being 100% backwards
compatible. For example, making function arguments available in the "a:"
dictionary adds quite a lot of overhead. In a Vim9 function this dictionary
is not available. Other differences are more subtle, such as how errors are
handled.
When using `:function` in a Vim9 script file the legacy syntax is used, with
the highest |scriptversion|. However, this can be confusing and is therefore
discouraged.
Vim9 script and legacy Vim script can be mixed. There is no requirement to
rewrite old scripts, they keep working as before. You may want to use a few
`:def` functions for code that needs to be fast.
==============================================================================
Overview ~
*E1146*
Brief summary of the differences you will most often encounter when using Vim9
script and `:def` functions; details are below:
- Comments start with #, not ": >
echo "hello" # comment
- Using a backslash for line continuation is hardly ever needed: >
echo "hello "
.. yourName
.. ", how are you?"
- White space is required in many places to improve readability.
- Assign values without `:let` *E1126* , declare variables with `:var`: >
var count = 0
count += 3
- Constants can be declared with `:final` and `:const`: >
final matches = [] # add to the list later
const names = ['Betty', 'Peter'] # cannot be changed
- `:final` cannot be used as an abbreviation of `:finally`.
- Variables and functions are script-local by default.
- Functions are declared with argument types and return type: >
def CallMe(count: number, message: string): bool
- Call functions without `:call`: >
writefile(['done'], 'file.txt')
- You cannot use old Ex commands:
`:Print`
`:append`
`:change`
`:d` directly followed by 'd' or 'p'.
`:insert`
`:k`
`:mode`
`:open`
`:s` with only flags
`:t`
`:xit`
- Some commands, especially those used for flow control, cannot be shortened.
E.g., `:throw` cannot be written as `:th`. *vim9-no-shorten*
- You cannot use curly-braces names.
- A range before a command must be prefixed with a colon: >
:%s/this/that
- Executing a register with "@r" does not work, you can prepend a colon or use
`:exe`: >
:exe @a
- Unless mentioned specifically, the highest |scriptversion| is used.
- When defining an expression mapping, the expression will be evaluated in the
context of the script where it was defined.
- When indexing a string the index is counted in characters, not bytes:
|vim9-string-index|
- Some possibly unexpected differences: |vim9-gotchas|.
In legacy Vim script comments start with double quote. In Vim9 script
comments start with #. >
# declarations
var count = 0 # number of occurrences
The reason is that a double quote can also be the start of a string. In many
places, especially halfway through an expression with a line break, it's hard
to tell what the meaning is, since both a string and a comment can be followed
by arbitrary text. To avoid confusion only # comments are recognized. This
is the same as in shell scripts and Python programs.
In Vi # is a command to list text with numbers. In Vim9 script you can use
`:number` for that. >
:101 number
When starting to read a script file Vim doesn't know it is |Vim9| script until
the `vim9script` command is found. Until that point you would need to use
legacy comments: >
" legacy comment
vim9script
# Vim9 comment
That looks ugly, better put `vim9script` in the very first line: >
vim9script
# Vim9 comment
In legacy Vim script # is also used for the alternate file name. In Vim9
script you need to use %% instead. Instead of ## use %%% (stands for all
arguments).
Vim9 functions ~
*E1099*
A function defined with `:def` is compiled. Execution is many times faster,
often 10 to 100 times.
Many errors are already found when compiling, before the function is executed.
The syntax is strict, to enforce code that is easy to read and understand.
When a function argument is optional (it has a default value) passing `v:none`
as the argument results in using the default value. This is useful when you
want to specify a value for an argument that comes after an argument that
should use its default value. Example: >
def MyFunc(one = 'one', last = 'last')
...
enddef
MyFunc(v:none, 'LAST') # first argument uses default value 'one'
<
*vim9-ignored-argument* *E1181*
The argument "_" (an underscore) can be used to ignore the argument. This is
most useful in callbacks where you don't need it, but do need to give an
argument to match the call. E.g. when using map() two arguments are passed,
the key and the value, to ignore the key: >
map(numberList, (_, v) => v * 2)
There is no error for using the "_" argument multiple times. No type needs to
be given.
When referring to a function and no "s:" or "g:" prefix is used, Vim will
search for the function:
- in the function scope, in block scopes
- in the script scope
Imported functions are found with the prefix from the `:import` command.
Since a script-local function reference can be used without "s:" the name must
start with an upper case letter even when using the "s:" prefix. In legacy
script "s:funcref" could be used, because it could not be referred to with
"funcref". In Vim9 script it can, therefore "s:Funcref" must be used to avoid
that the name interferes with builtin functions.
*vim9-s-namespace* *E1268*
The use of the "s:" prefix is not supported at the Vim9 script level. All
functions and variables without a prefix are script-local.
In legacy functions the use of "s:" for script items is required, as before.
No matter if the script is Vim9 or legacy.
In all cases the function must be defined before used. That is when it is
called, when `:defcompile` causes it to be compiled, or when code that calls
it is being compiled (to figure out the return type).
The result is that functions and variables without a namespace can usually be
found in the script, either defined there or imported. Global functions and
variables could be defined anywhere (good luck finding out where! You can
often see where it was last set using |:verbose|).
*E1102*
Global functions can still be defined and deleted at nearly any time. In
Vim9 script script-local functions are defined once when the script is sourced
and cannot be deleted or replaced by itself (it can be by reloading the
script).
When loading a Vim9 script a second time all existing script-local functions
and variables are deleted, thus you start with a clean slate. This is useful
if you are developing a plugin and want to try a new version. If you renamed
something you don't have to worry about the old name still hanging around.
You want to use this in scripts that use a `finish` command to bail out at
some point when loaded again. E.g. when a buffer local option is set to a
function, the function does not need to be defined more than once: >
vim9script noclear
setlocal completefunc=SomeFunc
if exists('*SomeFunc')
finish
endif
def SomeFunc()
....
The variables are only visible in the block where they are defined and nested
blocks. Once the block ends the variable is no longer accessible: >
if cond
var inner = 5
else
var inner = 0
endif
echo inner # Error!
Global variables must be prefixed with "g:", also at the script level. >
vim9script
var script_local = 'text'
g:global = 'value'
var Funcref = g:ThatFunction
*vim9-function-defined-later*
Although global functions can be called without the "g:" prefix, they must
exist when compiled. By adding the "g:" prefix the function can be defined
later. Example: >
def CallPluginFunc()
if exists('g:loaded_plugin')
g:PluginFunc()
endif
enddef
If you do it like this, you get an error at compile time that "PluginFunc"
does not exist, even when "g:loaded_plugin" does not exist: >
def CallPluginFunc()
if exists('g:loaded_plugin')
PluginFunc() # Error - function not found
endif
enddef
You can use exists_compiled() to avoid the error, but then the function would
not be called, even when "g:loaded_plugin" is defined later: >
def CallPluginFunc()
if exists_compiled('g:loaded_plugin')
PluginFunc() # Function may never be called
endif
enddef
Since `&opt = value` is now assigning a value to option "opt", ":&" cannot be
used to repeat a `:substitute` command.
*vim9-unpack-ignore*
For an unpack assignment the underscore can be used to ignore a list item,
similar to how a function argument can be ignored: >
[a, _, c] = theList
To ignore any remaining items: >
[a, b; _] = longList
< *E1163* *E1080*
Declaring more than one variable at a time, using the unpack notation, is
possible. Each variable can have a type or infer it from the value: >
var [v1: number, v2] = GetValues()
Use this only when there is a list with values, declaring one variable per
line is much easier to read and change later.
Constants ~
*vim9-const* *vim9-final*
How constants work varies between languages. Some consider a variable that
can't be assigned another value a constant. JavaScript is an example. Others
also make the value immutable, thus when a constant uses a list, the list
cannot be changed. In Vim9 we can use both.
*E1021* *E1307*
`:const` is used for making both the variable and the value a constant. Use
this for composite structures that you want to make sure will not be modified.
Example: >
const myList = [1, 2]
myList = [3, 4] # Error!
myList[0] = 9 # Error!
myList->add(3) # Error!
< *:final* *E1125*
`:final` is used for making only the variable a constant, the value can be
changed. This is well known from Java. Example: >
final myList = [1, 2]
myList = [3, 4] # Error!
myList[0] = 9 # OK
myList->add(3) # OK
The constant only applies to the value itself, not what it refers to. >
final females = ["Mary"]
const NAMES = [["John", "Peter"], females]
NAMES[0] = ["Jack"] # Error!
NAMES[0][0] = "Jack" # Error!
NAMES[1] = ["Emma"] # Error!
NAMES[1][0] = "Emma" # OK, now females[0] == "Emma"
Omitting :call and :eval ~
*E1190*
Functions can be called without `:call`: >
writefile(lines, 'file')
Using `:call` is still possible, but this is discouraged.
In the rare case there is ambiguity between a function name and an Ex command,
prepend ":" to make clear you want to use the Ex command. For example, there
is both the `:substitute` command and the `substitute()` function. When the
line starts with `substitute(` this will use the function. Prepend a colon to
use the command instead: >
:substitute(pattern (replacement (
If the expression starts with "!" this is interpreted as a shell command, not
negation of a condition. Thus this is a shell command: >
!shellCommand->something
Put the expression in parentheses to use the "!" for negation: >
(!expression)->Method()
Note that while variables need to be defined before they can be used,
functions can be called before being defined. This is required to allow
for cyclic dependencies between functions. It is slightly less efficient,
since the function has to be looked up by name. And a typo in the function
name will only be found when the function is called.
Omitting function() ~
When using `function()` the resulting type is "func", a function with any
number of arguments and any return type (including void). The function can be
defined later if the argument is in quotes.
The ending "}" must be at the start of a line. It can be followed by other
characters, e.g.: >
var d = mapnew(dict, (k, v): string => {
return 'value'
})
No command can follow the "{", only a comment can be used there.
*command-block* *E1026*
The block can also be used for defining a user command. Inside the block Vim9
syntax will be used.
Rationale: The "}" cannot be after a command because it would require parsing
the commands to find it. For consistency with that no command can follow the
"{". Unfortunately this means using "() => { command }" does not work, line
breaks are always required.
*vim9-curly*
To avoid the "{" of a dictionary literal to be recognized as a statement block
wrap it in parentheses: >
var Lambda = (arg) => ({key: 42})
For a method call using "->" and a member using a dot, a line break is allowed
before it: >
var result = GetBuilder()
->BuilderSetWidth(333)
->BuilderSetHeight(777)
->BuilderBuild()
var result = MyDict
.member
For commands that have an argument that is a list of commands, the | character
at the start of the line indicates line continuation: >
autocmd BufNewFile *.match if condition
| echo 'match'
| endif
Note that this means that in heredoc the first line cannot start with a bar: >
var lines =<< trim END
| this doesn't work
END
Either use an empty line at the start or do not use heredoc. Or temporarily
add the "C" flag to 'cpoptions': >
set cpo+=C
var lines =<< trim END
| this works
END
set cpo-=C
If the heredoc is inside a function 'cpoptions' must be set before :def and
restored after the :enddef.
After the range an Ex command must follow. Without the colon you can call a
function without `:call`, but after a range you do need it: >
MyFunc()
:% call MyFunc()
Note that the colon is not required for the |+cmd| argument: >
edit +6 fname
It is also possible to split a function header over multiple lines, in between
arguments: >
def MyFunc(
text: string,
separator = '-'
): string
Notes:
- "enddef" cannot be used at the start of a continuation line, it ends the
current function.
- No line break is allowed in the LHS of an assignment. Specifically when
unpacking a list |:let-unpack|. This is OK: >
[var1, var2] =
Func()
< This does not work: >
[var1,
var2] =
Func()
- No line break is allowed in between arguments of an `:echo`, `:execute` and
similar commands. This is OK: >
echo [1,
2] [3,
4]
< This does not work: >
echo [1, 2]
[3, 4]
- In some cases it is difficult for Vim to parse a command, especially when
commands are used as an argument to another command, such as `:windo`. In
those cases the line continuation with a backslash has to be used.
White space ~
*E1004* *E1068* *E1069* *E1074* *E1127* *E1202*
Vim9 script enforces proper use of white space. This is no longer allowed: >
var name=234 # Error!
var name= 234 # Error!
var name =234 # Error!
There must be white space before and after the "=": >
var name = 234 # OK
White space must also be put before the # that starts a comment after a
command: >
var name = 234# Error!
var name = 234 # OK
Dictionary literals ~
*vim9-literal-dict* *E1014*
Traditionally Vim has supported dictionary literals with a {} syntax: >
let dict = {'key': value}
Later it became clear that using a simple text key is very common, thus
literal dictionaries were introduced in a backwards compatible way: >
let dict = #{key: value}
However, this #{} syntax is unlike any existing language. As it turns out
that using a literal key is much more common than using an expression, and
considering that JavaScript uses this syntax, using the {} form for dictionary
literals is considered a much more useful syntax. In Vim9 script the {} form
uses literal keys: >
var dict = {key: value}
This works for alphanumeric characters, underscore and dash. If you want to
use another character, use a single or double quoted string: >
var dict = {'key with space': value}
var dict = {"key\twith\ttabs": value}
var dict = {'': value} # empty key
< *E1139*
In case the key needs to be an expression, square brackets can be used, just
like in JavaScript: >
var dict = {["key" .. nr]: value}
The key type can be string, number, bool or float. Other types result in an
error. Without using [] the value is used as a string, keeping leading zeros.
An expression given with [] is evaluated and then converted to a string.
Leading zeros will then be dropped: >
var dict = {000123: 'without', [000456]: 'with'}
echo dict
{'456': 'with', '000123': 'without'}
A float only works inside [] because the dot is not accepted otherwise: >
var dict = {[00.013]: 'float'}
echo dict
{'0.013': 'float'}
Comparators ~
The 'ignorecase' option is not used for comparators that use strings.
Thus "=~" works like "=~#".
"is" and "isnot" (|expr-is| and |expr-isnot|) when used on strings now return
false. In legacy script they just compare the strings, in |Vim9| script they
check identity, and strings are copied when used, thus two strings are never
the same (this might change someday if strings are not copied but reference
counted).
For loop ~
*E1254*
The loop variable must not be declared yet: >
var i = 1
for i in [1, 2, 3] # Error!
Legacy Vim script has some tricks to make a for loop over a list handle
deleting items at the current or previous item. In Vim9 script it just uses
the index, if items are deleted then items in the list will be skipped.
Example legacy script: >
let l = [1, 2, 3, 4]
for i in l
echo i
call remove(l, index(l, i))
endfor
Would echo:
1
2
3
4
In compiled Vim9 script you get:
1
3
Generally, you should not change the list that is iterated over. Make a copy
first if needed.
When looping over a list of lists, the nested lists can be changed. The loop
variable is "final", it cannot be changed but what its value can be changed.
*E1306*
The depth of loops, :for and :while loops added together, cannot exceed 10.
For the "??" operator and when using "!" then there is no error, every value
is either falsy or truthy. This is mostly like JavaScript, except that an
empty list and dict is falsy:
The boolean operators "||" and "&&" expect the values to be boolean, zero or
one: >
1 || false == true
0 || 1 == true
0 || false == false
1 && true == true
0 && 1 == false
8 || 0 Error!
'yes' && 0 Error!
[] || 99 Error!
When using "!" for inverting, there is no error for using any type and the
result is a boolean. "!!" can be used to turn any value into boolean: >
!'yes' == false
!![] == false
!![1, 2, 3] == true
When using "`.."` for string concatenation arguments of simple types are
always converted to string: >
'hello ' .. 123 == 'hello 123'
'hello ' .. v:true == 'hello true'
Simple types are Number, Float, Special and Bool. For other types |string()|
should be used.
*false* *true* *null* *null_blob* *null_channel*
*null_class* *null_dict* *null_function* *null_job*
*null_list* *null_object* *null_partial* *null_string*
*E1034*
In Vim9 script one can use the following predefined values: >
true
false
null
null_blob
null_channel
null_class
null_dict
null_function
null_job
null_list
null_object
null_partial
null_string
`true` is the same as `v:true`, `false` the same as `v:false`, `null` the same
as `v:null`.
While `null` has the type "special", the other "null_" values have the type
indicated by their name. Quite often a null value is handled the same as an
empty value, but not always. The values can be useful to clear a script-local
variable, since they cannot be deleted with `:unlet`. E.g.: >
var theJob = job_start(...)
# let the job do its work
theJob = null_job
The values can also be useful as the default value for an argument: >
def MyFunc(b: blob = null_blob)
# Note: compare against null, not null_blob,
# to distinguish the default value from an empty blob.
if b == null
# b argument was not given
See |null-compare| for more information about testing against null.
It is possible to compare `null` with any value, this will not give a type
error. However, comparing `null` with a number, float or bool will always
result in `false`. This is different from legacy script, where comparing
`null` with zero or `false` would return `true`.
*vim9-false-true*
When converting a boolean to a string `false` and `true` are used, not
`v:false` and `v:true` like in legacy script. `v:none` has no `none`
replacement, it has no equivalent in other languages.
*vim9-string-index*
Indexing a string with [idx] or taking a slice with [idx : idx] uses character
indexes instead of byte indexes. Composing characters are included.
Example: >
echo 'bár'[1]
In legacy script this results in the character 0xc3 (an illegal byte), in Vim9
script this results in the string 'á'.
A negative index is counting from the end, "[-1]" is the last character.
To exclude the last character use |slice()|.
To count composing characters separately use |strcharpart()|.
If the index is out of range then an empty string results.
In legacy script "++var" and "--var" would be silently accepted and have no
effect. This is an error in Vim9 script.
Numbers starting with zero are not considered to be octal, only numbers
starting with "0o" are octal: "0o744". |scriptversion-4|
Also, there cannot be a space between the command and the separator: >
g /pattern/cmd # invalid command - ERROR
s /pattern/repl # invalid command - ERROR
Functions defined with `:def` compile the whole function. Legacy functions
can bail out, and the following lines are not parsed: >
func Maybe()
if !has('feature')
return
endif
use-feature
endfunc
Vim9 functions are compiled as a whole: >
def Maybe()
if !has('feature')
return
endif
use-feature # May give a compilation error
enddef
For a workaround, split it in two functions: >
func Maybe()
if has('feature')
call MaybeInner()
endif
endfunc
if has('feature')
def MaybeInner()
use-feature
enddef
endif
Or put the unsupported code inside an `if` with a constant expression that
evaluates to false: >
def Maybe()
if has('feature')
use-feature
endif
enddef
The `exists_compiled()` function can also be used for this.
*vim9-user-command*
Another side effect of compiling a function is that the presence of a user
command is checked at compile time. If the user command is defined later an
error will result. This works: >
command -nargs=1 MyCommand echom <q-args>
def Works()
MyCommand 123
enddef
This will give an error for "MyCommand" not being defined: >
def Works()
command -nargs=1 MyCommand echom <q-args>
MyCommand 123
enddef
A workaround is to invoke the command indirectly with `:execute`: >
def Works()
command -nargs=1 MyCommand echom <q-args>
execute 'MyCommand 123'
enddef
Note that for unrecognized commands there is no check for "|" and a following
command. This will give an error for missing `endif`: >
def Maybe()
if has('feature') | use-feature | endif
enddef
Other differences ~
You may also find this wiki useful. It was written by an early adopter of
Vim9 script: https://github.com/lacygoill/wiki/blob/master/vim/vim9.md
*:++* *:--*
The ++ and -- commands have been added. They are very similar to adding or
subtracting one: >
++var
var += 1
--var
var -= 1
==============================================================================
*:def* *E1028*
:def[!] {name}([arguments])[: {return-type}]
Define a new function by the name {name}. The body of
the function follows in the next lines, until the
matching `:enddef`. *E1073*
*E1011*
The {name} must be less than 100 bytes long.
*E1003* *E1027* *E1056* *E1059*
The type of value used with `:return` must match
{return-type}. When {return-type} is omitted or is
"void" the function is not expected to return
anything.
*E1077* *E1123*
{arguments} is a sequence of zero or more argument
declarations. There are three forms:
{name}: {type}
{name} = {value}
{name}: {type} = {value}
The first form is a mandatory argument, the caller
must always provide them.
The second and third form are optional arguments.
When the caller omits an argument the {value} is used.
You may also find this wiki useful. It was written by an early adopter of
Vim9 script: https://github.com/lacygoill/wiki/blob/master/vim/vim9.md
*:defc* *:defcompile*
:defc[ompile] Compile functions and classes (|class-compile|)
defined in the current script that were not compiled
yet. This will report any errors found during
compilation.
:defc[ompile] {func}
:defc[ompile] debug {func}
:defc[ompile] profile {func}
Compile function {func}, if needed. Use "debug" and
"profile" to specify the compilation mode.
This will report any errors found during compilation.
{func} call also be "ClassName.functionName" to
compile a function or method in a class.
{func} call also be "ClassName" to compile all
functions and methods in a class.
*:disa* *:disassemble*
:disa[ssemble] {func} Show the instructions generated for {func}.
This is for debugging and testing. *E1061*
Note that for command line completion of {func} you
can prepend "s:" to find script-local functions.
Limitations ~
Local variables will not be visible to string evaluation. For example: >
def MapList(): list<string>
var list = ['aa', 'bb', 'cc', 'dd']
return range(1, 2)->map('list[v:val]')
enddef
For commands that are not compiled, such as `:edit`, backtick expansion can be
used and it can use the local scope. Example: >
def Replace()
var fname = 'blah.txt'
edit `=fname`
enddef
Closures defined in a loop will share the same context. For example: >
var flist: list<func>
for i in range(5)
var inloop = i
flist[i] = () => inloop
endfor
echo range(5)->map((i, _) => flist[i]())
# Result: [4, 4, 4, 4, 4]
< *E1271*
A closure must be compiled in the context that it is defined in, so that
variables in that context can be found. This mostly happens correctly, except
when a function is marked for debugging with `:breakadd` after it was compiled.
Make sure to define the breakpoint before compiling the outer function.
The "inloop" variable will exist only once, all closures put in the list refer
to the same instance, which in the end will have the value 4. This is
efficient, also when looping many times. If you do want a separate context
for each closure, call a function to define it: >
def GetClosure(i: number): func
var infunc = i
return () => infunc
enddef
Note that at the script level the loop variable will be invalid after the
loop, also when used in a closure that is called later, e.g. with a timer.
This will generate error |E1302|: >
for n in range(4)
timer_start(500 * n, (_) => {
echowin n
})
endfor
You need to use a block and define a variable there, and use that one in the
closure: >
for n in range(4)
{
var nr = n
timer_start(500 * n, (_) => {
echowin nr
})
}
endfor
Using `:echowindow` is useful in a timer, the messages go into a popup and will
not interfere with what the user is doing when it triggers.
==============================================================================
4. Types *vim9-types*
*E1008* *E1009* *E1010* *E1012*
*E1013* *E1029* *E1030*
The following builtin types are supported:
bool
number
float
string
blob
list<{type}>
dict<{type}>
job
channel
func
func: {type}
func({type}, ...)
func({type}, ...): {type}
void
These types can be used in declarations, but no simple value will actually
have the "void" type. Trying to use a void (e.g. a function without a
return value) results in error *E1031* *E1186* .
If the return type is "void" the function does not return a value.
The reference can also be a |Partial|, in which case it stores extra arguments
and/or a dictionary, which are not visible to the caller. Since they are
called in the same way the declaration is the same.
:interface MyInterface
:var mine: MyInterface
:class MyTemplate<Targ>
:var mine: MyTemplate<number>
:var mine: MyTemplate<string>
:class MyInterface<Targ>
:var mine: MyInterface<number>
:var mine: MyInterface<string>
{not implemented yet}
Variable types and type casting ~
*variable-types*
Variables declared in Vim9 script or in a `:def` function have a type, either
specified explicitly or inferred from the initialization.
Global, buffer, window and tab page variables do not have a specific type, the
value can be changed at any time, possibly changing the type. Therefore, in
compiled code the "any" type is assumed.
This can be a problem when the "any" type is undesired and the actual type is
expected to always be the same. For example, when declaring a list: >
var l: list<number> = [1, g:two]
At compile time Vim doesn't know the type of "g:two" and the expression type
becomes list<any>. An instruction is generated to check the list type before
doing the assignment, which is a bit inefficient.
*type-casting* *E1104*
To avoid this, use a type cast: >
var l: list<number> = [1, <number>g:two]
The compiled code will then only check that "g:two" is a number and give an
error if it isn't. This is called type casting.
The syntax of a type cast is: "<" {type} ">". There cannot be white space
after the "<" or before the ">" (to avoid them being confused with
smaller-than and bigger-than operators).
If a type is incomplete you get *E1363* , e.g. when you have an object for
which the class is not known (usually that is a null object).
Type inference ~
*type-inference*
In general: Whenever the type is clear it can be omitted. For example, when
declaring a variable and giving it a value: >
var name = 0 # infers number type
var name = 'hello' # infers string type
The type of a list and dictionary comes from the common type of the values.
If the values all have the same type, that type is used for the list or
dictionary. If there is a mix of types, the "any" type is used. >
[1, 2, 3] list<number>
['a', 'b', 'c'] list<string>
[1, 'x', 3] list<any>
The common type of function references, if they do not all have the same
number of arguments, uses "(...)" to indicate the number of arguments is not
specified. For example: >
def Foo(x: bool)
enddef
def Bar(x: bool, y: bool)
enddef
var funclist = [Foo, Bar]
echo funclist->typename()
Results in:
list<func(...)>
For script-local variables in Vim9 script the type is checked, also when the
variable was declared in a legacy function.
When a type has been declared this is attached to a List or Dictionary. When
later some expression attempts to change the type an error will be given: >
var ll: list<number> = [1, 2, 3]
ll->extend(['x']) # Error, 'x' is not a number
One consequence is that the item type of a list or dict given to |map()| must
not change, if the type was declared. This will give an error in Vim9
script: >
var mylist: list<number> = [1, 2, 3]
echo map(mylist, (i, v) => 'item ' .. i)
< E1012: Type mismatch; expected number but got string in map() ~
If the item type was not declared or determined to be "any" it can change to a
more specific type. E.g. when a list of mixed types gets changed to a list of
strings: >
var mylist = [1, 2.0, '3']
# typename(mylist) == "list<any>"
map(mylist, (i, v) => 'item ' .. i)
# typename(mylist) == "list<string>", no error
When using the list constant directly, the type is not declared and is allowed
to change: >
echo map([1, 2, 3], (i, v) => 'item ' .. i) # OK
The reasoning behind this is that when a type is declared and the list is
passed around and changed, the declaration must always hold. So that you can
rely on the type to match the declared type. For a constant this is not
needed.
*E1158*
Same for |extend()|, use |extendnew()| instead, and for |flatten()|, use
|flattennew()| instead. Since |flatten()| is intended to always change the
type, it can not be used in Vim9 script.
var l: list<any>
# ... add lots of stuff to list
l = [] # clear the variable and release container resources
Using the empty container, rather than null_<type>, to clear a container
variable may avoid null complications as described in |null-anomalies|.
When a list or dict is declared, if the item type is not specified and can not
be inferred, then the type is "any": >
var d1 = {} # type is "dict<any>"
var d2 = null_dict # type is "dict<any>"
*null-compare*
For familiar null compare semantics, where a null container is not equal to
an empty container, do not use null_<type> in a comparison: >
vim9script
def F(arg: list<string> = null_list)
if arg == null
echo "null"
else
echo printf("not null, %sempty", empty(arg) ? '' : 'not ')
endif
enddef
F() # output: "null"
F(null_list) # output: "null"
F([]) # output: "not null, empty"
F(['']) # output: "not null, not empty"
The above function takes a list of strings and reports on it.
Change the above function signature to accept different types of arguments: >
def F(arg: list<any> = null_list) # any type of list
def F(arg: any = null) # any type
<
In the above example, where the goal is to distinguish a null list from an
empty list, comparing against `null` instead of `null_list` is the correct
choice. The basic reason is because "null_list == null" and "[] != null".
Comparing to `null_list` fails since "[] == null_list". In the following section
there are details about comparison results.
*null-details* *null-anomalies*
This section describes issues about using null and null_<type>; included below
are the enumerated results of null comparisons. In some cases, if familiar
with vim9 null semantics, the programmer may chose to use null_<type> in
comparisons and/or other situations.
NOTE: the specialized variables, like job, default to null value and have no
corresponding empty value.
==============================================================================
A Vim9 script can be written to be imported. This means that some items are
intentionally exported, made available to other scripts. When the exporting
script is imported in another script, these exported items can then be used in
that script. All the other items remain script-local in the exporting script
and cannot be accessed by the importing script.
This mechanism exists for writing a script that can be sourced (imported) by
other scripts, while making sure these other scripts only have access to what
you want them to. This also avoids using the global namespace, which has a
risk of name collisions. For example when you have two plugins with similar
functionality.
You can cheat by using the global namespace explicitly. That should be done
only for things that really are global.
Namespace ~
*vim9-namespace*
To recognize a file that can be imported the `vim9script` statement must
appear as the first statement in the file (see |vim9-mix| for an exception).
It tells Vim to interpret the script in its own namespace, instead of the
global namespace. If a file starts with: >
vim9script
var myvar = 'yes'
Then "myvar" will only exist in this file. While without `vim9script` it would
be available as `g:myvar` from any other script and function.
*E1101*
The variables at the file level are very much like the script-local "s:"
variables in legacy Vim script, but the "s:" is omitted. And they cannot be
deleted.
In Vim9 script the global "g:" namespace can still be used as before. And the
"w:", "b:" and "t:" namespaces. These have in common that variables are not
declared, have no specific type and they can be deleted. *E1304*
*vim9-mix*
There is one way to use both legacy and Vim9 syntax in one script file: >
" comments may go here
if !has('vim9script')
" legacy script commands go here
finish
endif
vim9script
# Vim9 script commands go here
This allows for writing a script that takes advantage of the Vim9 script
syntax if possible, but will also work on a Vim version without it.
Export ~
*:export* *:exp*
Exporting an item can be written as: >
export const EXPORTED_CONST = 1234
export var someValue = ...
export final someValue = ...
export const someValue = ...
export def MyFunc() ...
export class MyClass ...
export interface MyClass ...
< *E1043* *E1044*
As this suggests, only constants, variables, `:def` functions and classes can
be exported.
*E1042*
`:export` can only be used in Vim9 script, at the script level.
Import ~
*:import* *:imp* *E1094* *E1047* *E1262*
*E1048* *E1049* *E1053* *E1071* *E1088* *E1236*
The exported items can be imported in another script. The import syntax has
two forms. The simple form: >
import {filename}
<
Where {filename} is an expression that must evaluate to a string. In this
form the filename should end in ".vim" and the portion before ".vim" will
become the script local name of the namespace. For example: >
import "myscript.vim"
<
This makes each exported item in "myscript.vim" available as "myscript.item".
*:import-as* *E1257* *E1261*
In case the name is long or ambiguous, this form can be used to specify
another name: >
import {longfilename} as {name}
<
In this form {name} becomes a specific script local name for the imported
namespace. Therefore {name} must consist of letters, digits and '_', like
|internal-variables|. The {longfilename} expression must evaluate to any
filename. For example: >
import "thatscript.vim.v2" as that
< *E1060* *E1258* *E1259* *E1260*
Then you can use "that.item", etc. You are free to choose the name "that".
Use something that will be recognized as referring to the imported script.
Avoid command names, command modifiers and builtin function names, because the
name will shadow them. It's better not to start the name with a capital
letter, since it can then also shadow global user commands and functions.
Also, you cannot use the name for something else in the script, such as a
function or variable name.
In case the dot in the name is undesired, a local reference can be made for a
function: >
var LongFunc = that.LongFuncName
This does not work for variables, since the value would be copied once and
when changing the variable the copy will change, not the original variable.
You will need to use the full name, with the dot.
`:import` can not be used in a function. Imported items are intended to exist
at the script level and only imported once.
If the name does not end in ".vim" then the use of "as name" is required.
Once a vim9 script file has been imported, the result is cached and used the
next time the same script is imported. It will not be read again.
It is not allowed to import the same script twice, also when using two
different "as" names.
When using the imported name the dot and the item name must be in the same
line, there can be no line break: >
echo that.
name # Error!
echo that
.name # Error!
< *import-map*
When you've imported a function from one script into a vim9 script you can
refer to the imported function in a mapping by prefixing it with |<SID>|: >
noremap <silent> ,a :call <SID>name.Function()<CR>
When the mapping is defined "<SID>name." will be replaced with <SNR> and the
script ID of the imported script.
An even simpler solution is using |<ScriptCmd>|: >
noremap ,a <ScriptCmd>name.Function()<CR>
Note that this does not work for variables, only for functions.
*import-legacy* *legacy-import*
`:import` can also be used in legacy Vim script. The imported namespace still
becomes script-local, even when the "s:" prefix is not given. For example: >
import "myfile.vim"
call s:myfile.MyFunc()
The "autoload" argument to `:import` means that the script is not loaded
until one of the items is actually used. The script will be found under
the "autoload" directory in 'runtimepath' instead of the "import"
directory. Alternatively a relative or absolute name can be used, see
below.
You can split up the functionality and import other scripts from the
autoload script as you like. This way you can share code between plugins.
Searching for the autoload script in all entries in 'runtimepath' can be a bit
slow. If the plugin knows where the script is located, quite often a relative
path can be used. This avoids the search and should be quite a bit faster.
Another advantage is that the script name does not need to be unique. An
absolute path is also possible. Examples: >
import autoload '../lib/implement.vim'
import autoload MyScriptsDir .. '/lib/implement.vim'
For defining a mapping that uses the imported autoload script the special key
|<ScriptCmd>| is useful. It allows for a command in a mapping to use the
script context of where the mapping was defined.
==============================================================================
In |Vim9| script you can have classes, objects and interfaces like in most
popular object-oriented programming languages. Since this is a lot of
functionality it is located in a separate help file: |vim9class.txt|.
==============================================================================
9. Rationale *vim9-rationale*
Plugin writers have asked for much faster Vim script. Investigations have
shown that keeping the existing semantics of function calls make this close to
impossible, because of the overhead involved with calling a function, setting
up the local function scope and executing lines. There are many details that
need to be handled, such as error messages and exceptions. The need to create
a dictionary for a: and l: scopes, the a:000 list and several others add too
much overhead that cannot be avoided.
Using "def" to define a function comes from Python. Other languages use
"function" which clashes with legacy Vim script.
Type checking ~
The syntax for types, using <type> for compound types, is similar to Java. It
is easy to understand and widely used. The type names are what were used in
Vim before, with some additions such as "void" and "bool".
Removing clutter and weirdness ~
Once decided that `:def` functions have different syntax than legacy functions,
we are free to add improvements to make the code more familiar for users who
know popular programming languages. In other words: remove weird things that
only Vim does.
We can also remove clutter, mainly things that were done to make Vim script
backwards compatible with the good old Vi commands.
Examples:
- Drop `:call` for calling a function and `:eval` for evaluating an
expression.
- Drop using a leading backslash for line continuation, automatically figure
out where an expression ends.
Goal is to limit the differences. A good criteria is that when the old syntax
is accidentally used you are very likely to get an error message.
Script writers have complained that the Vim script syntax is unexpectedly
different from what they are used to. To reduce this complaint popular
languages are used as an example. At the same time, we do not want to abandon
the well-known parts of legacy Vim script.
People familiar with other languages (Java, Python, etc.) will also find
things in TypeScript that they do not like or do not understand. We'll try to
avoid those things.
Declarations ~
Legacy Vim script uses `:let` for every assignment, while in Vim9 declarations
are used. That is different, thus it's good to use a different command:
`:var`. This is used in many languages. The semantics might be slightly
different, but it's easily recognized as a declaration.
Using `:const` for constants is common, but the semantics varies. Some
languages only make the variable immutable, others also make the value
immutable. Since "final" is well known from Java for only making the variable
immutable we decided to use that. And then `:const` can be used for making
both immutable. This was also used in legacy Vim script and the meaning is
almost the same.
Since legacy and Vim9 script will be mixed and global variables will be
shared, optional type checking is desirable. Also, type inference will avoid
the need for specifying the type in many cases. The TypeScript syntax fits
best for adding types to declarations: >
var name: string # string type is specified
...
name = 'John'
const greeting = 'hello' # string type is inferred
The first is more familiar for anyone used to C or Java. The second one
doesn't really have an advantage over the first, so let's discard the second.
Since we use type inference the type can be left out when it can be inferred
from the value. This means that after `var` we don't know if a type or a name
follows. That makes parsing harder, not only for Vim but also for humans.
Also, it will not be allowed to use a variable name that could be a type name,
using `var string string` is too confusing.
The chosen syntax, using a colon to separate the name from the type, adds
punctuation, but it actually makes it easier to recognize the parts of a
declaration.
Expressions ~
Expression evaluation was already close to what other languages are doing.
Some details are unexpected and can be improved. For example a boolean
condition would accept a string, convert it to a number and check if the
number is non-zero. This is unexpected and often leads to mistakes, since
text not starting with a number would be converted to zero, which is
considered false. Thus using a string for a condition would often not give an
error and be considered false. That is confusing.
If you have any type of value and want to use it as a boolean, use the `!!`
operator:
true: `!!'text'` `!![99]` `!!{'x': 1}` `!!99`
false: `!!''` `!![]` `!!{}`
A problem of legacy Vim script is that by default all functions and variables
are global. It is possible to make them script-local, but then they are not
available in other scripts. This defies the concept of a package that only
exports selected items and keeps the rest local.
In Vim9 script a mechanism very similar to the JavaScript import and export
mechanism is supported. It is a variant to the existing `:source` command
that works like one would expect:
- Instead of making everything global by default, everything is script-local,
some of these are exported.
- When importing a script the symbols that are imported are explicitly listed,
avoiding name conflicts and failures if functionality is added later.
- The mechanism allows for writing a big, long script with a very clear API:
the exported functions, variables and classes.
- By using relative paths loading can be much faster for an import inside of a
package, no need to search many directories.
- Once an import has been used, its items are cached and loading it again is
not needed.
- The Vim-specific use of "s:" to make things script-local can be dropped.
When sourcing a Vim9 script (from a Vim9 or legacy script), only the items
defined globally can be used, not the exported items. Alternatives
considered:
- All the exported items become available as script-local items. This makes
it uncontrollable what items get defined and likely soon leads to trouble.
- Use the exported items and make them global. Disadvantage is that it's then
not possible to avoid name clashes in the global namespace.
- Completely disallow sourcing a Vim9 script, require using `:import`. That
makes it difficult to use scripts for testing, or sourcing them from the
command line to try them out.
Note that you CAN also use `:import` in legacy Vim script, see above.
Functions are compiled when called or when `:defcompile` is used. Why not
compile them early, so that syntax and type errors are reported early?
The functions can't be compiled right away when encountered, because there may
be forward references to functions defined later. Consider defining functions
A, B and C, where A calls B, B calls C, and C calls A again. It's impossible
to reorder the functions to avoid forward references.
An alternative would be to first scan through the file to locate items and
figure out their type, so that forward references are found, and only then
execute the script and compile the functions. This means the script has to be
parsed twice, which is slower, and some conditions at the script level, such
as checking if a feature is supported, are hard to use. An attempt was made
to see if it works, but it turned out to be impossible to make work well.
It would be possible to compile all the functions at the end of the script.
The drawback is that if a function never gets called, the overhead of
compiling it counts anyway. Since startup speed is very important, in most
cases it's better to do it later and accept that syntax and type errors are
only reported then. In case these errors should be found early, e.g. when
testing, a `:defcompile` command at the end of the script will help out.
Vim supports interfaces to Perl, Python, Lua, Tcl and a few others. But
these interfaces have never become widely used, for various reasons. When
Vim9 was designed a decision was made to make these interfaces lower priority
and concentrate on Vim script.
Still, plugin writers may find other languages more familiar, want to use
existing libraries or see a performance benefit. We encourage plugin authors
to write code in any language and run it as an external process, using jobs
and channels. We can try to make this easier somehow.
Using an external tool also has disadvantages. An alternative is to convert
the tool into Vim script. For that to be possible without too much
translation, and keeping the code fast at the same time, the constructs of the
tool need to be supported. Since most languages support classes the lack of
support for classes in Vim is then a problem.
vim:tw=78:ts=8:noet:ft=help:norl: