Posts Tagged ‘vim’

Vim Tip – Registers and Macros

June 19, 2009 1 comment

Let’s say you’re a beginning vim user. You know how to yank, put, substitute and change. You know how to do forward searches within a line or do searches ’til’ a certain character; you can use /, ?, *, # effectively. You can combine your searches with commands to work some serious magic. You’ve seen the light. Vim is your One True Editor.

So what’s next? If you’ve mastered the basics, registers and macros are a good place to go turn pro. I’ll briefly explain both concepts, and then use an example to show how these features complement each other.


A register is like a variable in a typical programming language. It’s a location in memory where you can store something. We’ll look at two usages for registers: storing text, and storing macros.

Normally when you yank a line with yy, it’s contents get saved into the default, unnamed register, called “” (quote-quote). If you want to yank something into another named register, say register a, you have to prefix your yank with “{register}. The same goes for puts.

So for example…

yy    yank the current line into the unnamed register
"ayy    yank the current line into register a
"ap     put the contents of register a

Simple right? At first it might seem that this is only marginally useful, but registers can greatly simplify some macros. So we’ll talk about that next.


The q{register} command starts recording a macro. While you’re recording a macro, press q again to signal that you’re finished.

I like to use the q, w, e, and r registers for macros, simply because they’re easiest to reach. So for example, to start recording a macro, I’ll press qw, record my actions, and press q to signal the end of the macro.

The @{register} command executes a macro. So I can execute my new macro with @w. Usually once I know that it works, I’ll also have to run it many times with something like 100@w.

Tying it together

This came up a few weeks ago when I was generating some characterization tests. I had written a quick script to generate some test data in CSV; in reality it was a bit more involved than what follows, but this gets the point accross:

Input, Output
34523451, true
65434092, true
45810353, false
(~1000 more of these)

I had also written a single test at the bottom of the file, and I wanted a thousand more that all followed the template.

public void Test() {
    Assert.AreEqual(IsNumberValid(INPUT), OUTPUT);

So I used a vim macro — the vim keystrokes are in brackets () after each step:

  1. Cut the test template into register a (/\[Test<Enter>”a4dd)
  2. Move to the beginning of the file, start the macro (ggqq)
  3. Copy 34523451 into register b (“byw)
  4. Copy true into register c (f l”cyw)
  5. Delete the current line (dd)
  6. Put a copy of the test template at the end of the file (G”ap)
  7. Replace INPUT with the contents of register b (/INPUT, dw, “bP)
  8. Replace OUTPUT with the contents of register c (/OUTPUT, dw, “cP)
  9. Move the cursor back to the beginning of the file (gg)
  10. End the macro (q)
  11. Execute the macro 1000 times (1000@q)

Using the above, I was able to generate a ton of code in a minute or so. If I had tried to cobble together a script to do the same it would have taken at least several times as long. I use vim macros almost daily. I don’t often have to combine them with registers, but I’ve found it to be an invaluable technique in certain situations.

By Kevin Baribeau


Vim commands in Firefox

June 5, 2009 Comments off

I have always been secretly frustrated switching between keyboard and mouse trying to navigate the web.  I say secretly because I have always accepted the awkwardness of having to use multiple input devices and never openly questioned the deficiencies of such a work flow:  use keyboard to perform search, switch to mouse to click on resulting links, switch to keyboard to enter form data, switch to mouse to follow new links… does this scenario sound familiar?  If you have ever tried to use only the keyboard to perform this work flow you would have quickly become agitated using the TAB key to jump through the maze of focal points on a web page.

About two weeks ago I was introduced to a Firefox add-on, Vimperator, from Vimperator labs.  “Vimperator is a free browser add-on for Firefox, which makes it look and behave like the Vim text editor. It has similar key bindings and you could call it a modal web browser, as key bindings differ according to which mode you are in.” –

Being somewhat familiar with Vim from using Linux I was curious how a Vim command overlay would work for browsing the web.  I admit it was a bit intimidating at first when the Firefox toolbar and address bar disappeared, but after I started using the built-in help system, I quickly became amazed at how intelligent the integration was.  A favorite command so far is the way Vimperator handles links: use the ‘f’ key to tell Vimperator to enumerate the links visible on your screen, then use either the number to navigate to a link or start typing the text of the link and as soon as Vimperator identifies a unique link on a page you will be directed there, awesome.  Notice the yellow highlighted areas and the red/white numbered boxes in the screenshot below:

Vimperator QuickLinks

I was tempted to write this blog detailing the basics of how to use Vimperator but after a few google searches I realized there are many good blogs on this subject already.  One in particular stood out of the crowd and I would encourage you to take a look:

The most important things I should mention before you start on this adventure are:

1) use the help system! (accessed by typing ‘:help’ without the quotes in the Vimperator command line).

2) use autocomplete to help you find new commands (accessed by typing ‘:set wildoptions=auto’ without the quotes in the Vimperator command line).

3) there are situations which don’t work well with Vimperator at this time so don’t throw out your mouse (e.g. flash, filling in a form containing a lot of checkboxes, pages with high use of IFrames).

4) I would love to have a button – obvious to non-Vimperator users – to disable Vimperator.  It is not obvious for the non-initiated to disable Vimperator unless they know it is a Firefox add-on or they know how to execute a Vimperator command ‘:addons’ (without the quotes).

5) Vimperator mixes well with other add-ons:
* Speed dial []
* It’s All Text! []
* Ubiquity []

Good luck, be patient as the learning curve is steep, and have fun!

Logan Peters, Marcos Tarruella