*vim9.txt* For Vim version 8.2. Last change: 2021 Feb 03
VIM REFERENCE MANUAL by Bram Moolenaar
THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
Vim9 script commands and expressions. *Vim9* *vim9*
Most expression help is in |eval.txt|. This file is about the new syntax and
features in Vim9 script.
THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
1. What is Vim9 script? |Vim9-script|
2. Differences |vim9-differences|
3. New style functions |fast-functions|
4. Types |vim9-types|
5. Namespace, Import and Export |vim9script|
6. Future work: classes |vim9-classes|
9. Rationale |vim9-rationale|
==============================================================================
1. What is Vim9 script? *Vim9-script*
THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
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 main goal of Vim9 script is to drastically improve performance. This is
accomplished by compiling commands into instructions that can be efficiently
executed. An increase in execution speed of 10 to 100 times can be expected.
A secondary goal is to avoid Vim-specific constructs and get closer to
commonly used programming languages, such as JavaScript, TypeScript and Java.
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.
The Vim9 script syntax and semantics are used in:
- a function defined with the `:def` command
- a script file where the first command is `vim9script`
- an autocommand defined in the context of the above
- a command prefixed with the `vim9cmd` command modifier
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.
*:vim9* *:vim9cmd*
:vim9[cmd] {cmd}
Execute {cmd} using Vim9 script syntax and semantics.
Useful when typing a command and in a legacy script or
function.
==============================================================================
2. Differences from legacy Vim script *vim9-differences*
THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
Overview ~
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.
- Assign values without `:let`, declare variables with `:var`: >
var count = 0
count += 3
- Constants can be declared with `:final` and `:const`: >
final matches = [] # add matches
const names = ['Betty', 'Peter'] # cannot be changed
- `:final` cannot be used as an abbreviation of `:finally`.
- Variables and functions a