Monday, June 20, 2011

Making a Navigable Table of Contents in Vim

I love Vim. I never thought I'd say this, seeing as a decade ago I was strictly a notepad/Word guy, but I really like Vim. There is no doubt a learning curve, but it's well worth the effort.

Today I decided to create a simple document with a table of contents with a document in Vim. Text editors like Google Docs provide internal linking structures, but in Vim it's much, much simpler. Simply create a TOC with one item per line like the following:
>>SECTION_1<<
>>SECTION_2<<
    >>SECTION_2_a<<
>>SECTION_3<<
>>SECTION_4<<
    >>SECTION_4_a<<
    >>SECTION_4_b<<
>>SECTION_5<<

Once you create your TOC, it's really easy to navigate about your document. Simply move the cursor to the line in your TOC that you'd like to find in the main document, then hit the asterisk "*" key. Vim will find the closest word following the cursor's current location on the line and then search for the next value in the document that matches that word. You'll also notice I've used underscores in my section headings. The reason for this is that Vim will treat underscores as part of a word, rather than as a delimiting character like a space or hyphen. Does it get any simpler than this?

Cheers,
~Mike