Sep 22, 2008

Vim, revisited

A few months ago (just over 3 in fact) I posted about having begun to learn vi1. Since then, I've learned a lot about it, discovered several plugins, and many times attempt to hit Esc after typing into Firefox. Or use hjkl to navigate in a text area. The only other program I now use to edit text is OpenOffice, mainly because it's a little difficult to get fancy charts and things into Vim. Also I submitted a paper in monospaced font with no formatting, the prof might be a little annoyed. You might think it overkill to use Vim for something simple like jotting down notes, but the funny thing is that Vim starts a fair bit faster than any other graphical text editor on my machine, like GEdit or Kate.

I've replaced my IDEs with it. I used to use Quanta, but it is slow to boot, and is once in a while unstable. Once in a while it will crash when I use the built-in FTP. And when I mean crash, it not only crashes Quanta, but the entire X server goes down. Slightly annoying.

Your productivity is improved by a fair bit when you start using this. Due to the mode-based editing, it is much easier to type commands than using Ctrl/Shift/Alt/some-combination-of-the-three, especially when you want more complex things. Want to delete a line? Press dd. Swap two characters? xp. One I use a lot is Ctrl+6 (or Ctrl+^ without pressing Shift), which opens the last file you had open. Kinda like how in Half-life you press q to get to the last weapon. On that note, I wonder how games would play if you could differentiate between q and Q... I guess you wouldn't be able to use Shift for sprint anymore.

You can even record a set of keystrokes, and bind that set of keystrokes to a key: press q, then the key, call it k. Every key you type will be recorded. Then press q to stop recording. Then later on when you want to use that recording, press @ then k. My only problem with this is that @ is a little awkward to do over and over, but there is probably a rebinding of keys.

It doesn't just end with the keyboard shortcuts. There are plenty of plugins for Vim. I have three favourites:
- VTreeExplore - it is a window in Vim (btw in Vim you can split windows, just like most fancy editors) that shows a directory tree. Very handy. Others have written about it too.
- Surround - when typing contexts (like dw or d$, which are delete word and delete from-cursor-to-end-of-line, respectively) you now can use s, which affects the surroundings around a bit of text. Type ds( to delete the parentheses around something. Type cs{[ to switch the curly brackets to square brackets.
- Vim's Rails plugin - This does more than just syntax highlighting. It adds some very helpful things for file navigation (something that is a fair bit annoying in Vim). If your cursor is over a model or controller name, you can press gf to go to that file. If you're in a view or model, press :Rcontroller (this uses tab auto-completion too, so just type :Rcont and hit tab) to jump to the controller. Similarily for jumping to models. If you're in a controller action, you can jump to the view. It's all pretty handy, and there are probably plenty of shortcuts that I don't know about. You can read here to learn more about Vim+Rails.
Note: I know at least one person is going to mention Textmate. Two things: I don't use a Mac (nor do I intend to any time soon), and I don't like to pay for software.

So it's been over 3 months and I'm not turning back. In fact, this was pretty much the case after a few weeks, and I am continually learning more. I recommend it to any programmer. You can also try Emacs too, I think it does the same kind of stuff and it is mostly a matter of preference - kinda like Ruby vs. Python ;).

1 Technically, it's GVim, which is the graphical version of Vim, which is an open-source remake of an older text editor called vi, but these are just details.


Guillaume Theoret said...

Nice. I know I should take the time to learn vim but I'm just so lazy =(

Especially now that I'd found Komodo Edit.

Jeremiah said...

I'm an emacs man myself, but use vim here and there for editing configs.

You may be interested in Vimperator to help with your firefox/vim confusion-avoidance.

Rob Britton said...

@guillaume: Yeah I did the same thing for a while, and then one day got around to it. If you're happy with your editor though, might as well stick with it. Plus Komodo Edit has a vi-mode if you want to try it out ;)

@jeremiah: I remember trying Emacs a few years ago, and getting sick of Ctrl+this, Ctrl+that to do everything. As I said though, personal preference.
Vimperator is really neat. I saw it a while back on that Daily Vim blog, but didn't bother checking it out.
One thing I like: Hints mode. Press f, and all the links get numbers next to them. Type the number, and it links. It is less effort than using the mouse, but I'm wondering if it is faster.

Anonymous said...

About Vimperator's hints mode:

Try pressing f (or F if you want to open in another tab) and then type a few words which are found in the link. :)

Most of the time you won't have to type the numbers and that makes it *much* faster.

Rob Britton said...

Oh that's sweet. Yeah I always found that hints mode was a bit slow, since I had to type in the number. Much faster to just type the text.