blob: 1fbd66f3c34bc7abf0a08b6b1fc53e41f800bac6 [file] [log] [blame]
Bram Moolenaar2d8ed022022-05-21 13:08:16 +01001*usr_52.txt* For Vim version 8.2. Last change: 2022 May 16
Bram Moolenaar65e0d772020-06-14 17:29:55 +02002
3 VIM USER MANUAL - by Bram Moolenaar
4
5 Write plugins using Vim9 script
6
7
8The Vim9 script language is used for writing plugins, especially larger ones
9that use multiple files. This chapter explains how to split up a plugin into
10modules, import and export items and keep the rest local.
11
Bram Moolenaar30ab04e2022-05-14 13:33:50 +010012|52.1| Introduction
13|52.2| Variable declarations
14|52.3| Functions and types
15|52.4| Using a Vim9 script from legacy script
Bram Moolenaar65e0d772020-06-14 17:29:55 +020016
17 Next chapter: |usr_90.txt| Installing Vim
Bram Moolenaar30ab04e2022-05-14 13:33:50 +010018 Previous chapter: |usr_51.txt| Create a plugin
Bram Moolenaar65e0d772020-06-14 17:29:55 +020019Table of contents: |usr_toc.txt|
20
21==============================================================================
Bram Moolenaar30ab04e2022-05-14 13:33:50 +010022*52.1* Introduction *vim9-script-intro*
Bram Moolenaar65e0d772020-06-14 17:29:55 +020023
24Vim9 script was designed to make it easier to write large Vim scripts. It
25looks more like other script languages, especially Typescript. Also,
26functions are compiled into instructions that can be executed quickly. This
27makes Vim9 script a lot faster, up to a 100 times.
28
29The basic idea is that a script file has items that are private, only used
30inside the script file, and items that are exported, used outside of the
31script file. The exported items can then be used by scripts that import them.
32That makes very clear what is defined where.
33
34Let's start with an example, a script that exports one function and has one
35private function: >
36
Bram Moolenaare7b1ea02020-08-07 19:54:59 +020037 vim9script " This indicates a Vim9 script file.
Bram Moolenaar65e0d772020-06-14 17:29:55 +020038
39 export def GetMessage(): string
40 let result = ''
41 ...
42 result = GetPart(count)
43 ...
44 return result
45 enddef
46
47 def GetPart(nr: number): string
48 if nr == 4
49 return 'yes'
50 else
51 return 'no'
52 endif
53 enddef
54
55The `vim9script` command must be the very first command in the file. Without
56it Vim will assume legacy script syntax.
57
58The `export def GetMessage(): string` line starts with `export`, meaning that
59this function can be imported and called by other scripts. The line
60`def GetPart(...` does not start with `export`, this is a script-local
61function, it can only be used inside this script file.
62
63In the `export def GetMessage(): string` line you will notice the colon and
64the return type. Vim9 functions, defined with `def`, require specifying the
65type of arguments and the return type. That way Vim can compile the code
66efficiently. The GetPart function defines an argument "nr" of type "number".
67
68Notice that the assignment `result = GetPart(count)` does not use the `let`
69command. That is explained in the next section.
70
71==============================================================================
Bram Moolenaar30ab04e2022-05-14 13:33:50 +010072*52.2* Variable declarations *vim9-declarations*
Bram Moolenaar65e0d772020-06-14 17:29:55 +020073
74In Vim9 script variables are declared once with a `:let` or `:const` command.
75Assigning a value is done without `:let` and it is not possible to `:unlet`
76the variable.
77
78In most cases you will want to declare the variable and initialize it at the
79same time: >
80 let myText = 'some text'
81 ...
82 myText = 'other text'
83
84The type of the variable will be inferred from the expression. In this case
85it is a string. If you initialize with a number, then the type is number: >
86 let myNumber = 1234
87 ...
88 myNumber = 0
89
90If you try to assign a string to this variable, you will get an error: >
91 let myNumber = 'this fails!'
92
93In the rare case you want a variable that can take values of any type, you
94have to specify the type: >
95 let myVar: any = 1234
96 myVar = 'text also works'
97
98You can also declare a variable without assigning a value. In that case Vim
99will initialize it to zero or empty: >
100 let word: string
101 if condition
102 word = 'yes'
103 else
104 word = 'no'
105 endif
106
107Although it's shorter to do: >
108 let word = condition ? 'yes' : 'no'
109
110==============================================================================
Bram Moolenaar30ab04e2022-05-14 13:33:50 +0100111*52.3* Functions and types
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200112
Bram Moolenaar2d8ed022022-05-21 13:08:16 +0100113Legacy Vim script only checks types at runtime, when the code is executed.
114And it's permissive, often a computation gives an unexpected value instead of
115reporting an error. Thus you can define a function and think it's fine, but
116see a problem only later when it is called: >
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200117 let s:collected = ''
118 func ExtendAndReturn(add)
119 let s:collected += a:add
120 return s:collected
121 endfunc
122
123Can you spot the error? Try this: >
124 echo ExtendAndReturn('text')
125And you'll see zero. Why? Because in legacy Vim script "+=" will convert the
126arguments to numbers, and any string without a number results in zero!
127
128With `:def` the type checking happens when compiling the function. For that
129you need to specify the argument types and the return type. Also notice that
130the argument is used without the "a:" prefix: >
131 let s:collected = ''
132 def ExtendAndReturn(add: string): string
133 s:collected += add
134 return s:collected
135 enddef
Bram Moolenaar2d8ed022022-05-21 13:08:16 +0100136 disassemble ExtendAndReturn
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200137
Bram Moolenaar2d8ed022022-05-21 13:08:16 +0100138Here we use `:disassemble` to do the compilation right away, without it the
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200139compilation would happen when the function is called. Vim will tell you what
140you did wrong: >
Bram Moolenaar2d8ed022022-05-21 13:08:16 +0100141 E1051: Wrong argument type for +
142
143Side note: here the context is legacy script, when using Vim9 script you would
144put `:defcompile` at the end of the script to check for errors in the
145functions defined in it.
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200146
147Vim9 script is strict, it uses the "+" operator only for numbers and floats.
148For string concatenation ".." must be used. This avoids mistakes and avoids
Bram Moolenaar73fef332020-06-21 22:12:03 +0200149the automatic conversion that gave a surprising result above. So you change
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200150the first line of the function to: >
151 s:collected ..= add
152And now it works.
153
154If the function does not return anything, just leave out the return type: >
155 def ReportResult(result: string)
156 echo 'The result is: ' .. result
157 enddef
158
159This is also checked, if you try to return a value you'll get an error.
160
161In case you don't care about types or have a function that does work with
162multiple types, you can use the "any" type: >
163 def Store(key: string, value: any)
164 resultDict[key] = value
165 enddef
166
167==============================================================================
Bram Moolenaar30ab04e2022-05-14 13:33:50 +0100168*52.4* Using a Vim9 script from legacy script *source-vim9-script*
Bram Moolenaar65e0d772020-06-14 17:29:55 +0200169
170In some cases you have a legacy Vim script where you want to use items from a
171Vim9 script. For example in your .vimrc you want to initialize a plugin. The
172best way to do this is to use `:import`. For example: >
173
174 import Init as NiceInit from 'myNicePlugin.vim'
175 call NiceInit('today')
176
177This finds the exported function "Init" in the Vim9 script file and makes it
178available as script-local item "NiceInit". `:import` always uses the script
179namespace, even when "s:" is not given. If "myNicePlugin.vim" was already
180sourced it is not sourced again.
181
182Besides avoiding putting any items in the global namespace (where name clashes
183can cause unexpected errors), this also means the script is sourced only once,
184no matter how many times items from it are imported.
185
186In some cases, e.g. for testing, you may just want to source the Vim9 script.
187That is OK, but then only global items will be available. The Vim9 script
188will have to make sure to use a unique name for these global items. Example: >
189 source ~/.vim/extra/myNicePlugin.vim
190 call g:NicePluginTest()
191
192==============================================================================
193
194Next chapter: |usr_90.txt| Installing Vim
195
196Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: