Improving My Vim Skills

I recently attended the Vim Masterclass online course lead by vimcasts.org’s Drew Neil. It was a great learning experience and I immediately bought Drew’s book, Practical Vim, to learn even more. This got me thinking about why I love Vim and why, after more than two years, I’m still finding myself striving to improve my Vim skills.

Why Learn Vim

It might seem a little odd to talk about having skills in a text editor. Certainly, I’ve never thought about improving my TextMate skills. I believe the reason is that Vim feels more like programming than it feels like a typing tool. Just as composing JavaScript functions creates the behavior in a web page, Vim commands generate the JavaScript file itself. If you think about Vim commands as a language to be learned, it will make more sense why it’s a skill to be developed over time. Consider the following command:

22Gfzdaw

Without getting into the specifics, this line means, “Go to line 22, move the cursor to the first occurrence of the letter ‘z’ and delete the entire word.” Doesn’t this seem like coding? Obscure coding, for sure, but once you understand the syntax, it’s pretty powerful. Even though the biggest hurdle to understanding Vim is having to memorize such terse commands, there are plenty online tutorials to teach the mnemonics that will help you remember them.

Best Practices

Just like a programming language, there are many different ways to achieve the same outcome. And, just like a programming language, there are best practices to get the most out of what you type. In his online course, Drew Neil talks about what he calls “The Dot Formula.” In Vim, the Dot Command (pressing the dot key) will repeat a command. Since a lot of what we do as developers is repetitive in nature, this is a really handy feature. However, to get the most out of the Dot Command, you need to consider how you type your commands.

viwcfoo

ciwfoo

In the above two lines, they both will change a word to foo. The first command will use visual mode to highlight the word then change it to ‘foo’. This is nice because you see what you are about to change. However, you don’t get the full power of the Dot Command. If you moved over to another word and pressed the dot key, you get unexpected results. Now try the second command. This one changes the word under the cursor to ‘foo’ without using visual mode. Now move to another word and press the dot key. That works as expected.

Although the above is a somewhat trivial example, it shows that really understanding the mechanics behind Vim’s syntax can help you to be more efficient.

Coolest Thing I’ve Learned

I’m trying to get my point across without writing an insanely long post about Vim. So, I’m going to leave you with this one command that I learned from the Vim MasterClass that I was the most impressed with. The ‘normal’ command.

:10,20 normal A;<enter>

This essentially says, “Append a semi-colon to the end of each line on lines 10 through 20”. We are able to write one command to affect 10 lines of code. Additionally, you can use the Dot Command in normal mode to repeat the above on another selection of lines.

:30,40 normal .<enter>

Always Learning

This has barely touched the surface of what Vim can do. If you’ve never tried Vim, I hope I’ve made you curious. If you do use Vim, I hope I’ve given you something you haven’t seen before.