s:
(script local functions), or they must be prefixed with the name associated to the autoload plugin where they are defined (e.g. in {&rtp}/autoload/foo/bar.vim
we could define foo#bar#functionname()
).a:parameter_name
. Variadic functions can be defined with the ellipsis ...
, to access the parameters use a:000
(list of all parameters), or a:0
(number of parameters equal to len(a:000)
), a:1
first unnamed parameters, and so on.:call MyFunction(param1, param2)
:
, thus all the commands are colon commandsabort
function! MyFunction(foo, bar, ... ) abort
return a:foo . a:bar . (a:0 > 0 ? a:1 : '')
endfunction
If you only plan on using your function in the file where it's defined (either because you've broken a bigger function in smaller parts, or because you'll use it in a command, a mapping, ...), you can prefix it with s:
, avoiding littering your global namespace with useless internal functions:
function! s:my_private_function() " note we don't need to capitalize the first letter this time
echo "Hi!"
endfunction
If your script local function is going to be used in a mapping, you need to reference it using the special <SID>
prefix:
nnoremap <your-mapping-key> :call <SID>my_private_function()<CR>
See :help user-functions
.
Note however, that since Vim 7, it's considered a best practice to define mappings abbreviations, commands and menus in (ft)plugins, and defining functions in autoload plugins -- except the functions the plugins need to use when they're loaded. This means that nowadays the need to call scripts local functions from mappings is not as pertinent as what it used to be.