Mercurial > vim
comparison runtime/doc/vim9.txt @ 25619:29ec2c198c8d
Update runtime files
Commit: https://github.com/vim/vim/commit/6aa57295cfbe8f21c15f0671e45fd53cf990d404
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Aug 14 21:25:52 2021 +0200
Update runtime files
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 14 Aug 2021 21:30:04 +0200 |
parents | bdda48f01a68 |
children | d4faa2c5211b |
comparison
equal
deleted
inserted
replaced
25618:b5765575dc5a | 25619:29ec2c198c8d |
---|---|
1 *vim9.txt* For Vim version 8.2. Last change: 2021 Jul 28 | 1 *vim9.txt* For Vim version 8.2. Last change: 2021 Aug 11 |
2 | 2 |
3 | 3 |
4 VIM REFERENCE MANUAL by Bram Moolenaar | 4 VIM REFERENCE MANUAL by Bram Moolenaar |
5 | 5 |
6 | 6 |
322 echo temp # Error! | 322 echo temp # Error! |
323 | 323 |
324 This is especially useful in a user command: > | 324 This is especially useful in a user command: > |
325 | 325 |
326 command -range Rename { | 326 command -range Rename { |
327 | var save = @a | 327 var save = @a |
328 | @a = 'some expression' | 328 @a = 'some expression' |
329 | echo 'do something with ' .. @a | 329 echo 'do something with ' .. @a |
330 | @a = save | 330 @a = save |
331 |} | 331 } |
332 | 332 |
333 And with autocommands: > | 333 And with autocommands: > |
334 | 334 |
335 au BufWritePre *.go { | 335 au BufWritePre *.go { |
336 | var save = winsaveview() | 336 var save = winsaveview() |
337 | silent! exe ':%! some formatting command' | 337 silent! exe ':%! some formatting command' |
338 | winrestview(save) | 338 winrestview(save) |
339 |} | 339 } |
340 | 340 |
341 Although using a :def function probably works better. | 341 Although using a :def function probably works better. |
342 | 342 |
343 Declaring a variable with a type but without an initializer will initialize to | 343 Declaring a variable with a type but without an initializer will initialize to |
344 zero, false or empty. | 344 zero, false or empty. |
349 with `:unlet`. | 349 with `:unlet`. |
350 | 350 |
351 `:lockvar` does not work on local variables. Use `:const` and `:final` | 351 `:lockvar` does not work on local variables. Use `:const` and `:final` |
352 instead. | 352 instead. |
353 | 353 |
354 The `exists()` function does not work on local variables or arguments. These | 354 The `exists()` and `exists_compiled()` functions do not work on local variables |
355 are visible at compile time only, not at runtime. | 355 or arguments. |
356 | 356 |
357 Variables, functions and function arguments cannot shadow previously defined | 357 Variables, functions and function arguments cannot shadow previously defined |
358 or imported variables and functions in the same script file. | 358 or imported variables and functions in the same script file. |
359 Variables may shadow Ex commands, rename the variable if needed. | 359 Variables may shadow Ex commands, rename the variable if needed. |
360 | 360 |
370 def g:GlobalFunc(): string | 370 def g:GlobalFunc(): string |
371 return 'text' | 371 return 'text' |
372 enddef | 372 enddef |
373 echo GlobalFunc() | 373 echo GlobalFunc() |
374 The "g:" prefix is not needed for auto-load functions. | 374 The "g:" prefix is not needed for auto-load functions. |
375 | |
376 *vim9-function-defined-later* | |
377 Although global functions can be called without the "g:" prefix, they must | |
378 exist when compiled. By adding the "g:" prefix the function can be defined | |
379 later. Example: > | |
380 def CallPluginFunc() | |
381 if exists('g:loaded_plugin') | |
382 g:PluginFunc() | |
383 endif | |
384 enddef | |
385 | |
386 If you would do it like this you get an error at compile time that | |
387 "PluginFunc" does not exist, even when "g:loaded_plugin" does not exist: > | |
388 def CallPluginFunc() | |
389 if exists('g:loaded_plugin') | |
390 PluginFunc() # Error - function not found | |
391 endif | |
392 enddef | |
393 | |
394 You can use exists_compiled() to avoid the error, but then the function would | |
395 not be called, even when "g:loaded_plugin" is defined later: > | |
396 def CallPluginFunc() | |
397 if exists_compiled('g:loaded_plugin') | |
398 PluginFunc() # Function may never be called | |
399 endif | |
400 enddef | |
375 | 401 |
376 Since `&opt = value` is now assigning a value to option "opt", ":&" cannot be | 402 Since `&opt = value` is now assigning a value to option "opt", ":&" cannot be |
377 used to repeat a `:substitute` command. | 403 used to repeat a `:substitute` command. |
378 *vim9-unpack-ignore* | 404 *vim9-unpack-ignore* |
379 For an unpack assignment the underscore can be used to ignore a list item, | 405 For an unpack assignment the underscore can be used to ignore a list item, |
938 def Maybe() | 964 def Maybe() |
939 if has('feature') | 965 if has('feature') |
940 use-feature | 966 use-feature |
941 endif | 967 endif |
942 enddef | 968 enddef |
943 < *vim9-user-command* | 969 The `exists_compiled()` function can also be used for this. |
970 *vim9-user-command* | |
944 Another side effect of compiling a function is that the presence of a user | 971 Another side effect of compiling a function is that the presence of a user |
945 command is checked at compile time. If the user command is defined later an | 972 command is checked at compile time. If the user command is defined later an |
946 error will result. This works: > | 973 error will result. This works: > |
947 command -nargs=1 MyCommand echom <q-args> | 974 command -nargs=1 MyCommand echom <q-args> |
948 def Works() | 975 def Works() |
1405 - An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This | 1432 - An absolute path, starting with "/" on Unix or "D:/" on MS-Windows. This |
1406 will rarely be used. | 1433 will rarely be used. |
1407 - A path not being relative or absolute. This will be found in the | 1434 - A path not being relative or absolute. This will be found in the |
1408 "import" subdirectories of 'runtimepath' entries. The name will usually be | 1435 "import" subdirectories of 'runtimepath' entries. The name will usually be |
1409 longer and unique, to avoid loading the wrong file. | 1436 longer and unique, to avoid loading the wrong file. |
1410 Note that "after/import" is not used, unless it is explicitly added in | 1437 Note that "after/import" is not used. |
1411 'runtimepath'. | |
1412 | 1438 |
1413 Once a vim9 script file has been imported, the result is cached and used the | 1439 Once a vim9 script file has been imported, the result is cached and used the |
1414 next time the same script is imported. It will not be read again. | 1440 next time the same script is imported. It will not be read again. |
1415 *:import-cycle* | 1441 *:import-cycle* |
1416 The `import` commands are executed when encountered. If that script (directly | 1442 The `import` commands are executed when encountered. If that script (directly |