Crap! Wrong mode

A beginner’s beginner’s guide to Vim

Hao Su
Brigade Engineering

--

Vim changed my life. Seriously. It did. While that statement may sound like hyperbole, using Vim has completely changed how I interact with my computer.

Like many others when introduced to Vim, my reaction was far from welcoming. Why would I use a 22-year-old editor when I was perfectly capable of using a shiny new editor like Sublime Text or Textmate? After giving Vim a fair chance, I now feel weird typing in to this Medium window (or any other editor for that matter). The power of Vim’s text manipulation shortcuts coupled with its extensibility have made other editors feel inadequate.

To help ease the Vim learning curve for other new users, here are a few tips to help you along.

To start, a few rules to live by:

Rule 1:

Thou shalt not use thy mouse

Rule 2:

Thou shalt not use thy directional keys

Rule 3:

Thou shalt be as Vim-y as possible

While the first two rules seem simple enough, they are essential to the Vim transition. Both of these rules force you to learn Vim navigation and to understand normal mode. By forgoing the mouse, you are forced in to keyboard navigation. By forgoing the directional keys, you are forced in to normal mode navigation. I know some users will unbind the directional keys to help expedite this process.

Now that you’re forced into normal mode, editing text may seem like a daunting task. Jumping full hog and whole boar in to coding may be both the most effective but painful way of learning navigation. If you want a warm up, there are fun games like Vim Adventures to help you along.

The last rule, while a bit broad, will help skyrocket your Vim competency. Play golf with your operations in Vim, try to do things in as few commands as possible. If there’s something you need to do, (most of the time) Vim gives you a chain-able group of shortcuts to do it.

To empower you, young earth-slug-hopper, here are a few things to learn to stay sane while following the three rules:

  • Learn Vim word motions. This will fundamentally change the way you interact with text
  • Learn Vim navigation and command structure (action-times-what, d4w)
  • Learn Vim text objects. Just like word motions, learning about Vim text objects and text object navigation/manipulation blew my mind.
  • Use Command-T or ctrlp (if you’re lazy, sorry Greg)
  • Rebind caps lock to ctrl

With these 5 simple steps you can unlock weird secrets of Vim expertise (bahaha). As a fellow Vim apprentice, these steps have helped, and are still helping me understand my editor of choice better.

As mentioned above, Vim has changed the way I interact with my computer. I try to never leave the home row. I used the Vim-style shortcuts for Gmail. I’ve installed Vimium. I long for keyboard-only navigation of Mac OS. It’s turning into a problem… help…

I hope these tips have helped, below I’ve included some Vim commands, custom key-bindings, and plugins helpful. Happy typing!

Bonus round:

  • I use absolute line numbers, many people like relative. Either way, navigating by line number is great. Modifying text by line numbers is even better, try: `:10d` or `10,20d`.
  • Even better than navigating text by line number, navigate by search. Use `/` to start a search, and use `n` and `shift-n` to navigate between matches.
  • Even better than typing in search terms, move your cursor over text and type `shift-3`. This will automatically search for the word you are currently over.
  • Download the ctags plugin, with it you can find method definitions in a snap. It works almost all of the time, just make sure you exclude external libraries.
  • Start dabbling in Vim macros. Record sets of commands and navigation, and replay them in a snap.
  • Bind `jj` to escape. There aren’t any English words with two adjacent j’s. Super quick home-row shortcut to exit insert mode. (Some people may argue that this is a bad habit and you should just use `esc` instead)

--

--