Sharing code snippets isn’t always easy: between character limits on Twitter and formatting limitations everywhere else, it sometimes feels like the internet is trying to keep beautifully-formatted sample code out. And while there are plenty of tools for converting syntax-highlighted code into distribution-ready images, there’s a certain inconvenience to leaving the command line to use them.

Enter code2png, the easy way to generate beautiful snapshots from the comfort of the command line.

Installing via npm is as easy as:

$ npm install -g code2png
$ code2png hello.c -o hello.png

And et voilà! Out the other end comes a beautiful snapshot, syntax-highlighted and ready to take on the world.

Generated screenshot

How it works

Instead of creating beautiful snapshots from a desktop utility or the browser, code2png brings a browser (namely, PhantomJS) to where the code is. With a syntax-highlighting assist from Prism snippets from stdin or flat files are converted into HTML documents, snapshotted, and returned to the filesystem.

It isn’t subtle, but unwritten code rarely is. It’s functional–and brief. Better, smaller, faster? There’ll be time for that soon.