A Semi-Radical Argument for the VIM Text Editor

Erik Leon
Oct 25, 2018

To many (perhaps to most), VIM is more a meme than a text editor. It has a reputation of being incredibly difficult to learn, with the simplest of tasks requiring arcane commands and combinations of keystrokes. The internet is replete with jokes about how difficult it is to do anything with VIM, including something as fundamental as closing the program.

For those who choose to look past the reputation and try to learn to use VIM, the first thing they encounter is that it’s a command-line application. Command line? What is this, 1975? It seems archaic.

They open the program and try to type something only to find that they can’t write anything on the screen (unless they happen to type certain letters that trigger “Insert” mode). It seems unintuitive.

They trudge on and find endless articles and long cheat-sheets with a seemingly infinite number of commands they need to memorize. It seems daunting.

Archaic, unintuitive, daunting, why bother? Why not just use Atom/Sublime/WebStorm/(insert preferred text-editor/IDE here)?

My goal here is not to convince you to use VIM (I hope you’ll be convinced to at least try it, but I’m not holding my breath), but to explain why it’s my preferred editor and how I use its powerful features in my day-to-day life as a web developer. Perhaps I’m crazy and just want to reassure myself that there are at least some good reasons for putting myself through this. Either way, let’s get into it.

 

1. It’s Free and Open-Source

VIM is a free and open-source text editor with a GPL-compatible license, meaning it is not only free to use, but also free for you to modify, extend, and share. The code is available for anyone to view on Github. Anyone can contribute to the project: all they have to do is fork the repository, make their changes, and make a pull request to the main project (and hope it gets approved). I’m not going to get into extolling all the virtues of open-source software in general (maybe that’ll be another blog post) but just think: this piece of software is the work of many people over many years working together for no other reason than to make this tool better. This is not a product created by a company that’s trying to sell you something (or sell others your data), it’s the product of a community bound by a shared interest. I think that’s pretty neat.

 

2. It’s Lightweight

For how complicated and immensely powerful it is, VIM is very light on resource usage, with a memory footprint of only about 20Kb as I’m writing this. You read that right, kilobytes, not megabytes. The amount of memory it requires for loading even very large files is minuscule compared to other editors. Have a giant log file to read? Maybe a database dump? VIM is sure to handle opening and editing such a file quickly while heavier editors choke and slow to a crawl. This is also useful when you have other large programs that require lots of resources, like a browser, webserver, database, virtual machine, or Docker (or all of the above). Allocating fewer resources to your editor means those other programs have more to use, which should allow for better performance.

 

3. It’s Ubiquitous

VIM is a fork of VI, a program from the earliest days of UNIX. VI was originally written in 1976, and, due to its popularity in these early days, almost all UNIX-derived operating systems come with a version of VI or VIM installed. This means all flavors of Linux and even MacOS. (Yes, it’s on your Mac. Really. Open up Terminal, type “vi”, hit Enter, and see what happens). Since almost all web servers run Linux (some surveys have the number above 90%) and as developers we occasionally need to SSH into a server to look at logs or debug things, it’s nice to know there is a powerful tool at your disposal should you need it.

 

4. It has a Huge Community

VIM is designed to be easy to extend, and there is a giant community of developers that have created custom scripts and plugins for VIM, adding all kinds of functionality, from simple tasks like syntax highlighting for different programming languages to more complex, IDE-like features such as step-through debugging and intelligent auto-completion. There are even plugins that manage your plugins! So, if you find yourself in need of a bit of functionality the VIM doesn’t offer, there’s a good chance that there is a plugin out there to meet that need. For example, I have a full-fledged debugger that I use for PHP (Vdebug), a visual file-manager (NERDTree), and intelligent completion and go-to definition (Deoplete, PHP Language Server). While we’re discussing plugins, let me just point out that VIM is highly customizable. You can take full control over how it works, from key mappings to color schemes to plugins.

 

5. It’s Powerful

This is, obviously, the biggest reason I use this weird, hard-to-learn text editor that most people have never heard of. It is amazingly powerful if you take the time to learn its secrets. And there are many, many secrets. I’m about 3 or 4 years into using VIM as my full-time editor and I still have barely scratched the surface, but don’t let that intimidate you (well, maybe you can be a little intimidated), it only takes a short time to get up and running with the basics. The learning curve is steep compared to other editors, but the rewards are incalculable. Let me explain some of the ways VIM is more powerful and versatile than other editors.

 

Modal Editing

What most sets VIM apart from other editors is its multiple modes. In most editors, there is 1 mode, in VIM, there are 6, each with a slew of features. Here is a brief rundown of the 4 I use the most:

 

  1. Normal mode — This is the mode VIM starts in when it opens. You can navigate and manipulate text in a variety of ways in normal mode, including things like copying, cutting, and pasting.

 

  1. Insert mode — This is the mode most similar to most editors, allowing you to type in new text.

 

  1. Visual mode — This mode allows you to select blocks of text, much like what happens when you click and drag over text in most editors.

 

  1. Command-line mode — This mode allows you to enter VIM commands, for example, “q” to quit. This is perhaps the most powerful mode, as it allows access to the vast array of commands VIM offers.

 

