@@ -25,7 +25,7 @@ THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
2525
2626==============================================================================
2727
28- 1. What is Vim9 script? *vim9 -script*
28+ 1. What is Vim9 script? *Vim9 -script*
2929
3030THIS IS STILL UNDER DEVELOPMENT - ANYTHING CAN BREAK - ANYTHING CAN CHANGE
3131
@@ -112,7 +112,12 @@ In Vi # is a command to list text with numbers. In Vim9 script you can use
112112 101 number
113113
114114 To improve readability there must be a space between a command and the #
115- that starts a comment.
115+ that starts a comment: >
116+ var = value # comment
117+ var = value# error!
118+
119+ In legacy script # is also used for the alternate file name. In Vim9 script
120+ you need to use %% instead. Instead of ## use %%% (stands for all arguments).
116121
117122
118123Vim9 functions ~
@@ -193,6 +198,45 @@ You can use an autoload function if needed, or call a legacy function and have
193198| FuncUndefined | triggered there.
194199
195200
201+ Reloading a Vim9 script clears functions and variables by default ~
202+ *vim9-reload*
203+ When loading a legacy Vim script a second time nothing is removed, the
204+ commands will replace existing variables and functions and create new ones.
205+
206+ When loading a Vim9 script a second time all existing script-local functions
207+ and variables are deleted, thus you start with a clean slate. This is useful
208+ if you are developing a plugin and want to try a new version. If you renamed
209+ something you don't have to worry about the old name still hanging around.
210+
211+ If you do want to keep items, use: >
212+ vimscript noclear
213+
214+ You want to use this in scripts that use a `finish ` command to bail out at
215+ some point when loaded again. E.g. when a buffer local option is set: >
216+ vimscript noclear
217+ setlocal completefunc=SomeFunc
218+ if exists('*SomeFunc') | finish | endif
219+ def g:SomeFunc()
220+ ....
221+
222+ There is one gotcha: If a compiled function is replaced and it is called from
223+ another compiled function that is not replaced, it will try to call the
224+ function from before it was replaced, which no longer exists. This doesn't
225+ work: >
226+ vimscript noclear
227+
228+ def ReplaceMe()
229+ echo 'function redefined every time'
230+ enddef
231+
232+ if exists('s:loaded') | finish | endif
233+ var s:loaded = true
234+
235+ def NotReplaced()
236+ ReplaceMe() # Error if ReplaceMe() was redefined
237+ enddef
238+
239+
196240Variable declarations with :var, :final and :const ~
197241 *vim9-declaration* *:var*
198242Local variables need to be declared with `:var ` . Local constants need to be
@@ -340,7 +384,7 @@ When using `function()` the resulting type is "func", a function with any
340384number of arguments and any return type. The function can be defined later.
341385
342386
343- Lamba using => instead of -> ~
387+ Lambda using => instead of -> ~
344388
345389In legacy script there can be confusion between using "->" for a method call
346390and for a lambda. Also, when a "{" is found the parser needs to figure out if
@@ -351,7 +395,7 @@ To avoid these problems Vim9 script uses a different syntax for a lambda,
351395which is similar to Javascript: >
352396 var Lambda = (arg) => expression
353397
354- No line break is allowed in the arguments of a lambda up to and includeing the
398+ No line break is allowed in the arguments of a lambda up to and including the
355399"=>". This is OK: >
356400 filter(list, (k, v) =>
357401 v > 0)
@@ -369,9 +413,9 @@ Additionally, a lambda can contain statements in {}: >
369413 }
370414 NOT IMPLEMENTED YET
371415
372- Note that the "{" must be followed by white space, otherwise it is assumed to
373- be the start of a dictionary : >
374- var Lambda = (arg) => {key: 42}
416+ To avoid the "{" of a dictionary literal to be recognized as a statement block
417+ wrap it in parenthesis : >
418+ var Lambda = (arg) => ( {key: 42})
375419
376420
377421Automatic line continuation ~
@@ -737,18 +781,24 @@ prefix and they do not need to exist (they can be deleted any time).
737781Limitations ~
738782
739783Local variables will not be visible to string evaluation. For example: >
740- def EvalString (): list<string>
784+ def MapList (): list<string>
741785 var list = ['aa', 'bb', 'cc', 'dd']
742786 return range(1, 2)->map('list[v:val]')
743787 enddef
744788
745789 The map argument is a string expression, which is evaluated without the
746790function scope. Instead, use a lambda: >
747- def EvalString (): list<string>
791+ def MapList (): list<string>
748792 var list = ['aa', 'bb', 'cc', 'dd']
749- return range(1, 2)->map({ _, v - > list[v] } )
793+ return range(1, 2)->map(( _, v) = > list[v])
750794 enddef
751795
796+ The same is true for commands that are not compiled, such as `:global ` .
797+ For these the backtick expansion can be used. Example: >
798+ def Replace()
799+ var newText = 'blah'
800+ g/pattern/s/^/`=newText`/
801+ enddef
752802
753803==============================================================================
754804
0 commit comments