Skip to content

Commit ae5c07e

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

File tree

1 file changed

+83
-16
lines changed

1 file changed

+83
-16
lines changed

vim9script.md

Lines changed: 83 additions & 16 deletions
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 = '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
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
####################################################
@@ -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
396419
var greeting = get(g:, 'myplugin_greeting', 'Hello')
@@ -427,8 +450,8 @@ augroup END
427450
# This executes like pressing `ggddGp` in normal mode.
428451
normal! 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')`).
433456
echo has('unix')
434457
if 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')
451474
echo result
452475
453476
# Run and split into lines
@@ -458,11 +481,11 @@ endfor
458481
459482
# output files in folder of current file
460483
# ... to non-interactive shell
461-
:!ls %:h:S
484+
!ls %:h:S
462485
# ... to current buffer, replacing its contents
463486
:%!ls %:h:S
464-
# ... to current buffer, appending at cursor position
465-
:.read!ls %:h:S
487+
# ... to the same buffer, appending at cursor position
488+
:.read! ls %:h:S
466489
467490
# Using job_start() callback
468491
var shell_job: job
@@ -474,7 +497,6 @@ shell_job = job_start(["/bin/sh", "-c", "ls"], {
474497
out_cb: GotOutput,
475498
err_cb: GotOutput
476499
})
477-
478500
# Check exit status
479501
echo v:shell_error
480502
@@ -495,6 +517,51 @@ def GitDiffQuickfix()
495517
copen
496518
enddef
497519
command! GitDiffQF call GitDiffQuickfix()
520+
521+
####################################################
522+
## 9. Debugging, Compiling and Inspecting Bytecode
523+
####################################################
524+
525+
def MyFunc()
526+
var x = 10
527+
var y = x * 2
528+
echo y
529+
enddef
530+
531+
# To debug this function:
532+
# Add a breakpoint at line 3 of the function (line numbers are relative to the function)
533+
# :breakadd func MyFunc 3
534+
535+
# Then run the function in debug mode
536+
# :debug call MyFunc()
537+
538+
# While debugging, use commands like:
539+
# - :step to step into
540+
# - :next to step over
541+
# - :finish to run to end
542+
# - :cont to continue
543+
# - :echo to inspect variables
544+
545+
## Listing Bytecode of a Function,
546+
## Useful to understand how Vim optimizes Vim9Script functions
547+
548+
def MyMultiply(a: number, b: number): number
549+
return a * b
550+
enddef
551+
552+
# To compile and check for syntax/type errors:
553+
# Use :func MyMultiply to view the function definition
554+
555+
# To inspect the compiled bytecode:
556+
disassemble MyMultiply
557+
558+
# Example output:
559+
# <SNR>757_MyMultiply
560+
# return a * b
561+
# 0 LOAD arg[-2]
562+
# 1 LOAD arg[-1]
563+
# 2 OPNR *
564+
# 3 RETURN
498565
```
499566

500567
### Additional Resources

0 commit comments

Comments
 (0)