Viml functions: Difference between revisions
(7 intermediate revisions by the same user not shown) | |||
Line 69: | Line 69: | ||
echo a:000 " list of all spread-arguments (max 20) | echo a:000 " list of all spread-arguments (max 20) | ||
" ... | " ... | ||
endfunc | |||
</source> | |||
<source lang="vim"> | |||
" pass all spread-args to another function | |||
function! Foo(...) | |||
echo a:0 | |||
endfunc | |||
function! Bar(...) | |||
call('Foo', a:000) " <-- expand spread arguments when calling function 'Foo' | |||
endfunc | endfunc | ||
</source> | </source> | ||
Line 82: | Line 93: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- parameters --> | </blockquote><!-- parameters --> | ||
= Visual Selection Range = | |||
<blockquote> | |||
Both the function and the command need special syntax to properly handle a visual selection range. | |||
<syntaxhighlight lang="vim"> | |||
function! OrgToMarkdown() range | |||
echo "firstline num " . a:firstline | |||
echo "lastline num" . a:lastline | |||
endfunc | |||
command -range OrgToMarkdown <line1>,<line2>:call OrgToMarkdown() | |||
</syntaxhighlight> | |||
</blockquote><!-- Visual Selection Range --> | |||
= Async = | |||
<blockquote> | |||
You can schedule background tasks within vim now.<br> | |||
They can be recurring, or scheduled after a period of seconds. | |||
<syntaxhighlight lang="vim"> | |||
function SayHi(timer) | |||
echom "hi" | |||
endfunc | |||
let timer = timer_start(2000, 'SayHi') " in 2s, run 'call SayHi(timer)' | |||
</syntaxhighlight> | |||
</blockquote><!-- Async --> | |||
= Anonymous Functions = | |||
<blockquote> | |||
Viml supports lambda expressions. | |||
<source lang="vim"> | |||
" define lambda expression | |||
let my_fn = {params -> some_other_function(params)} | |||
" call lambda expression | |||
call my_fn("abc") | |||
</source> | |||
</blockquote><!-- anonymous functions --> |
Latest revision as of 00:54, 17 March 2024
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 num_extra = a:0 " number of ... args let extraA = a:1 " (...) *args accessed by number let extraB = a:2 return "success" endfuncviml 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 withoutcall
command ResizeTabs call ResizeTabs() " make function into vim-command :ResizeTabsSee 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(name, ...) echo a:name echo a:0 " number of spread-arguments echo a:1 " 1st spread-argument echo a:2 " 2nd spread-argument echo a:000 " list of all spread-arguments (max 20) " ... endfunc" pass all spread-args to another function function! Foo(...) echo a:0 endfunc function! Bar(...) call('Foo', a:000) " <-- expand spread arguments when calling function 'Foo' endfuncYou can call a function with an array of variable parameters
function! Foo(...) echo a:0 endfunction call Foo("a", "b", "c") ">> '3' call call("Foo", ["a", "b", "c"]) ">> '3' -- parameters from array
Visual Selection Range
Both the function and the command need special syntax to properly handle a visual selection range.
function! OrgToMarkdown() range echo "firstline num " . a:firstline echo "lastline num" . a:lastline endfunc command -range OrgToMarkdown <line1>,<line2>:call OrgToMarkdown()
Async
You can schedule background tasks within vim now.
They can be recurring, or scheduled after a period of seconds.function SayHi(timer) echom "hi" endfunc let timer = timer_start(2000, 'SayHi') " in 2s, run 'call SayHi(timer)'
Anonymous Functions
Viml supports lambda expressions.
" define lambda expression let my_fn = {params -> some_other_function(params)} " call lambda expression call my_fn("abc")