If you’ve ever worked with other people on some piece of code or program, particularly over IRC or IM or some other form of real-time or fast text communication, chances are you’ve used one of the many “paste” sites available (my personal favorite is pastie). These sites offer a convenient way of sending small to medium chunks of text to other people quickly, by simply copyng and pasting the text into a web form. This is a lot better than the old way of having to send an e-mail attachment, spam an IRC channel, or upload the text to some web host, but as I used pastie more and more often I started to realize that it could be made even faster.

So I wrote a simple Python script that lets you quickly upload some text or a set of files to Pastie. It can do stdin or a bunch of files, and it also tries to figure out what syntax highlighting to pick from the file extension. You can grab it here. Rename it to pastie, make it executable, and place it somewhere in your $PATH. There is built-in help available (just call pastie --help), but here are some simple examples:

# this pasties the output of do_something as a public pastie
$ do_something | pastie -p
# this pasties the specified files, using a header for each
# and the correct syntax highlighting setting
$ pastie foo.c bar.py baz.sh
# pastie stdin with author "marcan" as a python script
$ pastie -t python -n marcan

You can also set the DEFAULT_NAME variable at the top of the file to a quoted string to use a certain author name by default for all your pasties. There’s an extra undocumented feature useful for editor integration: if you pass in a filename to the -t option, it’ll try to figure out the parser from the extension and use that for syntax highlighting (but it’ll take input from stdin unless you use file arguments). This is useful for making an editor plug-in or action that can pastie the current selection from a document and still keep the right syntax highlighting:

echo "%current_selection" | pastie -t "%current_file" | xargs firefox

A very neat trick is to make a global shortcut to pastie the current clipboard contents. If you’re using KDE with X11, for example, you could bind ctrl+shift+c to the following command:

xsel -o | pastie | xsel -i && play ~/your_favorite_bell.wav

This will pastie the current X11 selection, then write back the URL as the selection, and play a ding of your choice. This means you can select any text, hit ctrl+shit+c, and then middle-click on an IRC or IM session to paste the URL to the text that you just selected.

Happy pasting!

2009-04-27 16:18