Functions

Vimscript functions must start with a capital letter if they are unscoped! (Function name must start with a capital or contain a colon:)

Function define

:function CallSean()

: echom "Hi sean"

:endfunction

call CallSean()

output: "Hi sean"

:function GetSean()

: return "Basket"

:endfunction

call GetSean()

output : nothing

echom GetSean()

output : Basket

Implicit Returning

echom CallSean()

output: "Hi sean" 0

     if a Vimscript function doesn't return a value, it implicitly returns 0. 

Now check this case

:function TextwidthIsTooWide()

:  if &l:textwidth ># 80

:    return 1

:  endif

:endfunction

Test case1

:set textwidth=80
:if TextwidthIsTooWide()
:  echom "WARNING: Wide text!"
:endif

output: (function return 0)

Test case2

:setlocal textwidth=100
:if TextwidthIsTooWide()
:  echom "WARNING: Wide text!"
:endif

output: "WARNING: Wide text!" (function return 1)

Exercises

Read :help :call. Ignore anything about "ranges" for now. How many arguments can you pass to a function? Is this surprising?

Read the first paragraph of :help E124 and find out what characters you're allowed to use in function names. Are underscores okay? Dashes? Accented characters? Unicode characters? If it's not clear from the documentation just try them out and see.

Read :help return. What's the "short form" of that command (which I told you to never use)? Is it what you expected? If not, why not?

Function Arguments

:help internal-variables

Last updated

Was this helpful?