Create a gist now

Instantly share code, notes, and snippets.

Code highlighting for Keynote presentations

Step 0:

Get Homebrew installed on your mac if you don't already have it

Step 1:

Install highlight. "brew install highlight". (This brings down Lua and Boost as well)

Step 2:

Highlight your source. "highlight -O rtf myfile.php | pbcopy". This will highlight the source code in RTF format and copy the result of that operation to your Mac's clipboard.

Step 3:

Paste into your slide in Keynote. Keynote will create a new text box with your highlighted code. You can adjust the width as needed, and adjust the font size with cmd+ and cmd-

@pmoran
pmoran commented Jan 18, 2012

Perfect - thanks! (Google indexed within 6 hours too). fyi I had to 'brew update' to avoid boost SHA mismatch error.

@snookca
snookca commented Sep 21, 2012

Also: "pbpaste | highlight --syntax=js -O rtf | pbcopy" I'll have to set this up as an alias to quickly run a few things through the highlighter.

@h2rd
h2rd commented Feb 3, 2013

it's very useful. Thanks.

@felixyz
felixyz commented May 29, 2013

Great tool! Here's just a dump of the params I use, in case it helps/inspires anyone:

highlight -O rtf highlight.rb --line-numbers --font-size 24 --font Inconsolata --style solarized-dark -W -J 50 -j 3 --src-lang ruby | pbcopy

@aoberoi
aoberoi commented Sep 5, 2013

I noticed that you lose the background color of the theme from highlight, is there any way to keep that?

@mikeyp
mikeyp commented Sep 24, 2013

I use pygments instead of highlight, which is easy to install via python. With that in place you can use an Applescript like this: http://apple.stackexchange.com/questions/94222/how-to-get-automator-to-treat-text-as-rtf/94246#94246.

I have that in my Pages (could be Keynote) scripts folder and then I enabled the scripts menu. Now I just select the text I want to format and hit a menu item and it highlights it for me!

@proteneer

What do you do when you can't brew install to /usr/local/ (the default location in python) and you don't want to install something like anaconda?

edit - well fml didnt realize this required boost. there goes an hour.

@proteneer

does this work on mavericks?

@oytuneren

Yes it works on mavericks too. Thank you for sharing.

@derek
derek commented Nov 1, 2013

If anyone needs it, here's the documentation for Highlight, http://www.andre-simon.de/doku/highlight/en/highlight.html

Very useful gist. Thanks for posting.

@rafarocha

for slides black background preferred theme anotherdark. which font style cool guys recommend?

@hadifarnoud

@snookca how do you add your filename in there?

@jongkwang

Oh... very great!!
thank you.

@simono
simono commented Mar 6, 2014

Here's my version, using Pygments https://gist.github.com/simono/9379347

@denvazh
denvazh commented Mar 12, 2014

This is really useful gist!
Thank you very much for sharing/posting it.

@reterVision

Awesome!

@mazuhl
mazuhl commented Apr 8, 2014

@hadifarnoud this one works with the filename or clipboard. Put the code in your .bash_profile.

Following on from @snookca and @Felixyz you can enter the syntax on the command line and either copy from the clipboard (light js) or pass a filename too (light js func.js).

function light() {
  if [ -z "$2" ]
    then src="pbpaste"
  else
    src="cat $2"
  fi
  $src | highlight -O rtf --syntax $1 --font Inconsolata --style solarized-dark --font-size 24 | pbcopy
}
@Chinchilla86

For me it works when I copy & paste some code directly from Xcode to TextEdit.
Also it is possible to create a textfield in Pages or Keynote and paste the code in it.
Tested on Mac OS X 10.9.1 with Xcode 5.1, Keynote 6.2 and Pages 5.2.

@backflip

If you want to use Alfred to format code from the clipboard (based on @mazuhl's comment above): https://dl.dropboxusercontent.com/u/1806977/Highlight%20Code.alfredworkflow

In case it doesn't work: Make sure $PATH is set accordingly. Alfred apparently does not know about your $PATH, that's why the workflow starts with export PATH=$PATH:/usr/local/bin. However, if highlight is on a different path, this has to be changed.

@lrytz
lrytz commented Nov 8, 2014

Here's how to set up a keyboard shortcut to highlight the current selection directly within Keynote: https://gist.github.com/lrytz/d82c1adf7337a4cecace

@nikcorg
nikcorg commented Nov 13, 2014

On mac, you can quickly preview a HTML version with the help of netcat:

(cat <source> | highlight --inline-css | nc -l 5555)& open http://localhost:5555

@zangbianxuegu

highlight: Could not read input file:

@lkuper
lkuper commented Sep 12, 2015

I've done this for years with pygmentize: pygmentize -f rtf <filename> | pbcopy does the trick, although some upthread commenters have fancier variations.

@prodis
prodis commented Dec 6, 2015

Thank you. This helped me to create my last presentation.

@cksachdev

@aoberoi Did you figure out a solution to persist background color using highlight?

@M2shad0w

Thank you. This helped me to create my last presentation.

@freak4pc

Anyone has ideas for ES6 support ?

@mcspronko

Thanks for advice. Works perfect for my slides.

@satblip
satblip commented Feb 10, 2016

Thanks for the share!

@naabys
naabys commented Mar 1, 2016

I tried and succeeded in copy-pasting from PHPStorm and XCode aswell [ MacOS 10.11.3 ].
@freak4pc : works in Atom too with the copy-as-rtf package, which uses pygments under the hood.

It seems there is a Sublime package doing the same.

As far as I know, this is not working with Netbeans

@fancymax
fancymax commented Jul 5, 2016

Thanks,very helpful~

@AlexanderTserkovniy

Amazing!

@cblanc
cblanc commented Sep 6, 2016

To format JSON with human readable spacing and breaklines (requires python 2.6+)

pbpaste | python -m json.tool | highlight --syntax=json -O rtf | pbcopy
@gyzerok
gyzerok commented Feb 21, 2017

Any suggestions about which highlight theme is better to use for good contrast on white slides?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment