Writing User Commands

At this point you probably won’t be shocked to learn how we define our own commands: by using a command! Specifically, we use :command to define commands. (Hmm—that certainly sounds redundant.)

User commands, like user functions, have to start with uppercase letters. And what’s funny about user commands is that their job is simply to call built-in commands. If we want to define a command that calls a function—as we will be doing—we execute the :call command. Consider an example:

mappings/command.vim
 
function​! EchoQuote()
 
echo ​'A poet can but ill spare time for prose.'
 
endfunction
 
command​ Quote ​call​ EchoQuote()

This defines a user command called :Quote. Just like with :command itself, we can leave the colon ...

Get The VimL Primer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.