Sublime Text 3 - Copy as RTF and HTML with SublimeHighlight

Posted on August 29th, 2014


What makes Sublime Text such an amazing text editor, is not only the amount of features it has right out of the box, but the amount of user created plugins. One plugin, SublimeHighlight, is incredibly useful if you want to copy your rich text in order to paste into editors such as Microsoft Word or Evernote. Not only can you copy your highlighted syntax as RTF or HTML, it also allows converting your text to RTF and HTML which will open up in a new file.

*Note - If you already have Package Control installed, and know how to install new packages, you can skip the first couple sections of this post and go right to Using SublimeHighlight. The package you are looking to install is called SublimeHighlight.

Installing Package Control
Before you able able to install third party plugins, you first need to install the Package Control. (These are the commands for Sublime Text 3. Please go to here to see the commands for Sublime Text 2). Copy and paste the following into the console. You can open the console with CTRL+`


import urllib.request,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c659d4bb41d3bdf022e94cab3cd0'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

Installing SublimeHighlight
The directions for installing SublimeHighlight can be found here, but I will also detail them below.

First, you need to add a new repository. You can do this by opening the Command Pallette with CTRL+SHIFT+P. Once opened, start typing 'add' and you should see this:


Hit 'enter' on the Add Repository command. A text box should popup at the bottom of your screen where you can enter the URL of the repository. Copy and paste the following into that field, and hit enter:


https://github.com/n1k0/SublimeHighlight/tree/python3

After the repository has been added, you can now find the plugin SublimeHighlight with Package Control. To get into Package Control, enter the Command Palette again with CTRL+SHIFT+P, and type "Install Package". Hit enter, and you will see a list of packages appear like this:


Simply search for SublimeHighlight, and install the plugin. After it is installed, restart Sublime Text 3.

Using SublimeHighlight

There are quite a few features associated with SublimeHighlight.

  • SublimeHighlight: convert to HTML: will convert current code to highlighted HTML in a new SublimeText tab.
  • SublimeHighlight: convert to RTF: will convert current code to highlighted RTF in a new SublimeText tab.
  • SublimeHighlight: view as HTML: will convert current code to highlighted HTML and open it in your default browser.
  • SublimeHighlight: view as RTF: will convert current code to an RTF document and open the generated file with your default program.
  • SublimeHighlight: copy to clipboard as HTML: will convert current code to highlighted HTML and store it into the system clipboard.
  • SublimeHighlight: copy to clipboard as RTF: will convert current code to raw highlighted RTF and store it into the system clipboard.
To use these commands, highlight your syntax, enter the Command Palette, and find you SublimeHighlight commands. Hit enter on the one you wish to perform. My personal favorite, is Copy to Clipboard as RTF. For example, the text below I copied straight from Sublime Text 3 with SublimeHighlight.

var callback = function(){
$( '.item-skills').each(function(){
newWidth = $(this).parent().width() * $(this).data('percent');
$( this).width(0 );
$( this).animate({
width : newWidth,
}, 1000);
});
$( '.icons-red').each(function(){
height = $(this).height();
$( this).animate({
height : 14 ,
}, 2000);
});
};
$(document).ready(callback);

There are many themes you can choose from when copying/converting using SublimeHighlight. Here are all the available themes. To change your theme, simply edit your user settings file in Preferences > Package Settings > SublimeHighlight > Settings - User.

A sample file would look like this:


{
"theme": "friendly",
"linenos": "inline",
"noclasses": true,
"fontface": "Menlo"
}

You may want to hotkey some of these commands. You can do so by editing your key bindings in Preferences > Key Bindings - User. Here is an example of binding CTRL+SHIFT+C to the SublimeHighlight copy as RTF command.


[
{
"keys": ["ctrl+shift+c"],
"command": "sublime_highlight",
"args": { "target": "clipboard",
"output_type": "rtf"
}},
]

Now it's time for you to go experiment with SublimeHighlight and let me know what you think in the comments below!


comments powered by Disqus
Andrew Backes

A Software Engineer living in Milwaukee, WI. Passionate about web/software development, tech, open source, and gaming.