Monday, February 8, 2010

Vim Keyboard Macros and Split Windows

To produce the card I showed you in the last post, which combines similarly named cards from all the six sections of the original program (Hydro, John, Harvey, George, Quote and Glossary), I used the keyboard macro capabilities of Vim.

So I would:
  • be working with a split screen (:sp), one open in the hprob.txt and the other on jprob.txt (the "John" problem file)
  • search for the card (/@Visceral_Congestion) in jprob.txt and if I found it press <Enter> so the cursor was on the #card line in jprob.txt
  • <Ctrl>-W p which takes me back to hprob.txt (my active text file)
  • put the cursor on the #card @Visceral_Congestion line in hprob.txt
  • call the following macro with "@a"

ofrom from Dr JH Kellogg's Hydriatic Techniques...^[k$^Wpofrom from Dr JH Kellogg's Prescriptions...^[k0ma/#end^Mk$d'a^Wppdd

This is:
o - opens a line below into Insert mode and inserts "from from Dr JH Kellogg's Hydriatic Techniques..." (in practice Vim puts a hash (#) in front of the first "from" making "#from" apparently the "o" command always puts in any unusual line starts. This is great for searching later as we will see. This correctly marks the source of what is already on the card
^[ - is <Esc> and takes us back to Normal mode
k - moves the cursor up a line
$ - moves the cursor to the end of the line ready for the jprob.txt card's contents to be prepended
^Wp - is <Ctrl>-W p which moves the cursor back to the Previous window (jprob.txt) and places the cursor where it was (on the "#card" line)
ofrom from Dr JH Kellogg's Prescriptions... - the "o" opens the line below into Insert mode and inserts "#from from Dr JH Kellogg's Prescriptions..."
^[ - is <Esc> and takes us back to Normal mode
k - takes the cursor up a line
0 - takes the cursor to the beginning of the line
ma - sets the "a" mark on the line
/#end - searches for the next "#end" (end of card)
^M - is <Enter> so the cursor stops on the "#end"
k - moves the cursor up a line
$ - takes the cursor to the end of the line
d'a - is delete to mark "a" (in effect the contents of the whole card)
^Wp - takes the cursor back to the previous card
p - puts (pastes) the contents of the delete
dd - deletes the line where the cursor is as its empty

I would do this for each section, this one is from "John", I just changed the "from" areas of the macro before I did each new section.

The result of all this is that now all the information on Visceral Congestion is on just one card. (Which is what you saw in my last post.)

It took just over a month of working in my spare time to combine all the cards from the original six parts into just four sections:

  1. Diseases.txt
  2. Effects.txt
  3. Problems.txt
  4. Techniques.txt

These will eventually end up as four subdirectories in the final Traditional Hydrotherapy web site.

No comments:

Post a Comment