Last Updated: June 24, 2016
·
136
· ryrych

Defining commands in Vim

Suppose that you have created a function that replaces <k>Ctrl</k> with <kbd>Ctrl</kbd> (this is useful in protips to save some time ;)

function! ReplaceKbd()
    " <kbd>Ctrl</kbd> => <kbd>Ctrl</kbd>
    :%s/<kbd>/<kbd>/ge
    :%s/<\/k>/<\/kbd>/ge
endfunction

In order to call that function you have to type :call ReplaceKbd(), which can soon become boring. Let’s save some key strokes by defining a command:

command ReplaceKbd :call ReplaceKbd()

Now you only have to type: :ReplaceKbd(). This is the same kind of a method like :GundoShow, :Gwrite and the like.