A few days ago I found that there is no way to print on Sublime Text 2. I was a little disappointed with this discovery, printing is such a basic feature that even Notepad offers. Why doesn’t Sublime Text 2 include it? Ok, besides saving trees, I hate to print stuff on paper myself, but sometimes you will be asked to print your code, either by a professor or someone else wanting to review your code.
In the meanwhile I found a reasonable workaround that preserves syntax highlighting and code numbers. It dumps your code to an HTML file and then it uses your default browser to open a print dialog. Pretty neat!
- Install the Package Manager (if you haven’t already) by pressing CTRL+` (open the Python console) and then paste the following:
import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('https://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation' - Use CMD+SHIFT+P or CTRL+SHIFT+P, then select “Install package”
- Search for “Print to HTML” and install the package.
- To print, select “Print as HTML to a Web Browser” from the File menu, or access it by pressing CMD+SHIFT+P or CTRL+SHIFT+P and searching for “Print to HTML”.
Voila’!
Thanks to Joel Thornton for developing and maintaining this plugin. You can fork it on Github at https://github.com/joelpt/sublimetext-print-to-html