:function Number()
: echo "line " .. line(".") .. " contains: " .. getline(".")
:endfunction
If you call this function with: >
:10,15call Number()
The function will be called six times.
VARIABLE NUMBER OF ARGUMENTS
Vim enables you to define functions that have a variable number of arguments.
The following command, for instance, defines a function that must have 1
argument (start) and can have up to 20 additional arguments: >
:function Show(start, ...)
The variable "a:1" contains the first optional argument, "a:2" the second, and
so on. The variable "a:0" contains the number of extra arguments.
For example: >
:function Show(start, ...)
: echohl Title
: echo "start is " .. a:start
: echohl None
: let index = 1
: while index <= a:0
: echo " Arg " .. index .. " is " .. a:{index}
: let index = index + 1
: endwhile
: echo ""
:endfunction
This uses the ":echohl" command to specify the highlighting used for the
following ":echo" command. ":echohl None" stops it again. The ":echon"
command works like ":echo", but doesn't output a line break.
You can also use the a:000 variable, it is a List of all the "..." arguments.
See |a:000|.
LISTING FUNCTIONS
The ":function" command lists the names and arguments of all user-defined
functions: >
:function
< function Show(start, ...) ~
function GetVimIndent() ~
function SetSyn(name) ~
To see what a function does, use its name as an argument for ":function": >
:function SetSyn
< 1 if &syntax == '' ~
2 let &syntax = a:name ~
3 endif ~
endfunction ~
DEBUGGING
The line number is useful for when you get an error message or when debugging.
See |debug-scripts| about debugging mode.
You can also set the 'verbose' option to 12 or higher to see all function
calls. Set it to 15 or higher to see every executed line.
DELETING A FUNCTION
To delete the Show() function: >
:delfunction Show
You get an error when the function doesn't exist.
FUNCTION REFERENCES
Sometimes it can be useful to have a variable point to one function or
another. You can do it with the function() function. It turns the name of a
function into a reference: >
:let result = 0 " or 1
:function! Right()
: return 'Right!'
:endfunc
:function! Wrong()
: return 'Wrong!'
:endfunc
:
:if result == 1
: let Afunc = function('Right')
:else
: let Afunc = function('Wrong')
:endif
:echo call(Afunc, [])
< Wrong! ~
Note that the name of a variable that holds a function reference must start
with a capital. Otherwise it could be confused with the name of a builtin
function.
The way to invoke a function that a variable refers to is with the call()
function. Its first argument is the function reference, the second argument
is a List with arguments.
Function references are most useful in combination with a Dictionary, as is
explained in the next section.
More information about defining your own functions here: |user-function|.
==============================================================================
*41.8* Lists and Dictionaries
So far we have used the basic types String and Number. Vim also supports two
composite types: List and Dictionary.
A List is an ordered sequence of things. The things can be any kind of value,
thus you can make a List of numbers, a List of Lists and even a List of mixed
items. To create a List with three strings: >
:let alist = ['aap', 'mies', 'noot']
The List items are enclosed in square brackets and separated by commas. To
create an empty List: >
:let alist = []
You can add items to a List with the add() function: >
:let alist = []
:call add(alist, 'foo')
:call add(alist, 'bar')
:echo alist
< ['foo', 'bar'] ~
List concatenation is done with +: >
:echo alist + ['foo', 'bar']
< ['foo', 'bar', 'foo', 'bar'] ~
Or, if you want to extend a List directly: >
:let alist = ['one']
:call extend(alist, ['two', 'three'])
:echo alist
< ['one', 'two', 'three'] ~
Notice that using add() will have a different