Skip to content

Commit 26c8ec3

Browse files
committed
address @girishji 's suggestion
1 parent f805b92 commit 26c8ec3

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

vim9script.md

+79-8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ echo "The year is 2025" =~ '[[:digit:]]\+$' # true
131131
echo 'abc123' =~ '\v\d+' # true
132132
echo '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 = '[email protected]'
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
265269
catch /MyError/
266270
echo 'File not found'
267271
endtry
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
301305
echo Add5(3)
302306
303-
# A lambda function.
307+
# Lambdas in Vim9Script use `(args) => expr` syntax:
304308
var Divide = (val: number, by: number): number => val / by
305309
echo 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
####################################################
@@ -390,7 +414,7 @@ endtry
390414
if exists('g:loaded_myplugin')
391415
finish
392416
endif
393-
var g:loaded_myplugin = true
417+
g:loaded_myplugin = true
394418
395419
# Default value
396420
var greeting = get(g:, 'myplugin_greeting', 'Hello')
@@ -427,8 +451,8 @@ augroup END
427451
# This executes like pressing `ggddGp` in normal mode.
428452
normal! ggddGp
429453
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
432456
# `has({feature})` checks if Vim has a specific feature (e.g. `has('unix')`).
433457
echo has('unix')
434458
if has('nvim')
@@ -495,6 +519,53 @@ def GitDiffQuickfix()
495519
copen
496520
enddef
497521
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
498569
```
499570

500571
### Additional Resources

0 commit comments

Comments
 (0)