O'Reilly logo

Mastering Vim by Ruslan Osipov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functions

Just like most other programming languages, Vim supports functions:

function AnimalGreeting(animal)  echo a:animal . ' says hello!'endfunction
In Vim, user-defined function names must start with a capital letter (unless they're within a script scope or behind a namespace). You'll get an error if you try to define a function starting with a lowercase letter.

You can try calling the function and you'll get the following output:

:call AnimalGreeting('cat')
cat says hello!

You can see that function arguments are accessed via the a: scope.

Functions, of course, can return values:

function! AnimalGreeting(animal)  return a:animal . ' says hello!'endfunction
Something to keep in mind is that in Vim a single script can get loaded multiple ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required