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

Classes

While Vim doesn't explicitly contain classes, dictionaries support having methods on them, supporting the object-oriented programming paradigm. There are two ways to define a method on a dictionary.

Given an existing dictionary, animal_names:


let animal_names = {  \ 'cat': 'Miss Cattington',  \ 'dog': 'Mr Dogson',  \ 'parrot': 'Polly'  \ }

You could do the following to add a method to it:


function animal_names.GetGreeting(animal)  return self[a:animal] . ' says hello'endfunction

You can now execute the function:


:echo animal_names.GetGreeting('cat')Miss Cattington says hello

You can use self (just like in Python!) to refer to dictionary keys.

In the previous example, GetGreeting becomes a callable dictionary key. Effectively, ...

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