Learning Vim: What I Wish I Knew

31,434 reads

Vim. Three letters that strike fear in the hearts of millions. By chance, I stumbled upon Vim (Vi-Improved) a couple of months ago. It was love at first sight. I proceeded to devour every article I could find on the mythical beast. This post is the advice I wish I would have found in an article when I was first starting out.

# Build Your Own .vimrc

At first, I started out by downloading one of the pre-configured Vim distros — the Ultimate Configuration. It was exciting, but I didn’t have the first idea how to do anything. I had a tricked out machine gun, but didn’t have the slightest clue on how to take off the safety. This seems to be a common problem when starting out with someone else’s configuration. So, while intimidating, it is recommended to start from scratch. The best way to get up and running is to follow this guide. It will coach you through setting up your initial configuration. One of the cooler aspects of this process is that you’ll start to understand how personal your config is. Your .vimrc, or init.vim — for Neovimmers, really becomes and extension of who you are as a programmer. It will reflect what you care about and how you spend your time. The journey of going from 50 lines to 500, or 5,000, is riveting. Plus, it is one of the best ways to help yourself grok Vim, both in philosophy and usage.

# Relative Numbers

This tip was the initial catalyst in understanding Vim’s value proposition. Before relative numbers I was hulk smashing jjjjjjjjjjjjjj and kkkkkkkkkk like a possessed loon. Clearly, missing the point of Vim. Then I uncovered a post that outlined the benefit of relative numbers. I was intrigued, so I set it up in my .vimrc and started cooking.

Immediately it made a profound difference. I started to be able to jump efficiently. This is a fairly common process in Vim: do something like a caveman, then learn the 007 method. At this point, you should continue on and read this holy text.

# f & F

While my vertical precision was improving, my horizontal movements were still barbaric. Llllllll, hhhhhhhh. Not the ideal method of transport across long lines. I learned about w and b, which, while convenient, still led to a lot of wwwww and bbbbbbb’s. It wasn’t until I uncovered the f & F command that I made headway. In essence, f & F can be thought of as find. Lower case f will search forward, while F searches backward. Either case will position your cursor on the character in question. If you desire a placement right before a character use t & T. These two simple keys are impressively dynamic. For instance, dfs, which will delete everything until Vim finds the next occurrence of s.

# c

At this point, I was starting to get my feet under me. I could navigate up, down and left, right with ease. One of the next major keys in furthering my development was c. This key analogizes to change.

cw — change to next word

cc — change entire line

ce — change from cursor to end of word

c$ — change to end of line

ci” — change inside double quotes

ca” — change around double quotes

cfx — change until next found occurrence of x

While powerful, this key gains further might when paired with targets.vim.

# Remap Caps Lock -> CTRL

At first, this seems preposterous. You’ll now have to hold shift anytime you want an uppercase letter, but this remapping is more practical than it appears. The control key lies at the heart of numerous operations within Vim.

CTRL + (insert key)

v — visual block mode

w — window adjustments

r — redo

b — back one full screen

f — forward one full screen

d — forward half screen

u — back half screen

[ — built in <esc> mapping

Hopefully, you’re starting to get the picture. Often times, people will remap CTRL to ESC for alternating back to Normal mode. While logical, it doesn’t appear to be the optimal solution. This becomes quite evident once CTRL-[ becomes second nature for escaping modes. Furthermore, CTRL plays a large part in Vim’s built-in completion methods. It will feel awkward at first, but with repeated use will become engrained.

# Plugins Aren’t The Solution

Out of the box, Vim seems like a deserted wasteland, so it makes sense that you’ll need to give it wings to soar. This notion makes sense until you learn the dark truth.

Want a file explorer? Type :Sex. Remove all trailing spaces? Try :%s/\s\+$//e. The list goes on and on. It’ll seem mystifying, but most plugins aren’t nearly as useful as they appear. This isn’t to say that all plugins are bad. There are certainly some that are worth their weight in gold. I’m quite fond of Nvim-R myself, but remain wary. In general, look for solutions to your problems via Vim itself. This can be done with a quick :help ‘your query here’ within Vim. This process will save you a variety of headaches, while getting yourself further acquainted with the internal docs.

For a complete PDF of Vim’s legendary help navigate here.

# Keep It Vanilla

Vim tends to lead to heavy customization. This isn’t necessarily bad, but can bite you in the end. For me the moment came when trying to use Vim keybindings within R Studio. I had become so dependent on all my mappings and shortcuts that my skills were nonexistent. Sure, using H and L instead 0 and $ is more convenient, but this misses the point. The value of Vim lies in the language. Once you master vanilla Vim you’ve gained a lifelong skill. One that will diminish in value with each custom mapping you set in your config. This isn’t to say avoid them at all costs, but more a friendly warning.

# Bonus Section

Tags