The benefit of modes is it allows for a lot of functionality to be accessible with just a few keystrokes. Like I mentioned, most editors have 1 mode. All extra functionality is hidden behind menus, which usually require taking your hand off the keyboard and fiddling with the mouse. But wait, that’s what keyboard shortcuts are for, right? Yes, exactly. VIM basically takes the idea of keyboard shortcuts and takes it to the extreme: everything is accessed via the keyboard, you don’t need to touch the mouse at all. There are no menus to click through, just commands to memorize. This keeps your fingers where they belong: on the keys. This makes you faster, more efficient, and more easily concentrated on the task of writing code (or anything else). It is easier to have these functions laid out in a menu structure? Yes. Is “easier” necessarily better? Absolutely not. In the same way you would take the time to learn the keyboard shortcuts for your most used functions, you take the time to learn VIM commands to make your text editing easier and better.

 

Everything is a command

Every action you do in VIM is a command, and commands are composable, modifiable, and reusable. I’ll illustrate with an example:

 

  • Composability — In Normal mode, pressing the “c” key initiates a “change” command, which basically means you’re going to delete some text and replace it with new text. The “w” key will advance the cursor to the next word. So, in order to (c)hange a (w)ord, we use “cw”. This will delete the text between the cursor and the start of the next word, and then put you into “Insert” mode so you can type in new text. So, what we’ve just done is combined two commands, “change” and “move to the next word”.

 

  • Modifiable — Let’s say we want to change multiple words. Perhaps, instead of one word, we want to replace the next 3. Easy: “c3w”. Here, we modified how we used the “w” command so that it would advance 3 words ahead instead of just 1, and then we combined it with “c” like before.

 

  • Reusable — When you complete an action in VIM, such as our “c3w” example, we can repeat that action using the “.” key. So, if there are multiple places where we need to change 3 words, we can do “w3c” once, navigate to the next spot, and press “.”. Further, we can save commands to arbitrary registers, which allows us to easily build sets of custom shortcuts on-the-fly. More on that next.

 

Macros

VIM makes it very easy to record and use macros. In case you don’t know, a macro is basically a set of actions that you record and can then repeat. So, anytime you have a set of actions you need to perform multiple times, whether it be in one file or in multiple files, you don’t have to waste time repeating yourself.

 

Here’s an example: I frequently write interface files that are then implemented, concrete classes. When I got to implement the concrete class, I like to start with the interface I’ve written since it already contains  So, the first step would be to go from something that looks like this:

 

interface ProductInterface {

    public function getName();

    public function getPrice();
}

 

to something like this:

 

class Product implements ProductInterface {

    public function getName()
    {
    }



    public function getPrice()
    {
    }
}

and then I would implement the code for each of those functions. The procedure for going from the interface to the skeleton of the class goes something like this:

 

  1. Copy the interface into a new file.
  2. Replace “interface ProductInterface” with “class Product implements ProductInterface”
  3. For each function, replace the trailing semicolon with a line-break, an opening brace, another line-break, and a closing brace.

 

Not terribly complicated, but it can get tedious if you’ve written a lot of interfaces with a lot of functions to implement. How do we get this boilerplate done quickly so we can move on to the implementation? We record a macro! Here’s what that would look like for this particular example:

  1. :qi 
    

    Initialize recording into register “i”.

    • “:” allows us to enter a command.
    • “q” tells VIM to start a recording.
    • “i” is the register into which we will save the recording. Think of registers as variables, we can write to them and then read them later.
  2. ggyG

    Copy the contents of the file.

    • “gg” moves the cursor to the top of the file.
    • “y” initializes a “yank”, which is VIM-speak for “copy”.
    • “G” moves the cursor to the bottom of the file.
  1. :new 

    Open a new file in a split window below the current window.

  2. P

     Paste the text we “yanked” in step 3.

  3. :s/interface .*\(.*\)Interface/class \1 implements \1Interface

    Replace the “interface” declaration with a “class” declaration.

    • The “s” command, “substitute”, is the “Find and Replace” of VIM. It takes a pattern and a replacement. I want to stay focused on VIM in this post, so I won’t go into detail about this regex.
  1. :%s/\(function .*\);/\1\r\{\r}

    Replace the trailing semicolon on the function declarations with an opening and closing brace.

    • Here we use the “s” command again, but this time with a preceding “%”, which tells VIM to do the substitution on the whole file, not just the line the cursor is on currently.
  1. q

    Stop recording.

Okay, I know that seems like a lot. But we only had to do that once, and now, for any interface file, we can generate a class skeleton with just this:

@i

The “@” tells VIM, “do whatever is in this register”. In our case, we saved all those actions into register “i“, so now “@i” will repeat everything we just recorded, saving us time and a lot of keystrokes. Pretty cool! Also, we can save this macro into our VIM configuration file so that we can use it again later on.

 

Conclusion

VIM is an awesome tool for development, or any kind of text-editing. It works off of a completely different paradigm that offers more efficient, powerful methods of accomplishing that kind of tasks I as a developer have to do every day. At the end of the day, a good developer can work with any text editor or IDE, and we all have different preferences for our tools. I hope this post stoked your curiosity a bit, and I encourage you to give VIM a try. Be warned, it is definitely a deep rabbit hole, but it’s a fun one! Happy coding!