comparison runtime/doc/vim9.txt @ 21676:1b345fb68ae3

Update runtime files. Commit: https://github.com/vim/vim/commit/e7b1ea0276cc83cd5c612f3189a174a60d57b5ed Author: Bram Moolenaar <Bram@vim.org> Date: Fri Aug 7 19:54:59 2020 +0200 Update runtime files.
author Bram Moolenaar <Bram@vim.org>
date Fri, 07 Aug 2020 20:00:05 +0200
parents d0c76ce48326
children 571832713efa
comparison
equal deleted inserted replaced
21675:49ed426f3fb5 21676:1b345fb68ae3
1 *vim9.txt* For Vim version 8.2. Last change: 2020 Jul 25 1 *vim9.txt* For Vim version 8.2. Last change: 2020 Aug 01
2 2
3 3
4 VIM REFERENCE MANUAL by Bram Moolenaar 4 VIM REFERENCE MANUAL by Bram Moolenaar
5 5
6 6
12 features in Vim9 script. 12 features in Vim9 script.
13 13
14 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE 14 THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
15 15
16 16
17 1 What is Vim9 script? |vim9-script| 17 1. What is Vim9 script? |vim9-script|
18 2. Differences |vim9-differences| 18 2. Differences |vim9-differences|
19 3. New style functions |fast-functions| 19 3. New style functions |fast-functions|
20 4. Types |vim9-types| 20 4. Types |vim9-types|
21 5. Namespace, Import and Export |vim9script| 21 5. Namespace, Import and Export |vim9script|
22 22
117 Functions and variables are script-local by default ~ 117 Functions and variables are script-local by default ~
118 *vim9-scopes* 118 *vim9-scopes*
119 When using `:function` or `:def` to specify a new function at the script level 119 When using `:function` or `:def` to specify a new function at the script level
120 in a Vim9 script, the function is local to the script, as if "s:" was 120 in a Vim9 script, the function is local to the script, as if "s:" was
121 prefixed. Using the "s:" prefix is optional. To define or use a global 121 prefixed. Using the "s:" prefix is optional. To define or use a global
122 function or variable the "g:" prefix must be used. For functions in an 122 function or variable the "g:" prefix should be used. For functions in an
123 autoload script the "name#" prefix is sufficient. > 123 autoload script the "name#" prefix is sufficient. >
124 def ThisFunction() # script-local 124 def ThisFunction() # script-local
125 def s:ThisFunction() # script-local 125 def s:ThisFunction() # script-local
126 def g:ThatFunction() # global 126 def g:ThatFunction() # global
127 def ThatFunction() # global if no local ThatFunction()
127 def scriptname#function() # autoload 128 def scriptname#function() # autoload
128 129
129 When using `:function` or `:def` to specify a new function inside a function, 130 When using `:function` or `:def` to specify a new function inside a function,
130 the function is local to the function. It is not possible to define a 131 the function is local to the function. It is not possible to define a
131 script-local function inside a function. It is possible to define a global 132 script-local function inside a function. It is possible to define a global
132 function, using the "g:" prefix. 133 function, using the "g:" prefix.
133 134
134 When referring to a function and no "s:" or "g:" prefix is used, Vim will 135 When referring to a function and no "s:" or "g:" prefix is used, Vim will
135 search for the function in this order: 136 prefer using a local function (in the function scope, script scope or
136 - Local to the current scope and outer scopes up to the function scope. 137 imported) before looking for a global function.
137 - Local to the current script file.
138 - Imported functions, see `:import`.
139 In all cases the function must be defined before used. That is when it is 138 In all cases the function must be defined before used. That is when it is
140 first called or when `:defcompile` causes the call to be compiled. 139 first called or when `:defcompile` causes the call to be compiled.
141 140
142 The result is that functions and variables without a namespace can always be 141 The result is that functions and variables without a namespace can usually be
143 found in the script, either defined there or imported. Global functions and 142 found in the script, either defined there or imported. Global functions and
144 variables could be defined anywhere (good luck finding where!). 143 variables could be defined anywhere (good luck finding out where!).
145 144
146 Global functions can be still be defined and deleted at nearly any time. In 145 Global functions can be still be defined and deleted at nearly any time. In
147 Vim9 script script-local functions are defined once when the script is sourced 146 Vim9 script script-local functions are defined once when the script is sourced
148 and cannot be deleted or replaced. 147 and cannot be deleted or replaced.
149 148
191 An existing variable cannot be assigned to with `:let`, since that implies a 190 An existing variable cannot be assigned to with `:let`, since that implies a
192 declaration. Global, window, tab, buffer and Vim variables can only be used 191 declaration. Global, window, tab, buffer and Vim variables can only be used
193 without `:let`, because they are not really declared, they can also be deleted 192 without `:let`, because they are not really declared, they can also be deleted
194 with `:unlet`. 193 with `:unlet`.
195 194
196 Variables cannot shadow previously defined variables. 195 Variables and functions cannot shadow previously defined or imported variables
196 and functions.
197 Variables may shadow Ex commands, rename the variable if needed. 197 Variables may shadow Ex commands, rename the variable if needed.
198 198
199 Global variables and user defined functions must be prefixed with "g:", also 199 Global variables and user defined functions must be prefixed with "g:", also
200 at the script level. > 200 at the script level. >
201 vim9script 201 vim9script
230 "foobar"->Process() 230 "foobar"->Process()
231 ("foobar")->Process() 231 ("foobar")->Process()
232 'foobar'->Process() 232 'foobar'->Process()
233 ('foobar')->Process() 233 ('foobar')->Process()
234 234
235 In rare case there is ambiguity between a function name and an Ex command, use 235 In rare case there is ambiguity between a function name and an Ex command,
236 ":" to make clear you want to use the Ex command. For example, there is both 236 prepend ":" to make clear you want to use the Ex command. For example, there
237 the `:substitute` command and the `substitute()` function. When the line 237 is both the `:substitute` command and the `substitute()` function. When the
238 starts with `substitute(` this will use the function. Prepend a colon to use 238 line starts with `substitute(` this will use the function. Prepend a colon to
239 the command instead: > 239 use the command instead: >
240 :substitute(pattern (replacement ( 240 :substitute(pattern (replacement (
241 241
242 Note that while variables need to be defined before they can be used, 242 Note that while variables need to be defined before they can be used,
243 functions can be called before being defined. This is required to be able 243 functions can be called before being defined. This is required to be able
244 have cyclic dependencies between functions. It is slightly less efficient, 244 have cyclic dependencies between functions. It is slightly less efficient,
259 259
260 260
261 Automatic line continuation ~ 261 Automatic line continuation ~
262 262
263 In many cases it is obvious that an expression continues on the next line. In 263 In many cases it is obvious that an expression continues on the next line. In
264 those cases there is no need to prefix the line with a backslash. For 264 those cases there is no need to prefix the line with a backslash
265 example, when a list spans multiple lines: > 265 |line-continuation|. For example, when a list spans multiple lines: >
266 let mylist = [ 266 let mylist = [
267 'one', 267 'one',
268 'two', 268 'two',
269 ] 269 ]
270 And when a dict spans multiple lines: > 270 And when a dict spans multiple lines: >
450 % another " Vim9: line continuation without a backslash 450 % another " Vim9: line continuation without a backslash
451 :%s/a/b " Vim9: substitute on all lines 451 :%s/a/b " Vim9: substitute on all lines
452 'text'->func() " Vim9: method call 452 'text'->func() " Vim9: method call
453 :'t " legacy Vim: jump to mark m 453 :'t " legacy Vim: jump to mark m
454 454
455 Some Ex commands can be confused with assignments in Vim9 script: >
456 g:name = value # assignment
457 g:pattern:cmd # invalid command - ERROR
458 :g:pattern:cmd # :global command
459
455 Functions defined with `:def` compile the whole function. Legacy functions 460 Functions defined with `:def` compile the whole function. Legacy functions
456 can bail out, and the following lines are not parsed: > 461 can bail out, and the following lines are not parsed: >
457 func Maybe() 462 func Maybe()
458 if !has('feature') 463 if !has('feature')
459 return 464 return