expression is true
assert_exception() assert that a command throws an exception
assert_beeps() assert that a command beeps
assert_nobeep() assert that a command does not cause a beep
assert_fails() assert that a command fails
assert_report() report a test failure
Timers: *timer-functions*
timer_start() create a timer
timer_pause() pause or unpause a timer
timer_stop() stop a timer
timer_stopall() stop all timers
timer_info() get information about timers
wait() wait for a condition
Tags: *tag-functions*
taglist() get list of matching tags
tagfiles() get a list of tags files
gettagstack() get the tag stack of a window
settagstack() modify the tag stack of a window
Prompt Buffer: *promptbuffer-functions*
prompt_getprompt() get the effective prompt text for a buffer
prompt_setcallback() set prompt callback for a buffer
prompt_setinterrupt() set interrupt callback for a buffer
prompt_setprompt() set the prompt text for a buffer
Registers: *register-functions*
getreg() get contents of a register
getreginfo() get information about a register
getregtype() get type of a register
setreg() set contents and type of a register
reg_executing() return the name of the register being executed
reg_recording() return the name of the register being recorded
Context Stack: *ctx-functions*
ctxget() return context at given index from top
ctxpop() pop and restore top context
ctxpush() push given context
ctxset() set context at given index from top
ctxsize() return context stack size
Various: *various-functions*
mode() get current editing mode
visualmode() last visual mode used
exists() check if a variable, function, etc. exists
has() check if a feature is supported in Vim
changenr() return number of most recent change
did_filetype() check if a FileType autocommand was used
eventhandler() check if invoked by an event handler
getpid() get process ID of Vim
getscriptinfo() get list of sourced Vim scripts
getstacktrace() get current stack trace of Vim scripts
libcall() call a function in an external library
libcallnr() idem, returning a number
undofile() get the name of the undo file
undotree() return the state of the undo tree for a buffer
shiftwidth() effective value of 'shiftwidth'
wordcount() get byte/word/char count of buffer
luaeval() evaluate |Lua| expression
py3eval() evaluate |Python| expression
pyeval() evaluate |Python| expression
pyxeval() evaluate |python_x| expression
rubyeval() evaluate |Ruby| expression
debugbreak() interrupt a program being debugged
==============================================================================
*41.7* Defining a function
Vim enables you to define your own functions. The basic function declaration
begins as follows: >
:function {name}({var1}, {var2}, ...)
: {body}
:endfunction
<
Note:
Function names must begin with a capital letter.
Let's define a short function to return the smaller of two numbers. It starts
with this line: >
:function Min(num1, num2)
This tells Vim that the function is named "Min" and it takes two arguments:
"num1" and "num2".
The first thing you need to do is to check to see which number is smaller:
>
: if a:num1 < a:num2
The special prefix "a:" tells Vim that the variable is a function argument.
Let's assign the variable "smaller" the value of the smallest number: >
: if a:num1 < a:num2
: let smaller = a:num1
: else
: let smaller = a:num2
: endif
The variable "smaller" is a local variable. Variables used inside a function
are local unless prefixed by something like "g:", "a:", or "s:".
Note:
To access a global variable from inside a function you must prepend
"g:" to it. Thus "g:today" inside a function is used for the global
variable "today", and "today" is another variable, local to the
function.
You now use the ":return" statement to return the smallest number to the user.
Finally, you end the function: