Skip to content

Commit ab0f4f9

Browse files
committed
add initial paragraphs comparing to legacy Vimscript
1 parent 57d988a commit ab0f4f9

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

vim9script.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,35 @@ contributors:
88
---
99

1010
Vim9Script is a modern scripting language introduced in Vim 9.0.
11-
It is designed to replace legacy Vimscript (also called VimL), which is a sequence of ex-commands enhanced with scripting constructs like variables, functions, and control flow.
1211

13-
Unlike legacy Vimscript, Vim9Script enforces stricter syntax, improves performance, and supports modern programming features such as strong typing, classes, lambdas, and modules.
12+
Vim9script, which is exclusive to Vim version 9+, improves on its predecessor, legacy Vimscript, also called VimL, which is a sequence of Ex commands enhanced with scripting constructs like variables, functions, and control flow.
13+
(Ex commands, such as `:echo`, `:write`, `:substitute`, `:quit`, ... are commands that are part of the legacy Ex editor to execute a single action, one-liners without return values.)
14+
Legacy Vimscript is interpreted line by line, requiring backslashes to continue lines, and commands like `let` and `call` are used to make them resemble ex-commands.
15+
16+
In contrast, Vim9script supports multi-line syntax natively, without needing line continuation.
17+
This makes it less suited for command-line usage, unlike traditional ex-commands, so that Vim9Script complements these for scripting.
18+
19+
Vim9Script enforces stricter syntax, improves performance, and supports modern programming features such as strong typing, classes, lambdas, and modules.
20+
Differences (see https://vimhelp.org/vim9.txt.html#vim9-differences) include:
21+
22+
1. New syntax basics
23+
• Comments start with `#` instead of `"`.
24+
• Line-continuation backslashes are rarely needed --- just concatenate with `..`.
25+
• Whitespace is significant in many places to keep things readable.
26+
27+
2. Variables and constants
28+
• Declare regular variables with `:var`, e.g. `var count = 0`
29+
• Change them with standard operators (`count += 3`) --- no more `:let`.
30+
• Declare immutable names with `:const` or `:final`.
31+
32+
3. Typed, script-local functions
33+
• All functions (and variables) are script-local by default.
34+
• Use `:def` with typed params and a return type, e.g.
35+
`def foo(x: number, y: string): bool`
36+
• Call them like normal functions (no `:call`).
37+
38+
All Ex commands can still be used inside functions and, vice-versa, you can call a function by an Ex command with `:vim9` (respectively `:call` in legacy vimscript) on the command line.
39+
You can also define your own commands that call functions.
1440

1541
Try [vim9-conversion-aid](https://github.com/ubaldot/vim9-conversion-aid) as a starting point to convert legacy Vimscript to Vim9Script.
1642

@@ -25,6 +51,10 @@ vim9script
2551
# There is no distinction between single and multi-line comments.
2652
# Use # inside a line at any position to comment out all following characters.
2753
54+
# You can run this Vim9 script directly in Vim. After pasting the content
55+
# into a Vim buffer, enter the command `so` in command-line mode (press `:`
56+
# first to enter command-line mode).
57+
2858
##################################################################
2959
## 1. Primitive types, collection types, operators, and regex
3060
##################################################################
@@ -112,7 +142,7 @@ echo v:numbersize # echos either 32 or 64
112142
# permitted value, for 64-bit 9,223,372,036,854,775,807, will fail:
113143
echo 9223372036854775807 + 1 # -9223372036854775808
114144
115-
# Numbers may be expressed as decimal, hexadecimal (prefixed with 0x),
145+
# Numbers may be expressed as decimal, hexadecimal (prefixed with 0x),
116146
# octal (0o or 0O), or binary (0b). These are all decimal 15:
117147
echo 15 + 0b1111 + 0xF + 0o17 # 60
118148
@@ -205,7 +235,7 @@ echo $PATH # (references the environment variable, PATH)
205235
206236
# Vim has many settings, which are also variables. They may be local or
207237
# global scoped.
208-
echo &textwidth # (echos the bufferlocal value of the textwidth option)
238+
echo &textwidth # (echos the buffer-local value of the textwidth option)
209239
echo &wildmenu # (echos the boolean value of command-line wildcard expansion)
210240
211241
@@ -375,7 +405,7 @@ var q: Quad = Quad.Square
375405
# result using string interpolation and showing multiple lines without `\`,
376406
# which is required in vimscript but not in Vim9 script.
377407
var result = $"A {tolower(q.name)} has {q.QuadProps()[0]} sides of equal " ..
378-
"length and " ..
408+
"length and " ..
379409
(q.QuadProps()[1] ? 'has only right angles' : 'has no right angle')
380410
echo result
381411

0 commit comments

Comments
 (0)