Last.FM: Recently listened

Friday, March 09, 2007

VIM

My favourite editor, vim, is a very complex tool. If you search the web, you will find many places, where people tell you that even if you are an experienced vim user, you only know a subset of its features. Every once in a while you get to know another useful feature.

Vim is sometimes also annoying. For instance I was oftentimes annoyed when I hit the q key followed by another key and ended up in macro recording mode. Before I learned that I could exit this mode by hitting q another time in comand mode, I was really frustrated. Thereafter it only got on my nerves. Until today I decided to find out what's so interesting about this record mode.

By hitting q you tell vim that you want to record a macro. If you hit a number or a letter thereafter, you tell vim into which register you want this macro to be stored. So hitting qq will end up recording a macro into register q. Now you can do anything you want with vim: insert text, search for things, open new files, ... When you are done, hit q for another time. Now the macro is recorded and you can re-run it using @ where is the name of the register you stored your macro in.

This feature came in handy today. I had to wrap a lot of one-liners with #ifdef X and #endif. So I recorded a macro that
  1. Entered insert mode.
  2. Opened a new line.
  3. Inserted #ifdef X
  4. Moved one line down.
  5. Opened another new line.
  6. Inserted #endif
  7. Exited insert mode.
Thereafter the boring task of inserting those ifdef's was just some keystrokes and I realized that I should have learned this feature earlier.

Now can somebody tell me how I can store macros for reusing them later on?

No comments: