Sunday, February 14, 2010

Vim Substitution using Registers

In the last post I showed you an example of the large macros I used to create page titles and URLs. The reason they were powerful was because Substitute (:s) was combined with Registers.

Having Vim use the card title to create the contents of the registers then read them into the new page title, URL and link, all without input from me, meant that there was little possibilities of typos and other errors. It was also very quick as I never have to manually enter anything.

The particular section of the macro involved is:


This is the standard form of a substitution in Vim and lots of other places as well. What is interesting is what is being substituted. Rather than typing in the strings, both "from" and "to" strings are contents of registers.

To read in the original link name in the substitute the "<Ctrl>-R o" (insert the contents of register o) command is used. This is the command in Insert mode and it is expanded on the message line, but the "to" string (below) is not;
To read in the new link name the \=@l command is used. The "\" is the escape code and means that the "=" is not interpreted, but read literally and "@l" is the Normal mode (which we are in when we run the substitute command) method of inserting "register l".

I came across this way of doing things at StackOverflow and Vim Tips Blog. I'm not sure why the difference in methods of inserting registers is used. But it works, and quickly too, it took around 5 seconds to complete each card.

More on this substitution next time...

No comments:

Post a Comment