When I first encountered Vim it seemed far too old and complicated to use. Once I had mastered the basics, though, I found it to be the most beautifully minimalistic text editor that I’d ever used, while being flexible and extensible enough to craft to my needs.

Reason 1: It’s Comfortable to Use

Having to move around larger blocks of text and numerous files, I’ve found that being able to leave your hands on the keyboard is a big plus. I never feel any pain in my hands or wrists, something that I often feel when I’m constantly reaching for the mouse and clicking around, and the default commands make editing almost second nature.

I won’t make any great claims that I can work faster as a result of this, but the simplicity of something like d3w to delete three words, cit to change inside a tag, or A to start writing at the end of a line (append) definitely make things easier than grabbing the mouse all the time.

Reason 2: Great Search and Replace

Search and replace is one of the most useful features that a text editor can provide, particularly when you’re dealing with large volumes of text. Vim’s search and replace is not only quick to use, but also provides powerful Regular Expression support for searching and replacing by patterns.

With this style of search and replace, you can do much more than just replace words or sentences, you can also completely reformat your code. Not only this, but you can reformat the code in either a particular selection in a file, or the entire file, or all of the files in the buffer.

Take the following examples, for instance:

  • Change id within selected text only (a block of text must be selected):
  :<,'>s/id=".\{-}\([0-9]\{-}\)" class="iAmNotANumber"/id="iAmNumber\1"/
  • Change id within current file only:
  :%s/id=".\{-}\([0-9]\{-}\)" class="iAmNotANumber"/id="iAmNumber\1"/
  • Change id within all open buffers:
  :bufdo %s/id=".\{-}\([0-9]\{-}\)" class="iAmNotANumber"/id="iAmNumber\1"/

These simple examples take any tag with an id containing a number and a class of ‘iAmNotANumber’ one space after the id and replaces them with only an id containing ‘iAmNumber#’, where ‘#’ would be the recorded number from the original search, for example the following:

<div id="individual_30" class="iAmNotANumber">

.. would be transformed to:

<div id="iAmNumber30">

Once you get the hang of it, you’ll find yourself using them all the time for anything small or large.

Reason 3: Grow it to your requirements

Vim takes a refreshingly simple approach to bells and whistles: there aren’t really any. Instead Vim comes with a set of composable key commands that make it easy to do what you want, and if you use a particular set of key commands a lot or a particular regular expression, then you can commit them to a key binding in the vimrc file, where your personalised settings are stored, and reuse them with the alias that you gave them.

On top of this are a number of great plugins that can add any functionality that you may require. The best way to start off, however, is to add to your vimrc config as you find the need, though there are a few useful settings to start off with in the vimrc file that you can find by graually canibalising other peoples vimrc files (I often find myself looking for a dotfiles repo on someones github profile).

Reason 4: Stay in the Command Line

This reason might not be for eveyone (and for those that like GUIs, there MacVim), but I really love not having to change windows to get to the terminal. With Tmux, I generally setup a session for each project I start and can have my environment entirely within that session, so I can have my files, server, database command line and anything else I need in Tmux and being able to use Vim as well is great.

Also great is the ability quickly put the Vim session in the background with Ctrl-z to do something from the command line than go back in with fg.

Reason 5: Quick and Easy Macros

To start recording a macro all you have to do is press q and then a letter (many people just do qq to quickly record a macro on the fly to ‘q’). To use this macro you then just press @q and the macro will run. Coupling this with a number also enables you to run the macro several times, and Vim can do incremental counting or maths, so you can go through numbers and count up (Ctrl-a) or down (Ctrl-x) or even do more complex maths (Ctrl-r =), which can be useful when combined with copying with y and pasting into the maths expression (Ctrl-r “).

Say you have a form, for instance, with twelve Inputs with Labels, but the labels are missing the ‘for’ attribute to provide a semantic link between the label and the input (particularly important for accessibility). With a Vim macro all that you have to do is the following (assuming that the inputs have ids and that the input comes directly after the relevant label):

qq                 " Start a macro on the 'q' key
/<input " Search next input tag (assumes it has a label before it)
/id= " Search the id of the input (assumes it has an id)
f" " Find the quotes for the brackets (assumes quotes are all double quotes)
yi" " Yank (copy) inside the double quotes
?<label " Search backwards for the label tag (assumes this relates to this input)
e " Go to the End of the word 'label'
a for="p" " After this write 'for="' and then Paste the Id from before and close with '"'
/<label " Search for the next label
q " Stop recording the macro
11@q " Run the macro 11 times for the rest of the inputs

This might not convince everyone to use Vim, but if it does you can get the basics with Vim Adventures, and then hone your skills with Vim Golf.