@@ -131,6 +131,12 @@ echo "The year is 2025" =~ '[[:digit:]]\+$' # true
131
131
echo 'abc123' =~ '\v\d+' # true
132
132
echo 'a|b' =~ '\Va|b' # true
133
133
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
+
138
+ echo matchstr(email, '@\zs[^ ]\+\ze') # Output: 'example.com'
139
+
134
140
135
141
####################################################
136
142
## 2. Variables
@@ -258,17 +264,15 @@ try
258
264
var lines = readfile('file.txt')
259
265
# Append
260
266
writefile(['line3'], 'file.txt', 'a')
261
- endif
262
- echo lines
263
-
267
+ echo lines
264
268
endif
265
269
catch /MyError/
266
270
echo 'File not found'
267
271
endtry
268
272
269
273
270
274
####################################################
271
- ## 4. Functions
275
+ ## 4. Functions and Lambdas
272
276
####################################################
273
277
274
278
# Basic function
@@ -300,10 +304,30 @@ var Add5 = MakeAdder(5)
300
304
# Call the closure with 3; result is 8
301
305
echo Add5(3)
302
306
303
- # A lambda function.
307
+ # Lambdas in Vim9Script use `(args) => expr` syntax:
304
308
var Divide = (val: number, by: number): number => val / by
305
309
echo Divide(420, 10) # 42
306
310
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
+
307
331
####################################################
308
332
## 5. Classes and enums
309
333
####################################################
@@ -390,7 +414,7 @@ endtry
390
414
if exists('g:loaded_myplugin')
391
415
finish
392
416
endif
393
- var g:loaded_myplugin = true
417
+ g:loaded_myplugin = true
394
418
395
419
# Default value
396
420
var greeting = get(g:, 'myplugin_greeting', 'Hello')
@@ -427,8 +451,8 @@ augroup END
427
451
# This executes like pressing `ggddGp` in normal mode.
428
452
normal! ggddGp
429
453
430
- # `exist ({name})` checks if a variable, function, or command is defined.
431
- echo exist (':myVariable') == 2
454
+ # `exists ({name})` checks if a variable, function, or command is defined.
455
+ echo exists (':myVariable') == 2
432
456
# `has({feature})` checks if Vim has a specific feature (e.g. `has('unix')`).
433
457
echo has('unix')
434
458
if has('nvim')
@@ -495,6 +519,53 @@ def GitDiffQuickfix()
495
519
copen
496
520
enddef
497
521
command! GitDiffQF call GitDiffQuickfix()
522
+
523
+ ####################################################
524
+ ## 9. Debugging, Compiling and Inspecting Bytecode
525
+ ####################################################
526
+
527
+ def MyFunc()
528
+ var x = 10
529
+ var y = x * 2
530
+ echo y
531
+ enddef
532
+
533
+ # To debug this function:
534
+ # Add a breakpoint at line 3 of the function (line numbers are relative to the function)
535
+ # :breakadd func MyFunc 3
536
+
537
+ # Then run the function in debug mode
538
+ # :debug call MyFunc()
539
+
540
+ # While debugging, use commands like:
541
+ # - :step to step into
542
+ # - :next to step over
543
+ # - :finish to run to end
544
+ # - :cont to continue
545
+ # - :echo to inspect variables
546
+
547
+ ## Listing Bytecode of a Function,
548
+ ## Useful to understand how Vim optimizes Vim9Script functions
549
+
550
+ def MyMultiply(a: number, b: number): number
551
+ return a * b
552
+ enddef
553
+
554
+ # To compile and check for syntax/type errors:
555
+ # Use :func MyMultiply to view the function definition
556
+
557
+ # To inspect the compiled bytecode:
558
+ # :disassemble MyMultiply
559
+
560
+ # Example output:
561
+ # func MyMultiply(a: number, b: number): number
562
+ # 1 return a * b
563
+ # Disassembly of MyMultiply:
564
+ # 0000 PUSHARG a
565
+ # 0001 PUSHARG b
566
+ # 0002 MULTIPLY
567
+ # 0003 RETURN
568
+ vim9script
498
569
```
499
570
500
571
### Additional Resources
0 commit comments