Experimental Lex

Playing with words.

Autonumbered Content in TextMate

Today, I had another reminder why TextMate 1.5.x is still my favorite text editor. I really needed a convenient way to duplicate a line of HTML over a hundred times while auto-numbering specific places in the code.

It took me a while since I’ve never attempted this level of TextMate hacking, though it was totally worth it.  Here’s the code that you can create as a Command.

export line=`echo $TM_SELECTED_TEXT`

res=$(ruby -rui -e"print TextMate::UI.request_string(:title => 'Setup', :prompt => 'Enter autonumber max value:', :button1 => 'Go', :button2 => 'Cancel').to_s")
[[ -z "$res" ]] && exit_discard
export maxval=$res
"${TM_PYTHON:-python}" -c '
import sys, os
template=os.environ["line"]
for i in range(int(os.environ["maxval"])):
 line=template.replace("###", str(i))
 sys.stdout.write("{0}\n".format(line))
'

Here’s how you use it. Setup the line(s) of code you want to duplicate using “###” as placeholders where you want it to insert the current line index (zero-based).

<div class="bb-item" id="item###"><div class="content"><img src="ebook/page_###.jpg"/></div></div>

Select the line(s) in TextMate and run the command. And you just saved yourself a bunch of time.

Advertisements

Comments are closed.

%d bloggers like this: