Chapter 12. Vim Scripts
Sometimes customization alone isn’t enough for your editing environment. Vim lets you define all of your favorite settings in your .vimrc file, but maybe you want more dynamic or “just in time” configuration. Vim scripts let you do that.
From inspecting buffer contents to handling unanticipated external factors, Vim’s scripting language lets you complete complex tasks and make decisions based on your needs.
If you have a Vim configuration file (.vimrc, .gvimrc, or both),
you are already scripting in Vim; you just don’t know it. All
of the Vim commands and options are valid inputs to scripts. And,
as you’d expect, Vim provides all of the standard flow control statements
(if…then…else
, while
, etc.), variables, and functions typical
in any language.
In this chapter, we’ll walk through an example and incrementally build up a script. We’ll look at simple constructs, use some of Vim’s built-in functions, and examine rules you must consider in order to write well-behaved and predictable Vim scripts.
What’s Your Favorite Color (Scheme)?
Let’s begin with the simplest of configurations. We’ll customize our environment to a color scheme we prefer. This is simple and uses one of the basics of Vim scripts, the simple Vim command.
Vim ships with 17 customized color schemes.1 You can choose and activate
a color scheme by putting the colorscheme
command in your .vimrc
or .gvimrc file. A favorite “understated” color scheme
of one author is the desert scheme:
:
colorscheme ...
Get Learning the vi and Vim Editors, 8th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.