Viml functions

From wikinotes
Revision as of 22:44, 18 June 2020 by Will (talk | contribs) (→‎Parameters)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Basics

All vim Functions must start with a captial letter (an underscore is also valid)

function ResizeTabs(size, num, ...)   " (...) indicates accepts *args
    let size = a:size                 " args must be saved to variable in order to be used
    let num = a:num
    let extraA = a:1                  " (...) *args accessed by number
    let extraB = a:2
    return "success"
endfunc

viml functions are cannot be called directly from the vim's command-mode, you must call them using the keyword call

:call ResizeTabs("four", 4, "abc", "efg")

You may also define a command for your function, allowing it to be used without call

command ResizeTabs call ResizeTabs()  " make function into vim-command

:ResizeTabs

See Also viml commands .

Public/Private

vim's version of private-functions are script-local scoped functions. They have a prefix of s:.

function Public()
    " ...
endfunc

function s:Private()
    " ...
endfunc

Parameters

function! PrintArgs(text)
    echo a:text   " arguments are scoped with 'a:...'
endfunc

call PrintArgs("mytext")

You may define a variable number of args (like python's *args using ....

function! VarArgs(...)
    echo a:1
    echo a:2
    echo a:000  " list of all arguments (max 20)
    " ...
endfunc