Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shortcut for Syntax Highlighting in Keynote

Shortcut for Syntax Highlighting in Keynote

The idea is based on a gist by @jimbojsb.

You can use Pygments or Highlight.

Install Pygments

brew install python

pip install pygments --pre (pre is currently required to get version 2.0rc1, which adds fontsize to the rtf formatter)

Pygments quick help:

  • show available styles, formatters, etc: pygmentize -L
  • show help for a formatter: pygmentize -H formatter rtf


  • pygmentize -L style
  • previews:
  • others are available: pip search pygments, e.g. pip install pygments-style-solarized or pip install pygments-style-github

Install Highlight

brew install highlight

Styles: highlight --list-themes

Create a Service

Start the Automator, File - New, choose Service.

Add Copy to Clipboard, then Run AppleScript.

Paste the script below, adjust to your preferences:

-- for pygmentize
set command to "/usr/local/bin/pygmentize -l scala -f rtf -O style=github,fontface=\"Source Code Pro\",fontsize=48"

-- for highlight
-- set command to "/usr/local/bin/highlight --syntax scala --out-format rtf --style solarized-light --font \"DejaVu Sans Mono\" --font-size 24"

do shell script "/bin/bash -c 'pbpaste | " & command & " | pbcopy'"

tell application "System Events" to tell (process 1 where frontmost is true)
  click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1
end tell

Save the service as Syntax Highligh (or a different name).

See Screenshot 1

Go to System Preferences - Keyboard - Shortcuts. Under Services, look for your new service and add a shortcut.

See Screenshot 2

Go to System Preferences - Security & Privacy - Privacy. Under click the lock on the bottom left. Under Accessibility, add Keynote (or PowerPoint) to the list of apps.

See Screenshot 3

In Keynote, create a text box and add some code. Select the code and hit your shortcut. That should replace the text by its highlighted version.


This comment has been minimized.

Copy link

commented May 2, 2016

For the record, PowerPoint 2016 does not enable "Text" services, even when a text box has the focus. For compatibility with PowerPoint create the service with "Service receives: no input" and use a "tell application..." command to perform the "Copy" (similar to how "Paste" is performed). This sets the service type to "General", thus making it available to PowerPoint.


This comment has been minimized.

Copy link

commented May 2, 2016

Another addition for PowerPoint users: There's a bug that only shows up when pasting RTF output from Pygments into PowerPoint which prevents styles from being disabled in the same line, so that, for example, everything after the first bold token in a line is also bold. I don't know if the problem lies with Pygments' or PowerPoint's interpretation of RTF but pasting from other sources or into other apps works fine. To work around this issue, you can run the Pygments output through OSX's built-in textutil for RTF sanitation:

do shell script "/bin/bash -c 'pbpaste -Prefer ps | " & command & " | textutil -stdin -stdout -format rtf -convert rtf | pbcopy'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.