= Vim9script = See also [[Vim/Script|Vimscript]], which is the original language predating Vim 9. <> ---- == Example == This is a convoluted example, making use of a command that is also compatible with traditional Vimscript, but nonetheless works. The Vim9script interpretter only begins to run after the `vim9script` command is seen within a script. {{{ vim9script :echo "Hello, world!" }}} ---- == Breaking Changes == * The comment character has been changed from a doublequote (`"`) to a hash (`#`) * White space is required in many places to improve readability. * Declare variables with `:var` * Variables are script-local by default * Assigning values to variables does not require `:let` * Declare constants with `:final` and `:const` Constants can be declared with :final and :const: * `:final` cannot be used as a short form of `:finally` * Declare functions like `def CallMe(count: number, message: string): bool` * Functions are script-local by default * Calling functions does not require `:call` * Many `ex(1)` commands have been removed * `:Print` * `:append` * `:change` * `:d` immediately followed by `d` or `p`. * `:insert` * `:k` * `:mode` * `:open` * `:s` followed only by flags * `:t` * `:xit` * Many commands cannot be shortened (see `vim9-no-shorten`) * Curly brace names are disallowed * A range before a command must be prefixed with a colon (e.g. `:%s/foo/bar`) * Executing a register requires prefixing with a colon (e.g. `:@r`) or using `:exe` (e.g. `:exe @r`) * Expression mappings are evaluated in the context of its definition Furthermore, in order to break code across lines in traditional Vimscript, backslashes were often necessary. Vim9script does not have the same requirement. Functions are now compiled. The compilation occurs in the background of a `vim(1)` session, and may raise errors. ---- == Variables and Constants == {{{ var count = 0 count += 3 }}} {{{ final matches = [] # add to the list later const names = ['Betty', 'Peter'] # cannot be changed }}} ---- == Functions == Vim9script tries to compile functions lazily. Compilation is triggered by: * the first call to a function * a `:defcompile` command located within a script after the function definition * `:disassemble` * a new function definition that calls to a function, or uses a function as a function reference To forward reference a function that is not defined yet, try: {{{ def MyFunc() execute('DefinedLater') enddef }}} ---- CategoryRicottone