@@ -131,6 +131,12 @@ echo "The year is 2025" =~ '[[:digit:]]\+$' # true
131131echo 'abc123' =~ '\v\d+' # true
132132echo 'a|b' =~ '\Va|b' # true
133133
134+ # `\zs`: Sets the start of the match.
135+ # `\ze`: Sets the end of the match.
136+ # Match only the domain from an email:
137+ var email = 'user@example.com'
138+ echo matchstr(email, '@\zs[^ ]\+\ze') # Output: 'example.com'
139+
134140
135141####################################################
136142## 2. Variables
@@ -258,17 +264,15 @@ try
258264 var lines = readfile('file.txt')
259265 # Append
260266 writefile(['line3'], 'file.txt', 'a')
261- endif
262- echo lines
263-
267+ echo lines
264268 endif
265269catch /MyError/
266270 echo 'File not found'
267271endtry
268272
269273
270274####################################################
271- ## 4. Functions
275+ ## 4. Functions and Lambdas
272276####################################################
273277
274278# Basic function
@@ -300,10 +304,30 @@ var Add5 = MakeAdder(5)
300304# Call the closure with 3; result is 8
301305echo Add5(3)
302306
303- # A lambda function.
307+ # Lambdas in Vim9Script use `(args) => expr` syntax:
304308var Divide = (val: number, by: number): number => val / by
305309echo Divide(420, 10) # 42
306310
311+ # Sample list
312+ var nums = [1, 2, 3, 4, 5, 6]
313+
314+ # map(): Square each number
315+ var squares = map(copy(nums), (i, v) => v * v)
316+ echo squares # [1, 4, 9, 16, 25, 36]
317+
318+ # filter(): Keep even numbers
319+ var evens = filter(copy(nums), (i, v) => v % 2 == 0)
320+ echo evens # [2, 4, 6]
321+
322+ # reduce(): Sum all numbers
323+ var sum = reduce(copy(nums), (acc, v) => acc + v, 0)
324+ echo sum # 21
325+
326+ # sort(): Sort descending using lambda
327+ # Use `copy()` to avoid mutating the original list.
328+ var sorted = sort(copy(nums), (a, b) => b - a)
329+ echo sorted # [6, 5, 4, 3, 2, 1]
330+
307331####################################################
308332## 5. Classes and enums
309333####################################################
@@ -387,10 +411,9 @@ endtry
387411####################################################
388412
389413# Source guard (plugin pattern)
390- if exists('g:loaded_myplugin')
391- finish
392- endif
393- var g:loaded_myplugin = true
414+ if exists('g:loaded_myplugin') | finish | endif
415+ # Set to true to avoid sourcing the following code multiple times.
416+ # g:loaded_myplugin = true
394417
395418# Default value
396419var greeting = get(g:, 'myplugin_greeting', 'Hello')
@@ -427,8 +450,8 @@ augroup END
427450# This executes like pressing `ggddGp` in normal mode.
428451normal! ggddGp
429452
430- # `exist ({name})` checks if a variable, function, or command is defined.
431- echo exist (':myVariable') == 2
453+ # `exists ({name})` checks if a variable, function, or command is defined.
454+ echo exists (':myVariable') == 2
432455# `has({feature})` checks if Vim has a specific feature (e.g. `has('unix')`).
433456echo has('unix')
434457if has('nvim')
@@ -447,7 +470,7 @@ echo type(123) == v:t_number
447470####################################################
448471
449472# Run a shell command and capture output
450- var result = system('ls')
473+ silent result = system('ls')
451474echo result
452475
453476# Run and split into lines
@@ -495,6 +518,51 @@ def GitDiffQuickfix()
495518 copen
496519enddef
497520command! GitDiffQF call GitDiffQuickfix()
521+
522+ ####################################################
523+ ## 9. Debugging, Compiling and Inspecting Bytecode
524+ ####################################################
525+
526+ def MyFunc()
527+ var x = 10
528+ var y = x * 2
529+ echo y
530+ enddef
531+
532+ # To debug this function:
533+ # Add a breakpoint at line 3 of the function (line numbers are relative to the function)
534+ # :breakadd func MyFunc 3
535+
536+ # Then run the function in debug mode
537+ # :debug call MyFunc()
538+
539+ # While debugging, use commands like:
540+ # - :step to step into
541+ # - :next to step over
542+ # - :finish to run to end
543+ # - :cont to continue
544+ # - :echo to inspect variables
545+
546+ ## Listing Bytecode of a Function,
547+ ## Useful to understand how Vim optimizes Vim9Script functions
548+
549+ def MyMultiply(a: number, b: number): number
550+ return a * b
551+ enddef
552+
553+ # To compile and check for syntax/type errors:
554+ # Use :func MyMultiply to view the function definition
555+
556+ # To inspect the compiled bytecode:
557+ disassemble MyMultiply
558+
559+ # Example output:
560+ # <SNR>757_MyMultiply
561+ # return a * b
562+ # 0 LOAD arg[-2]
563+ # 1 LOAD arg[-1]
564+ # 2 OPNR *
565+ # 3 RETURN
498566```
499567
500568### Additional Resources
0 commit comments