Define a Vim9 user function
Example
Normal 0 / 2Command-line 1 / 2Normal 2 / 2Define a Vim9 user function.
Script runtime
- Load state
not sourced
Command entered · ready to apply
Script runtime
- Load state
not sourced
Command line
- Typed text
:source twice-demo.vim- Insertion offset
22
:def appears in the complete source file. The def/enddef block defines a typed function; Twice(3) returns 6.
Script runtime
- Defined function
Twice(n: number): number- echo output
6
Before & after
Before
Script runtime
- Load state
not sourced
After
Script runtime
- Defined function
Twice(n: number): number- echo output
6
Documentation-backed illustration. Vim9 script and :def support; source code is prepared as twice-demo.vim. :def appears in the complete source file. The def/enddef block defines a typed function; Twice(3) returns 6. Primary help: :def.
vim9script
def Twice(n: number): number
return n * 2
enddef
echo Twice(3)Cursor on line 1, column 1Details & Vim help
Define a Vim9 user function. This Ex reference uses the full command name; the alphabetical index omits arguments. Supply the ranges, arguments, flags and prerequisite state documented in the linked help. Press Enter to submit a complete command. Availability and exact behavior can depend on Vim build features, options, mappings and editor state; the linked help is authoritative. Vim9 script constructs require the documented Vim9 script context.