Build on your editor’s capabilities and tailor your editing experience with VimL, the powerful scripting language built into Vim. With VimL you can configure basic settings or add entirely new functionality. Use this quick and easy introduction to create your own Vim plugin while learning the concepts and syntax of VimL.
The VimL Primer: Edit Like a Pro with Vim Plugins and Scripts
by Benjamin Klein
The VimL Primer
Edit Like a Pro with Vim Plugins and Scripts
by Benjamin Klein
With Drew Neil’s Practical Vim, you’ve mastered all the magic of Vim, but with Ben Klein’s fast-paced VimL Primer, it’s time you learned how to write your own spells and plugins, with the VimL language wand, like a pro!
— Guillaume Laforge, Groovy project lead
Ben’s book is an eye-opener: I’ve used Vim for years but ignored the power and
flexibility it offers. Now I’m paying attention. The VimL Primer is a gentle,
thoughtful introduction to a new world for Vim users.
- Michael Easter
Software developer, ScreenScape Networks
Vim is an incredibly useful tool in any developer’s toolkit, and Ben Klein offers
an easy-to-read, helpful, and at-times-witty guide to scripting it with VimL. A
must-read for all Vim-using developers.
- Joshua Scott
Managing partner, Resonant Media Technologies, LLC
The VimL Primer gets straight to the point and shows you the ropes for dealing
with Vim plugins. Much like Vim itself, this book communicates a lot of detail efficiently
and effectively. This book can help you take your Vim skills to the next
- Kevin Munc
Founder, Method Up LLC
The VimL Primer does an incredible job of showing you how to take one of the
most enduring text editors and extend it so that it becomes even more useful. Do
you want to bend Vim to your will? This is where you start!
- Jared Richardson
Principal Consultant, Agile Artisans, Inc.
With Drew Neil’s Practical Vim, you’ve mastered all the magic of Vim, but with
Ben Klein’s fast-paced VimL Primer, it’s time you learned how to write your own
spells and plugins, with the VimL language wand, like a pro!
- Guillaume Laforge
Groovy project lead
Everything you need to start working on the next popular Vim plugin.
- Mac Liaw
CTO, CylaTech.com, Inc.
About this Title
Release: P1.1 (2015-02-19)
VimL is the scripting language of the Vim editor. If you’ve ever edited or saved a vimrc file, you’ve written VimL. And VimL can do much more than simply configure settings and specify option values—you can write entire plugins in VimL. But without a background in scripting Vim, it can be hard to know where to start.
The VimL Primer gives you the tools and confidence you need. It gets you comfortable in VimL quickly, walking you through creating a working plugin that you can run yourself as you write it in Vim. You’ll learn how to script common commands and buffer interaction, work with windows and buffers from within a plugin script, and how to use autocommands to have Vim recognize entirely new filetypes. You’ll discover how to declare filetype-specific settings and define your own syntax elements for use with Vim’s syntax highlighting. And you’ll see how you can write your own command-line commands and define new mappings to call them.
With this introduction to scripting Vim, your own Vim extensions are only plugins away. Take control of your editor!
Q&A with author Ben Klein:
1. I don’t want to write plugins. Why should I care about VimL?
Not so fast, there! In fact, you’re executing lines of VimL each time you run a command on Vim’s command line (as we discuss in Chapter 1). If you have a Vim configuration file – .vimrc – you have a VimL script that Vim is reading every time it launches.
You don’t have to write a plugin as such to benefit from VimL. (And once you’re comfortable with scripting, you might eventually come to separate scripts into a plugin for your own use, but if you do, you don’t necessarily have to distribute it.) The concepts and techniques you’ll learn in this book will aid you in customizing or extending Vim, all plugins aside.
2. There’s no such thing as VimL! What is this book about?
This book is about Vim’s built-in scripting language, VimL. This language is also known as Vimscript. Depending on how you look at it, either VimL is an alternate name for Vimscript or Vimscript is an alternate name for VimL.
Actually, there’s no real official name for the language; the closest seems to be the two-word “Vim script.” To better follow English naming conventions, this is usually altered to “Vimscript,” or more rarely, “Vim Script”—but all of this can be confusing, since the files which store code in this language are themselves called “Vim scripts.”
The relatively new name “VimL” (“Vim Language”) has been gaining in popularity in rough correlation with the growth of the code-sharing site GitHub. Its use is a matter of preference, but I do find it more easily distinguishable from mentions of Vim scripts or of writing generic scripts using Vim (in search results, for instance). It’s a short, tolerably memorable name, a bit catchy, and what I use throughout this book.
3. Does this book cover Vim scripting in Python or Ruby?
That’s the short answer. The long answer is that Vim does let you script it in common languages such as Python, but also that when you do this, you still have to work with a VimL interface (in the Python example, at the very least you’ll need some VimL that loads the Python code). It can only help to have some VimL background before you attempt to do this.
This book is an introduction to scripting Vim. For that, the official (default) language is VimL, and that’s what this book introduces.
4. Can I get the plugin from the book somewhere?
Absolutely! You can get the current version of the plugin from GitHub. That project starts with the code from the book (beginning with how we leave the plugin at the end of Chapter 6) and will build on it. Feel free to fork it, send pull requests, create issues, or do any number of the other cool things GitHub lets you do with it there.
If you have any comments, questions, suggestions, criticisms, or debts of unending gratitude regarding the plugin code or the book, you can let me know on Twitter: @fifthposition
What You Need
Vim version 7 or later is required, and it’s available on any of the major operating systems. This book uses the “Huge” version of Vim 7.4.
Contents & Extracts
- An Introduction
- The World’s Shortest History Lesson
- Who Should Read This Book
- How to Read This Book
- Online Resources
- The Lay of the Land excerpt
- Functions, Types, and Variables
- Loops and Comparisons
- Our Project: An Interface for mpc
- The Structure of a Vim Plugin
- A Real Live Plugin excerpt
- But First, a Function
- Running External Commands
- Writing Text to a Buffer
- The Autoload System
- Autoloading Functions
- Finding Windows by Buffers
- The Built-in Buffer Functions
- Retrieving the Text of a Line
- Recognizing File Types
- Autocommands and Their Events
- Detecting the Current File Type
- Making Filetype-Specific Changes
- Highlighting Syntax excerpt
- The Vim Syntax File
- Using conceal with Syntax Regions
- Specifying a New Syntax
- Commands and Mappings
- Writing User Commands
- Adding Mappings
- Localizing Mappings
- In Conclusion
- Some Resources
Benjamin Klein is a software developer at Silver Chalice, LLC, where he works with Grails and uses Vim for anything text-based that he can. He has been a longtime writer for GroovyMag and a contributing author to Grails 2: A Quick-Start Guide.