Switching to Vim
Last year I made a decision to switch to Vim for all my coding and text editing. Many times before I’ve tried Vim but always ended up going back to the comfort of the much loved Sublime – a fabulous text editor – for doing anything serious or commercial. Vim just made my head hurt. Yet I had heard so much about Vim, how once you get it you never go back, that I thought to myself "right, lets do this" and forced myself to only use Vim for all my coding and text editing. One year on I love Vim and will never use anything else to write code or edit text.
Whenever I mention Vim on Twitter I often – quite rightly – get asked why I’ve moved to Vim when there are so many great text editors out there that don’t have the overhead of learning Vim. So here’s just a few of the reasons why I’m such a big fan.
Selecting text. When you’re coding, much of the time is spent traversing a document, finding methods, selecting bits of text, etc etc. Think about how many times your hand leaves the keyboard to then use your mouse to select a piece of text to replace. Those little micro-movements all add up. In Vim the whole idea is your hands stay on the keyboard so you don’t waste time making unnecessary movements. For instance say you want to replace some text on a line that’s inside some quotes – something you probably do a lot. In most text editors you use the mouse to select the text, delete it and then insert the new text. In Vim, whilst on that line and in Normal mode, you type:
c i "
which means change inner quotes. It then deletes the words within the quotes, places the cursor inside, and goes straight into Insert mode, ready for you to type your new text. Imagine how much time that simple thing will save over the course of a day?
Markers. Everyday I learn something new in Vim and recently I discovered a thing called Markers. Often times you’ll be working on several files and you may need to jump around from file to file, maybe working on different methods, going back and to. Well in Vim in normal mode you can type:
This will make a marker with the letter A for that current position in that file. You can then jump to another file you might be working on, but at anytime you can jump back to that previous place by typing:
and like magic you’ve now instantly jumped back to that other piece of code. I can’t tell you how much I use that in the course of a day. Another great little shortcut is pressing CTRL-o which will jump backwards in the editing history, across documents, which is another thing I use all the time.
There’s so many great things I love about Vim, too many to detail here such as using Ack, to do multi-file search and replace, recording macros and much more but one other thing I want to mention is something I used recently that might be more long winded without using Vim.
I had a csv file for a project I’m working on made up of thousands of lines. I noticed that some lines had some date anomalies, namely they had the date as 2079, and that was causing problems with my code. In Vim it’s trivial to ditch these lines from the file. In Vim’s execute mode I type
and bam – all lines matching that pattern were deleted from the file. The nice thing is I also wanted to know just how many of these lines had this anomaly so I could tell the client, and even create a document with just these lines. That’s easy too. I just reversed the pattern matching like this
and now it deletes any line that DOESN’T contain that date. Now I’m sure this is all possible on the command line or in other tools but this is all built into Vim and for me it’s really valuable.
Oh and I should mention also that since I use Processing for much of my work I installed this great Processing Vim script so I can build / compile Processing code directly from Vim.
When you start getting into Vim you’ll begin putting together your .vimrc file. This is a settings file that Vim uses on start-up to configure things just how you like it. Here’s what mine looks like.
I also prefer to use MacVim , a GUI version of Vim, that you can still trigger from the command line.
Vim has a built in tutor but to be honest I’ve never used it. The thing that tipped me over into the Vim camp was the Venture into Vim tutorial on Tutsplus. Vimcasts is also great and you should definitely follow Harry Roberts who shares some great Vim articles and tips on his blog and his Twitter feed. The main thing for me though was actually committing to using Vim everyday on all my work. I think without that I would have slipped back into using my usual text editors.