Sunday, February 14, 2010

Vim Search and Replace to edit lines

In the previous post we began making an index of Kellogg's quotes. When all the Section were read into QuoteIndex.html I used the following macros:

"@q" searches for #qhead (Kellogg's heading) and replaces the displayed text in the link with this title

/#qhead/e^Mw"ly$2k^f>ldt<"lPn

/#qhead/e^M - search for #qhead and leave the cursor at the "e"nd of the search txt, (ie. the d of #qhead) <Enter>
w - move to next word to the right (the first word of the heading)
"ly$ - yank into "register l" everything to the end of the line (ie. the title of the quote, "Hydrotherapy_Departments"
2k^ - move cursor up two lines and then to the first character of the line (the #card line)
f> - "find" the first occurrence of ">" on the line and move the cursor onto it
ldt< - move one letter to the right one space then "d"elete from there to "<", ie all the displayed text.
"lP "P"ut or paste the contents of "register l" in the displayed text area of the link.

The resulting line now looks like:
#card @<a href="../Techniques/OtherApplications.html">Hydrotherapy_Departments</a>

"@w" gets rid of all the #quote and #qhead lines

/#quote^M2dd

This means:
/#quote^M - search for the text "#quote" <Enter>
2dd - delete two lines (the #quote and the #qhead lines)
I did this 20 cards at a time by typing
20@w until the end of the file, leaving nothing but the #card lines.

To get rid of anything but the link use a simple substitution:
%s/#card @//
- for the whole file substitute nothing for the text "#card @", in effect deleting it
The line now looks like:
<a href="../Techniques/OtherApplications.html">Hydrotherapy Departments</a>

No comments:

Post a Comment