Here's quick little command/macro pair for handling Markdown headings using the setext format. I wrote it because I always find myself changing the text of my headings, which means erasing the underline and redoing it so the line of equals or hyphens matches the length of the new heading text. (Yes, I know that Markdown doesn't require the length of the underline to match the length of the heading. That's one of the many niceties John Gruber designed into Markdown. But I'm anal and like my underlines to match.)
Start with this command for `<h1>` headings.
#!/usr/bin/perl
chomp(@lines = <>); $under = "=" x length($lines[0]); print $lines[0] . "\n" . $under . "\n"; unless ($#lines == 0 or $lines[1] =~ /^[-=~]+$/) { print $lines[1] . "\n"; }
The input should be **Selected Text** or **Nothing**, and the output should be **Replace Selected Text**. I named it "Heading 1."
Now record a macro that prepares the input and runs the command. The idea is to be able to call the macro with the caret anywhere on the line with the heading text. If the heading isn't already underlined it will underline it; if the heading is underlined it will adjust the underline to the new heading length.
To make the macro, put the caret on the heading line, start recording and
1. Type **Command-Leftarrow** to move the caret to the beginning of the line. 2. Type **Shift-Downarrow** twice to select the heading line and the line below it. (Don't worry if there is no line below it; the macro will record both keystrokes and the command was written to handle the single-line case.) 4. Invoke the "Heading 1" command to transform the selected text. 5. Type **Rightarrow** to put the caret at the end of the selected text and deselect it.
I called this macro "Heading 1" also. For a "Heading 2" macro that inserts a line of hyphens, do exactly the same thing, but change the quoted equals sign in the fourth line of the command to a quoted hyphen.
There's a writeup of this on [my blog][1] if you need to see more details.
On 19/5/2006, at 5:32, Dr. Drang wrote:
Here's quick little command/macro pair for handling Markdown headings using the setext format [...]
Actually, if you write a = or - in the first column of a line (in Markdown) and press tab, it will insert as many as required to underline the line above.
On 5/18/06, Allan Odgaard throw-away-1@macromates.com wrote:
On 19/5/2006, at 5:32, Dr. Drang wrote:
Here's quick little command/macro pair for handling Markdown headings using the setext format [...]
Actually, if you write a = or - in the first column of a line (in Markdown) and press tab, it will insert as many as required to underline the line above.
Yes, I know about the Tab-Triggered commands that come with TM (should have been more explicit about that in the message, I guess). But when I change the headings--which I often do--I like to readjust the lengths of the underlines with a single keystroke, not an erase-equal-tab sequence.
Now that I think of it, a snippet that automatically types the underline as you're typing the heading text would be really cool. Not productive, maybe, but cool.