How I get started on using Vim, part 1 – my .vimrc explained

For some reason, I’ve had it in my head that as a programmer, I should learn how to use a command-line-based text editor like Vim. It is more universal across devices, so knowing it would at least give me an option to navigate around computers other than my own, is an advice I often hear. It would be more applicable if I were an ops person, but on the infrequent (though increasingly less so) occasions that I have to remotely log in into a server box, I do find that to be true. Furthermore, since for most codebases I deal with, I already have a terminal window open for git and build tools, I figure keeping text editing in the same context will be beneficial.

I tend to agree with Yehuda Katz’s experience learning Vim, that it is something one should learn incrementally. I started down this path almost two years ago, only to decide on several occasions that it wasn’t the right tool for me. It is not until very recently that I feel comfortable enough to use it as my primary text editor. Even then, there are times when I find myself opening up Sublime Text or Atom because they’re more appropriate for what I need to do.

What allowed me to be more comfortable in Vim is less about learning the shortcuts and muscle memory, but more the heavy customization that I have put on top of Vim to give it the convenience editors like Sublime have out of the box, and also the deeper insight into how Vim actually works.

I have been thinking about writing this blog post for a while, but never felt like I was ready to have an “authority” on this topic. But I decided today that it’s time I break down and explain my Vim setup (through the .vimrc file) before it gets too complicated.

To me, the most fundamental difference between Vim and a GUI editor like Sublime is that Vim is a modal editor. That means Vim has different “modes” that a user could be in at any given time. In Sublime, you’re always in edit mode. Anything you type on the keyboard is recorded in the text buffer. Other actions, such as saving, linting or navigating, are done through special key combinations, such as key on Windows or Cmd key on OS X. In Vim, the default mode is NORMAL, which is where you can do most of these “meta” actions on the document, without modifying the text itself. You need to explicitly enter the INSERT mode to edit something, and escape out of it to perform those actions. Being aware of the current active mode is important, and will help beginners feel less confused and lost. You can make vim show the mode with the command :set showmode while in NORMAL mode, or see the .vimrc breakdown to see how to set that permanently.

The second most powerful difference in Vim as compared to a GUI editor is the concept of motions, or text objects. There are a lot of great tutorials and resources out there explaining this. I find Vim Text Objects: The Definitive Guide to be a really great place to start, as it was the “ah ha!” moment for me. This way of moving around a document is probably what makes Vim much more appealing and efficient for me.

As I walk through my .vimrc setup, I will denote the ones that I think are basic developer use as [basic], so search for those if you just want a quick way to get started with Vim. My .vimrc file actually changes quite often as I refine the setup, so take a look at the source code to see the latest version. Here goes:

While plugins and basic setup establish a friendlier stage for a beginner to Vim to start exploring around, one of the most frustrating things when trying Vim out is not knowing how to do basic developer tasks in Vim. In addition to the “[basic]” setup I have outlined here, I will document more of what those tasks are, such as find/replace, copy/paste, indentation etc. in the second part of this series.

comments powered by Disqus