Sunday, February 14, 2010

Vim Sort to make an Index

I have shown you how I began creating an Index of Kellogg's Quotes, firstly using Vim keyboard macros that found the quotes and put their URLs in a file. Then I used Vim's search and replace to get a file with just the quote's title and URL, in other words a list of quotes. It was not in order, so:
To sort the index list into alphabetical order:
"@w" to get the displayed text to the front of the line for sorting:
^df>$pj
That is:
^ - move to the beginning of the line
df> - delete to and including the ">"
$p - move to the end of the "p"ut or paste what had been deleted
j - move down one line

Once again I did this 20 lines at a time with the command
20@w

Our line now looks like:
Hydrotherapy Departments</a><a href="../Techniques/OtherApplications.html">

The actual sorting is very simple
Place the cursor on the first line of the list
ma - to mark this line
G - to go to the end of the file (and the list)
!'a sort - runs the block back to the mark through the bash command "sort"

Sorted!

^2f<D^Pj

"@q" To get the link back in shape:
^ - go to the beginning of the line
2f< - move the cursor the second "<" character
D - delete to the end of the line
^P - move back to the beginning of the line and "P"ut the deleted text before the cursor
j - move down a line, ready to repeat the procedure.

Once more I repeated it 20 times with:
20@q

Finally to complete the HTML contents of QuoteIndex.html. I made the title line and marked it up as the appropriate header then simply put <ul> under the title to start the list and a </ul> at the end of the file to end the list. Then

"@w" Make into list items
I<li>^[A</li>^[j

I<li>^[ - Insert at the beginning of the line the text "<li> then <Esc> back to normal mode
A</li>^[ - Append at end of the line, the text, </li>
j - move down a line, ready to repeat

Once again done with a 20@w to do 20 lines at a time. And presto, the file was back to normal HTML.

The body of QuoteIndex.html is now complete.

No comments:

Post a Comment