Engineering

An Approachable Guide to Learning Vim

Noah Ezekwugo May 15, 2020

Introduction

Every developer has been there: You’re sitting there, minding your own business, and need to update /etc/hosts, write a commit message after resolving a merge conflict, or do something else completely innocuous and you’re dropped into an unfamiliar environment. Suddenly your mouse doesn’t work, keys aren’t doing what they’re supposed to do and you don’t even know how to exit[1]. Welcome to the wonderful world of Vim!

When I first started working at DataGrail, one of my first OKRs was to prepare a Lunch and Learn on a topic I had no prior experience in. After being exposed to the heated contention among our team’s hardcore vim-ers and casual IDE-ers, I decided to teach myself vim. However vim can be extremely daunting to learn, and I soon found myself losing motivation as quickly as I lost my productivity. There’s a ton of technical resources out there, but most of them focus on strict definitions and huge lists of commands, which left me feeling more intimidated than ready to dive in. So rather than teach you all the ins and outs of vim (or practically any of them really), hopefully in this guide I can provide some context and a roadmap that can help you approach adopting it with a bit more confidence as you make a smooth transition.

What Even Is Vim, And Why Should I Care?

Vim is simply a text editor, just like Notepad, Microsoft Word, VSCode, or whatever else you use to edit text. However the (oversimplified) thing that makes vim different from these other editors, and probably any other program you’re used to using, is rather than being controlled visually (by mousing around, clicking buttons, etc.) vim is controlled entirely through keyboard commands in its scripting language vimscript. Like other languages, vimscript has verbs, nouns, and modifiers you use to compose commands that perfectly describe the actions you intend to take.[2]

Once you get the hang of this, you’ll see a huge efficiency boost.

Suddenly you can do cool commands like:

  • J – (J)oin/append the line below to your current line, adding a space in between
  • m+3 – (m)ove the current line 3 lines down
  • di{ – (d)elete all the text (i)nside the { curly braces } your cursor is currently inside
  • :-5,+10s/old text/new text/gi – Between 5 lines prior(-) and 10 lines after(+) your current line, (s)ubstitute “old text” with “new text”. Do this for every(i.e. (g)lobal) instance, and apply this with case (i)nsensitivity

To put this in perspective, think about how great it felt when you learned the keyboard shortcuts to Exit a window, or Copy/Paste, or Select All. And think about how frustrating it is to watch your computer illiterate loved ones manage to find the most inefficient possible way to accomplish these tasks. That’s how vim users feel about everything.

Philosophy of Learning

Vim is extensive. Like, really really extensive. And trying to learn it all at once can be both overwhelming and discouraging as you try to ramp up to speed. So I recommend taking things one step at a time:

Step 0: The Barest Basics

One of the cool things about vim is it comes preinstalled on most Unix-like systems, which means there’s not any upfront setup needed to start learning. That being said I would recommend installing the newest version of vim to get a few quality of life improvements that come with vim ≥8.0:

brew install vim

Beyond that, rather than recreate one of the many tutorials already out there, here’s a few resources to get a handle on the basics:

  • vimtutor – Run this in your command line and go through a ~30 minute tutorial on vim. This’ll give you a good baseline for navigation, basic editing, and file manipulation.
  • https://vim.rtorr.com/ – This cheatsheet was a permanently open tab on my laptop for the first couple weeks of learning, but you can find any number of cheatsheets and references to bounce back to as you ramp up.

Step 1: Toes in the Water

“Vim extensions are a gateway drug” – Josh (DataGrail Vim Expert)

If you’re already used to working in an IDE (IntelliJ, VSCode, etc.), going straight to vim can be a huge temporary drag on your productivity until you get back to your usual speed. Instead, I’d recommend installing a vim extension in your IDE of choice that can enable vim commands while maintaining your normal workflow as a fallback. For example:

These extensions won’t let you unlock the full power of vim, but will usually emulate the basic navigation/editing commands so you can acclimate to doing things the “vim way”. At this point I liked to write all the vim commands I repeatedly used on sticky notes and stick them all around my monitor so I could reference them at a glance and build habits until using them became second nature.

Step 2: Taking the Plunge

So you’ve been using your Vim extension for a couple weeks and you’ve gotten pretty comfortable editing lines and keeping your hands off the mouse. But you’ve noticed that half the commands on your cheatsheet don’t work and you realize you’ve barely scratched the surface of what vim can do. You’re ready to fully transition to vim!

Open your terminal, run vim {your-directory}, go forth and be free!

Well, maybe not yet. You’ll probably immediately notice you’re missing a lot of the convenience features of your old IDE like simple navigation through files, autocomplete, syntax highlighting, etc. but the good news is there are vim plugins and settings which can help replace and go beyond all of these things.

Think of the core features you can’t live without, and do some web-searching to probably find something that’ll fit your needs. Here’s a few to get you started:

  • FZF – Global search through directories
  • NERDTree – Visual file system explorer so you don’t have to ls and mv through all your directories
  • Auto Pairs – Automatily match parens, curly braces, etc.
  • Vim Prettier – Auto-format files

This is where you get to customize vim to perfectly fit your workflow. Of course you can always do what I did instead and copy someone else’s .vimrc to get a fully up-and-running environment and tweak it from there (mine which I 95% copied from our tech lead is located here). There’s also Maximum Awesome, an open-source vim config meant to get you started.

If you do go down the route of copying someone else’s config I would recommend at least googling each of the settings/plugins both so you can learn what’s available to you and also to allow you to trim/add as it suits your use cases.

Step 3: Diving Deep

It took me about a week to go from starting Step 2 to being back to my old speed. Once you get there, the real fun begins. Like I said before, vim is really really extensive**.** I have yet to look for a way to do something in vim and come up short. Soon your efficiency will cruise past where you were before and you’ll wonder how you ever managed any other way. A few things I’ve had fun learning about:

Step 4: Spread the Good Word

Hopefully this guide has made the prospect of learning vim a bit less intimidating and you’ve set yourself on the path to become the resident vim expert. However one of the best ways to truly master a skill is to learn alongside others. In the 6 months since I presented this guide during one of our Lunch & Learns at DataGrail, all of the former IDE-ers on our engineering team have transitioned over to vim and we’ve had a great time sharing our learnings, frustrations, and snippets in a slack channel called #vim-is-hard.

I encourage you to try something similar and tell your friends, classmates, or coworkers about the wonders of vim to go forth on the exciting journey together. Or come work at DataGrail (We’re hiring!) and we’ll set you up with our newest key bindings or favorite tpope plugin.[3]

Endnotes

[1] You are not alone. Exiting vim is one of the most frequently asked questions on Stack Overflow

[2] To internalize this idea, check out this great post about “grokking vi”

[3] Of course, using vim at DataGrail is completely optional and we won’t judge you if you want to stick to an IDE