/usr/share/vim/vim82/doc
NameSizeModeActions
arabic.txt122040644editdlrm
autocmd.txt743140644editdlrm
builtin.txt3890750644editdlrm
change.txt792140644editdlrm
channel.txt503870644editdlrm
cmdline.txt501300644editdlrm
debug.txt68960644editdlrm
debugger.txt54430644editdlrm
develop.txt228610644editdlrm
diff.txt175150644editdlrm
digraph.txt620820644editdlrm
editing.txt753190644editdlrm
eval.txt1782820644editdlrm
farsi.txt5230644editdlrm
filetype.txt279980644editdlrm
fold.txt241390644editdlrm
ft_ada.txt182460644editdlrm
ft_ps1.txt21870644editdlrm
ft_raku.txt41830644editdlrm
ft_rust.txt95960644editdlrm
ft_sql.txt306970644editdlrm
gui.txt530760644editdlrm
gui_w32.txt191170644editdlrm
gui_x11.txt299060644editdlrm
hangulin.txt5980644editdlrm
hebrew.txt56660644editdlrm
help.txt90980644editdlrm
help.txt.vim-tiny14310644editdlrm
helphelp.txt159330644editdlrm
howto.txt29160644editdlrm
if_cscop.txt193250644editdlrm
if_lua.txt185460644editdlrm
if_mzsch.txt119410644editdlrm
if_ole.txt73610644editdlrm
if_perl.txt112810644editdlrm
if_pyth.txt386440644editdlrm
if_ruby.txt85160644editdlrm
if_sniff.txt2710644editdlrm
if_tcl.txt229410644editdlrm
indent.txt419020644editdlrm
index.txt811250644editdlrm
insert.txt875680644editdlrm
intro.txt389340644editdlrm
map.txt744640644editdlrm
mbyte.txt596400644editdlrm
message.txt316710644editdlrm
mlang.txt81870644editdlrm
motion.txt518910644editdlrm
netbeans.txt379820644editdlrm
options.txt4012540644editdlrm
os_390.txt47490644editdlrm
os_amiga.txt54660644editdlrm
os_beos.txt2930644editdlrm
os_dos.txt154960644editdlrm
os_haiku.txt84960644editdlrm
os_mac.txt68560644editdlrm
os_mint.txt3910644editdlrm
os_msdos.txt5230644editdlrm
os_os2.txt2990644editdlrm
os_qnx.txt40710644editdlrm
os_risc.txt3280644editdlrm
os_unix.txt26030644editdlrm
os_vms.txt329030644editdlrm
os_win32.txt144040644editdlrm
pattern.txt629490644editdlrm
pi_getscript.txt210820644editdlrm
pi_gzip.txt12940644editdlrm
pi_logipat.txt41960644editdlrm
pi_netrw.txt1818240644editdlrm
pi_paren.txt22740644editdlrm
pi_spec.txt41270644editdlrm
pi_tar.txt72470644editdlrm
pi_vimball.txt118590644editdlrm
pi_zip.txt74910644editdlrm
popup.txt396890644editdlrm
print.txt312090644editdlrm
quickfix.txt833310644editdlrm
quickref.txt724920644editdlrm
quotes.txt127480644editdlrm
README.Debian3240644editdlrm
recover.txt107200644editdlrm
remote.txt84200644editdlrm
repeat.txt435650644editdlrm
rileft.txt49380644editdlrm
russian.txt30240644editdlrm
scroll.txt137810644editdlrm
sign.txt268700644editdlrm
spell.txt640950644editdlrm
sponsor.txt68030644editdlrm
starting.txt735990644editdlrm
syntax.txt2297950644editdlrm
tabpage.txt172480644editdlrm
tags3626700644editdlrm
tags.vim-tiny300644editdlrm
tagsrch.txt402780644editdlrm
term.txt498220644editdlrm
terminal.txt570980644editdlrm
testing.txt166780644editdlrm
textprop.txt163950644editdlrm
tips.txt210890644editdlrm
todo.txt3071470644editdlrm
uganda.txt140280644editdlrm
undo.txt165840644editdlrm
usr_01.txt70810644editdlrm
usr_02.txt242290644editdlrm
usr_03.txt238720644editdlrm
usr_04.txt191310644editdlrm
usr_05.txt270230644editdlrm
usr_06.txt106380644editdlrm
usr_07.txt159950644editdlrm
usr_08.txt193790644editdlrm
usr_09.txt115020644editdlrm
usr_10.txt292000644editdlrm
usr_11.txt130250644editdlrm
usr_12.txt134330644editdlrm
usr_20.txt137330644editdlrm
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.txt956260644editdlrm
usr_42.txt138530644editdlrm
usr_43.txt74090644editdlrm
usr_44.txt289480644editdlrm
usr_45.txt176880644editdlrm
usr_46.txt70960644editdlrm
usr_90.txt168360644editdlrm
usr_toc.txt95050644editdlrm
various.txt314830644editdlrm
version4.txt139110644editdlrm
version5.txt3085330644editdlrm
version6.txt5769420644editdlrm
version7.txt6747030644editdlrm
version8.txt19404660644editdlrm
vim9.txt686120644editdlrm
visual.txt218460644editdlrm
vi_diff.txt574280644editdlrm
windows.txt558980644editdlrm
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: