Escape code for html <pre> or <code> tag into clipboard

Alec Jacobson

August 13, 2009

weblog/

Here's a speed up for posting code in a <pre> or <code> tag in an html page or blog. The script escapes all less than and greater than symbols (< with &lt; and > with &gt;) in a given file then puts the results in the clipboard to facilitate pasting into a text area or text editor. Save this in a file called escape-copy.sh:
#!/bin/bash
cat "$1" | sed s/\</\\\<\;/g | sed s/\>/\\\>\;/g | pbcopy
Posting the above code was super easy now, I just ran ./escape-copy.sh escape-copy.sh and pasted it in a <pre> tag.