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
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
About This Title
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
Read the reviews .
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
Brought to You By
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.