The VimL Primer
Edit Like a Pro with Vim Plugins and Scripts
by Benjamin Klein
Version: P1.1 (February 2015)
Copyright 2015 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
The team that produced this book includes:
Lynn Beighley and Fahmida Y. Rashid (editor)
Candace Cunningham (copyeditor)
Dave Thomas (typesetter)
Janet Furlow (producer)
Ellie Callahan (support)
For international rights, please contact .
For the Best Reading Experience...
We strongly recommend that you read this book with the publisher defaults setting enabled for your reading device or application. Certain formats and characters may not display correctly without this setting. Please refer to the instructions for your reader on how to enable the publisher defaults setting.
Table of Contents
Copyright 2015, The Pragmatic Bookshelf.
Early Praise for The VimL Primer
Bens book is an eye-opener: Ive used Vim for years but ignored the power and flexibility it offers. Now Im 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 developers 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 level.
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 Neils Practical Vim, youve mastered all the magic of Vim, but with Ben Kleins fast-paced VimL Primer, its 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. |
An Introduction
The Worlds Shortest History Lesson
If youve used Vim for more than a couple of minutes, youre probably familiar with at least a few of its commands. To save a file, you run :w . When you want to exit the editor, you run :q .
Commands such as :w and :q are called Ex commands because they originated in Ex , the line-based editor. Pioneering computer scientist Bill Joy invented vi , a visual mode for Ex, in 1975. In the years since, vi has inspired newer editors and has been ported several times. Of these, the most popular and perhaps the most enduring editor is the one Im using to write this book: Vim.
In Vim, Ex commands can be run on the command line, but they also make up the bulk of Vims built-in scripting language, VimL . Recent Vim versions (notably, version 7) have added data types, functions, and many other common language features that together turn VimL into a highly capable scripting language. In this book, youll learn how to work with VimL.
Who Should Read This Book
This book is for Vim users who want to get started with VimL. I assume that youre familiar with how to use Vim for basic text editing. You dont have to be an expert Vim user, because this is an introduction, after all. VimL is not a wildly advanced topic if youre already comfortable with the editor. You just need to know your way around.
If youre not familiar with Vim buffers, windows, the command line, and modes, I suggest you try the Vim tutorial first. Its a splendid interactive tutorial for first-time Vim users.
This is also not a book on advanced Vim usage. For users looking to advance their Vim editing skills, I recommend Drew Neils .
How to Read This Book
We start with Chapter 1, , you learn how to write your own commands to run in the Vim command line and how to map keys to your plugins functionality.
Because of its project-based format, this is not a good book for skimming or jumping around. Its also not a good book to just read straight through. The goal is to code along! When we get to the end, youll not just have read through an introductory textbookyoull have written a fully functional Vim plugin.
Online Resources
You can download the code from this book from the Pragmatic Bookshelf website. Click on the Source code link on the books page. The code is broken into at least one directory for each chapter; the intro directory contains the example code from the first chapter, and the other directories contain the example code from each chapters version of the plugin.
Also on the books website is the forum. Click Discuss on the books page to ask questions, make comments, or just discuss the book with fellow readers and me. Youll also find a link to report errata; if you come across a problem in the code, something thats not explained clearly enough, or even a typo, head over there to report it.
And with that, lets be off!
Acknowledgments
Thanks to the entire Pragmatic Bookshelf team for letting me work with them. Dave Thomas, Andy Hunt, Susannah Pfalzer, Fahmida Y. Rashid, and all of the rest that Ive worked with have been unreasonably patient and very kind; particular thanks go to Susannah for answering all of my many questions, to Dave for bearing with my explanations of some of the joyous oddities in VimL syntax, and especially to Fahmida for keeping this project on track in spite of my constant mangling of schedules.