/
usr
/
share
/
vim
/
vim82
/
doc
/
/usr/share/vim/vim82/doc
mkdir
upload
Name
Size
Mode
Actions
arabic.txt
11901
0644
edit
dl
rm
autocmd.txt
72220
0644
edit
dl
rm
change.txt
78049
0644
edit
dl
rm
channel.txt
50309
0644
edit
dl
rm
cmdline.txt
49342
0644
edit
dl
rm
debug.txt
6896
0644
edit
dl
rm
debugger.txt
5443
0644
edit
dl
rm
develop.txt
22861
0644
edit
dl
rm
diff.txt
17024
0644
edit
dl
rm
digraph.txt
61978
0644
edit
dl
rm
editing.txt
74161
0644
edit
dl
rm
eval.txt
541209
0644
edit
dl
rm
farsi.txt
523
0644
edit
dl
rm
filetype.txt
27594
0644
edit
dl
rm
fold.txt
23689
0644
edit
dl
rm
ft_ada.txt
18246
0644
edit
dl
rm
ft_rust.txt
9596
0644
edit
dl
rm
ft_sql.txt
30700
0644
edit
dl
rm
gui.txt
52741
0644
edit
dl
rm
gui_w32.txt
19451
0644
edit
dl
rm
gui_x11.txt
29906
0644
edit
dl
rm
hangulin.txt
598
0644
edit
dl
rm
hebrew.txt
5666
0644
edit
dl
rm
help.txt
8899
0644
edit
dl
rm
helphelp.txt
15729
0644
edit
dl
rm
howto.txt
2916
0644
edit
dl
rm
if_cscop.txt
19325
0644
edit
dl
rm
if_lua.txt
16755
0644
edit
dl
rm
if_mzsch.txt
11941
0644
edit
dl
rm
if_ole.txt
7361
0644
edit
dl
rm
if_perl.txt
11281
0644
edit
dl
rm
if_pyth.txt
38435
0644
edit
dl
rm
if_ruby.txt
8516
0644
edit
dl
rm
if_sniff.txt
271
0644
edit
dl
rm
if_tcl.txt
22951
0644
edit
dl
rm
indent.txt
42284
0644
edit
dl
rm
index.txt
80638
0644
edit
dl
rm
insert.txt
85294
0644
edit
dl
rm
intro.txt
38972
0644
edit
dl
rm
map.txt
70799
0644
edit
dl
rm
mbyte.txt
59642
0644
edit
dl
rm
message.txt
31425
0644
edit
dl
rm
mlang.txt
8187
0644
edit
dl
rm
motion.txt
51341
0644
edit
dl
rm
netbeans.txt
37982
0644
edit
dl
rm
options.txt
391308
0644
edit
dl
rm
os_390.txt
4749
0644
edit
dl
rm
os_amiga.txt
5466
0644
edit
dl
rm
os_beos.txt
293
0644
edit
dl
rm
os_dos.txt
12378
0644
edit
dl
rm
os_haiku.txt
8592
0644
edit
dl
rm
os_mac.txt
6856
0644
edit
dl
rm
os_mint.txt
391
0644
edit
dl
rm
os_msdos.txt
523
0644
edit
dl
rm
os_os2.txt
299
0644
edit
dl
rm
os_qnx.txt
4071
0644
edit
dl
rm
os_risc.txt
328
0644
edit
dl
rm
os_unix.txt
2603
0644
edit
dl
rm
os_vms.txt
32906
0644
edit
dl
rm
os_win32.txt
13351
0644
edit
dl
rm
pattern.txt
60137
0644
edit
dl
rm
pi_getscript.txt
21082
0644
edit
dl
rm
pi_gzip.txt
1294
0644
edit
dl
rm
pi_logipat.txt
4196
0644
edit
dl
rm
pi_netrw.txt
179286
0644
edit
dl
rm
pi_paren.txt
2274
0644
edit
dl
rm
pi_spec.txt
4127
0644
edit
dl
rm
pi_tar.txt
6813
0644
edit
dl
rm
pi_vimball.txt
11859
0644
edit
dl
rm
pi_zip.txt
7294
0644
edit
dl
rm
popup.txt
39472
0644
edit
dl
rm
print.txt
31025
0644
edit
dl
rm
quickfix.txt
82925
0644
edit
dl
rm
quickref.txt
72144
0644
edit
dl
rm
quotes.txt
12748
0644
edit
dl
rm
recover.txt
10720
0644
edit
dl
rm
remote.txt
8417
0644
edit
dl
rm
repeat.txt
42186
0644
edit
dl
rm
rileft.txt
4938
0644
edit
dl
rm
russian.txt
3024
0644
edit
dl
rm
scroll.txt
13781
0644
edit
dl
rm
sign.txt
25859
0644
edit
dl
rm
spell.txt
64099
0644
edit
dl
rm
sponsor.txt
7203
0644
edit
dl
rm
starting.txt
73591
0644
edit
dl
rm
syntax.txt
225863
0644
edit
dl
rm
tabpage.txt
17248
0644
edit
dl
rm
tags
354812
0644
edit
dl
rm
tagsrch.txt
40278
0644
edit
dl
rm
term.txt
47498
0644
edit
dl
rm
terminal.txt
56100
0644
edit
dl
rm
testing.txt
14954
0644
edit
dl
rm
textprop.txt
13402
0644
edit
dl
rm
tips.txt
21067
0644
edit
dl
rm
todo.txt
309420
0644
edit
dl
rm
uganda.txt
14028
0644
edit
dl
rm
undo.txt
16584
0644
edit
dl
rm
usr_01.txt
7081
0644
edit
dl
rm
usr_02.txt
24226
0644
edit
dl
rm
usr_03.txt
23872
0644
edit
dl
rm
usr_04.txt
19131
0644
edit
dl
rm
usr_05.txt
27023
0644
edit
dl
rm
usr_06.txt
9611
0644
edit
dl
rm
usr_07.txt
15995
0644
edit
dl
rm
usr_08.txt
19378
0644
edit
dl
rm
usr_09.txt
11502
0644
edit
dl
rm
usr_10.txt
29200
0644
edit
dl
rm
usr_11.txt
13025
0644
edit
dl
rm
usr_12.txt
13429
0644
edit
dl
rm
usr_20.txt
13709
0644
edit
dl
rm
usr_21.txt
18456
0644
edit
dl
rm
usr_22.txt
14999
0644
edit
dl
rm
usr_23.txt
12744
0644
edit
dl
rm
usr_24.txt
20827
0644
edit
dl
rm
usr_25.txt
19176
0644
edit
dl
rm
usr_26.txt
8259
0644
edit
dl
rm
usr_27.txt
17709
0644
edit
dl
rm
usr_28.txt
16020
0644
edit
dl
rm
usr_29.txt
20121
0644
edit
dl
rm
usr_30.txt
22658
0644
edit
dl
rm
usr_31.txt
10399
0644
edit
dl
rm
usr_32.txt
5378
0644
edit
dl
rm
usr_40.txt
23195
0644
edit
dl
rm
usr_41.txt
97378
0644
edit
dl
rm
usr_42.txt
13853
0644
edit
dl
rm
usr_43.txt
7409
0644
edit
dl
rm
usr_44.txt
28948
0644
edit
dl
rm
usr_45.txt
17688
0644
edit
dl
rm
usr_46.txt
7096
0644
edit
dl
rm
usr_90.txt
16836
0644
edit
dl
rm
usr_toc.txt
9505
0644
edit
dl
rm
various.txt
31097
0644
edit
dl
rm
version4.txt
13911
0644
edit
dl
rm
version5.txt
308532
0644
edit
dl
rm
version6.txt
576940
0644
edit
dl
rm
version7.txt
674701
0644
edit
dl
rm
version8.txt
1940307
0644
edit
dl
rm
vim9.txt
57158
0644
edit
dl
rm
visual.txt
21794
0644
edit
dl
rm
vi_diff.txt
57428
0644
edit
dl
rm
windows.txt
55732
0644
edit
dl
rm
workshop.txt
371
0644
edit
dl
rm
Edit:
/usr/share/vim/vim82/doc/usr_46.txt
(7096B)
*usr_46.txt* For Vim version 8.2. Last change: 2020 Jun 14 VIM USER MANUAL - by Bram Moolenaar Write plugins using Vim9 script The Vim9 script language is used for writing plugins, especially larger ones that use multiple files. This chapter explains how to split up a plugin into modules, import and export items and keep the rest local. |46.1| Introduction |46.2| Variable declarations |46.3| Functions and types |46.?| Using a Vim9 script from legacy script Next chapter: |usr_90.txt| Installing Vim Previous chapter: |usr_45.txt| Select your language (locale) Table of contents: |usr_toc.txt| ============================================================================== *46.1* Introduction *vim9-script-intro* Vim9 script was designed to make it easier to write large Vim scripts. It looks more like other script languages, especially Typescript. Also, functions are compiled into instructions that can be executed quickly. This makes Vim9 script a lot faster, up to a 100 times. The basic idea is that a script file has items that are private, only used inside the script file, and items that are exported, used outside of the script file. The exported items can then be used by scripts that import them. That makes very clear what is defined where. Let's start with an example, a script that exports one function and has one private function: > vim9script " This indicates a Vim9 script file. export def GetMessage(): string let result = '' ... result = GetPart(count) ... return result enddef def GetPart(nr: number): string if nr == 4 return 'yes' else return 'no' endif enddef The `vim9script` command must be the very first command in the file. Without it Vim will assume legacy script syntax. The `export def GetMessage(): string` line starts with `export`, meaning that this function can be imported and called by other scripts. The line `def GetPart(...` does not start with `export`, this is a script-local function, it can only be used inside this script file. In the `export def GetMessage(): string` line you will notice the colon and the return type. Vim9 functions, defined with `def`, require specifying the type of arguments and the return type. That way Vim can compile the code efficiently. The GetPart function defines an argument "nr" of type "number". Notice that the assignment `result = GetPart(count)` does not use the `let` command. That is explained in the next section. ============================================================================== *46.2* Variable declarations *vim9-declarations* In Vim9 script variables are declared once with a `:let` or `:const` command. Assigning a value is done without `:let` and it is not possible to `:unlet` the variable. In most cases you will want to declare the variable and initialize it at the same time: > let myText = 'some text' ... myText = 'other text' The type of the variable will be inferred from the expression. In this case it is a string. If you initialize with a number, then the type is number: > let myNumber = 1234 ... myNumber = 0 If you try to assign a string to this variable, you will get an error: > let myNumber = 'this fails!' In the rare case you want a variable that can take values of any type, you have to specify the type: > let myVar: any = 1234 myVar = 'text also works' You can also declare a variable without assigning a value. In that case Vim will initialize it to zero or empty: > let word: string if condition word = 'yes' else word = 'no' endif Although it's shorter to do: > let word = condition ? 'yes' : 'no' ============================================================================== *46.3* Functions and types Legacy Vim script does have type checking, but this happens at runtime, when the code is executed. And it's permissive, often a computation gives an unexpected value instead of reporting an error. Thus you can define a function and think it's fine, but see a problem only later when it is called: > let s:collected = '' func ExtendAndReturn(add) let s:collected += a:add return s:collected endfunc Can you spot the error? Try this: > echo ExtendAndReturn('text') And you'll see zero. Why? Because in legacy Vim script "+=" will convert the arguments to numbers, and any string without a number results in zero! With `:def` the type checking happens when compiling the function. For that you need to specify the argument types and the return type. Also notice that the argument is used without the "a:" prefix: > let s:collected = '' def ExtendAndReturn(add: string): string s:collected += add return s:collected enddef defcompile Here we use `:defcompile` to do the compilation right away, without it the compilation would happen when the function is called. Vim will tell you what you did wrong: > E1013: type mismatch, expected number but got string Vim9 script is strict, it uses the "+" operator only for numbers and floats. For string concatenation ".." must be used. This avoids mistakes and avoids the automatic conversion that gave a surprising result above. So you change the first line of the function to: > s:collected ..= add And now it works. If the function does not return anything, just leave out the return type: > def ReportResult(result: string) echo 'The result is: ' .. result enddef This is also checked, if you try to return a value you'll get an error. In case you don't care about types or have a function that does work with multiple types, you can use the "any" type: > def Store(key: string, value: any) resultDict[key] = value enddef ============================================================================== *46.?* Using a Vim9 script from legacy script *source-vim9-script* In some cases you have a legacy Vim script where you want to use items from a Vim9 script. For example in your .vimrc you want to initialize a plugin. The best way to do this is to use `:import`. For example: > import Init as NiceInit from 'myNicePlugin.vim' call NiceInit('today') This finds the exported function "Init" in the Vim9 script file and makes it available as script-local item "NiceInit". `:import` always uses the script namespace, even when "s:" is not given. If "myNicePlugin.vim" was already sourced it is not sourced again. Besides avoiding putting any items in the global namespace (where name clashes can cause unexpected errors), this also means the script is sourced only once, no matter how many times items from it are imported. In some cases, e.g. for testing, you may just want to source the Vim9 script. That is OK, but then only global items will be available. The Vim9 script will have to make sure to use a unique name for these global items. Example: > source ~/.vim/extra/myNicePlugin.vim call g:NicePluginTest() ============================================================================== Next chapter: |usr_90.txt| Installing Vim Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl:
Save
cmd:
run