/usr/share/vim/vim82/doc
NameSizeModeActions
arabic.txt119010644editdlrm
autocmd.txt722200644editdlrm
change.txt780490644editdlrm
channel.txt503090644editdlrm
cmdline.txt493420644editdlrm
debug.txt68960644editdlrm
debugger.txt54430644editdlrm
develop.txt228610644editdlrm
diff.txt170240644editdlrm
digraph.txt619780644editdlrm
editing.txt741610644editdlrm
eval.txt5412090644editdlrm
farsi.txt5230644editdlrm
filetype.txt275940644editdlrm
fold.txt236890644editdlrm
ft_ada.txt182460644editdlrm
ft_rust.txt95960644editdlrm
ft_sql.txt307000644editdlrm
gui.txt527410644editdlrm
gui_w32.txt194510644editdlrm
gui_x11.txt299060644editdlrm
hangulin.txt5980644editdlrm
hebrew.txt56660644editdlrm
help.txt88990644editdlrm
helphelp.txt157290644editdlrm
howto.txt29160644editdlrm
if_cscop.txt193250644editdlrm
if_lua.txt167550644editdlrm
if_mzsch.txt119410644editdlrm
if_ole.txt73610644editdlrm
if_perl.txt112810644editdlrm
if_pyth.txt384350644editdlrm
if_ruby.txt85160644editdlrm
if_sniff.txt2710644editdlrm
if_tcl.txt229510644editdlrm
indent.txt422840644editdlrm
index.txt806380644editdlrm
insert.txt852940644editdlrm
intro.txt389720644editdlrm
map.txt707990644editdlrm
mbyte.txt596420644editdlrm
message.txt314250644editdlrm
mlang.txt81870644editdlrm
motion.txt513410644editdlrm
netbeans.txt379820644editdlrm
options.txt3913080644editdlrm
os_390.txt47490644editdlrm
os_amiga.txt54660644editdlrm
os_beos.txt2930644editdlrm
os_dos.txt123780644editdlrm
os_haiku.txt85920644editdlrm
os_mac.txt68560644editdlrm
os_mint.txt3910644editdlrm
os_msdos.txt5230644editdlrm
os_os2.txt2990644editdlrm
os_qnx.txt40710644editdlrm
os_risc.txt3280644editdlrm
os_unix.txt26030644editdlrm
os_vms.txt329060644editdlrm
os_win32.txt133510644editdlrm
pattern.txt601370644editdlrm
pi_getscript.txt210820644editdlrm
pi_gzip.txt12940644editdlrm
pi_logipat.txt41960644editdlrm
pi_netrw.txt1792860644editdlrm
pi_paren.txt22740644editdlrm
pi_spec.txt41270644editdlrm
pi_tar.txt68130644editdlrm
pi_vimball.txt118590644editdlrm
pi_zip.txt72940644editdlrm
popup.txt394720644editdlrm
print.txt310250644editdlrm
quickfix.txt829250644editdlrm
quickref.txt721440644editdlrm
quotes.txt127480644editdlrm
recover.txt107200644editdlrm
remote.txt84170644editdlrm
repeat.txt421860644editdlrm
rileft.txt49380644editdlrm
russian.txt30240644editdlrm
scroll.txt137810644editdlrm
sign.txt258590644editdlrm
spell.txt640990644editdlrm
sponsor.txt72030644editdlrm
starting.txt735910644editdlrm
syntax.txt2258630644editdlrm
tabpage.txt172480644editdlrm
tags3548120644editdlrm
tagsrch.txt402780644editdlrm
term.txt474980644editdlrm
terminal.txt561000644editdlrm
testing.txt149540644editdlrm
textprop.txt134020644editdlrm
tips.txt210670644editdlrm
todo.txt3094200644editdlrm
uganda.txt140280644editdlrm
undo.txt165840644editdlrm
usr_01.txt70810644editdlrm
usr_02.txt242260644editdlrm
usr_03.txt238720644editdlrm
usr_04.txt191310644editdlrm
usr_05.txt270230644editdlrm
usr_06.txt96110644editdlrm
usr_07.txt159950644editdlrm
usr_08.txt193780644editdlrm
usr_09.txt115020644editdlrm
usr_10.txt292000644editdlrm
usr_11.txt130250644editdlrm
usr_12.txt134290644editdlrm
usr_20.txt137090644editdlrm
usr_21.txt184560644editdlrm
usr_22.txt149990644editdlrm
usr_23.txt127440644editdlrm
usr_24.txt208270644editdlrm
usr_25.txt191760644editdlrm
usr_26.txt82590644editdlrm
usr_27.txt177090644editdlrm
usr_28.txt160200644editdlrm
usr_29.txt201210644editdlrm
usr_30.txt226580644editdlrm
usr_31.txt103990644editdlrm
usr_32.txt53780644editdlrm
usr_40.txt231950644editdlrm
usr_41.txt973780644editdlrm
usr_42.txt138530644editdlrm
usr_43.txt74090644editdlrm
usr_44.txt289480644editdlrm
usr_45.txt176880644editdlrm
usr_46.txt70960644editdlrm
usr_90.txt168360644editdlrm
usr_toc.txt95050644editdlrm
various.txt310970644editdlrm
version4.txt139110644editdlrm
version5.txt3085320644editdlrm
version6.txt5769400644editdlrm
version7.txt6747010644editdlrm
version8.txt19403070644editdlrm
vim9.txt571580644editdlrm
visual.txt217940644editdlrm
vi_diff.txt574280644editdlrm
windows.txt557320644editdlrm
workshop.txt3710644editdlrm
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: