How do you paste syntax highlighted code into Keynote '13?

Solution 1:

The 'short' answer

... is that different apps copy formatted text to the clipboard in different ways, and different apps support pasting from different formats. Typically you'll get some kind of 'plain text' format, as well as maybe HTML or RTF formatted text in the clipboard, and it's up to the pasting app to choose which it likes best.

Keynote 6.1 definitely supports some kind(s) of formatted text pasting, as I can get this to work with Safari. But it doesn't seem to work with Firefox or Chrome (at the time of writing). From a bit of testing, it seems the Apple apps all play nicely together (no surprise there), and a few others will copy/paste to/from the Apple apps too (including Microsoft Office products). Unfortunately when you copy from Chrome, you only get plain text & HTML-formatted text copied to the clipboard, so it would appear Apple's iWork apps don't support pasting HTML formatted text.

So if copy/paste doesn't include formatting, you can either:

  • Try a different browser to copy from, such as Safari; or
  • Find an intermediate app to paste into (which 'speaks' both HTML formatting and can re-copy into a format Keynote/etc understand), such as Apple's own TextEdit.app

Surprisingly, Apple's own TextEdit.app will happily paste formatted text from Chrome, and it can be re-copied to the clipboard and pasted with formatting into Keynote/Pages/etc.

Depending on your point of view, this may seem a limitation (or fault) in Chrome/Firefox or in Pages/Keynote/etc, but in the end they simply don't speak the same formatting languages (at present).

I'd encourage anyone experiencing this to send feedback to the developers on both 'sides' in the hope of getting this fixed—either with Chrome/Firefox supporting RTF/RTFD copying (or whatever is most standard/appropriate beyond HTML), or by Apple's iWork apps supporting the pasting of HTML content.


More than you want to know about copying to the clipboard

When you copy some text to the clipboard, depending on the application, it may get copied in a few formats. You can see these using Apple's Clipboard Viewer application (requires building in Xcode).

For example, copying from Google Chrome, the clipboard gets populated with four separate copies of the information I copied—two plain text (public.utf8-plain-text, NSStringPboardType), and two HTML formatted (public.html, Apple HTML pasteboard type).

Copying from other apps gives various combinations of format types, with some other common ones being 'TEXT' (CorePastboardFlavorType), com.apple.traditional-mac-plain-text, Apple PDF pasteboard type, Apple PICT pasteboard type, public.rtf, NeXT Rich Text Format v1.0 pasteboard type, NeXT RTFD pasteboard type, com.apple.flat-rtfd, com.apple.iWork.TSPNativeData, com.adobe.pdf, com.adobe.indesign-import-ustl, and so on...

As you can see there are a variety of plain text formats (with various encodings like UTF8/UTF16), RTF types, PDF and Adobe types, image types. There's also a bunch of metadata types that describe the application that copied the data (e.g. com.apple.iWork.TSPNativeMetadata), or the features of the copied text (e.g. com.apple.iWork.TSPDescription), and what appear to be separated text and style data (ustl and TEXT as CorePasteboardFlavorTypes, and their com.adobe.indesign-import- equivalents).

I don't know any way of seeing which format types are supported by which applications for pasting, but using the Clipboard Viewer you can quickly see which format types are generated on copying.

Solution 2:

This is probably the online highlighter you are using.

I use http://markup.su and it works fine with Keynote 6.1 - it autodetects a number of languages and has a number of "themes".

If it isn't the highlighter then it may be your template or browser. I was able to take your code and paste it from markup.su in Safari into Keynote perfectly.

enter image description here

Solution 3:

If you don't like the idea of pasting your code online, consider installing the Pygments python package and generating your highlighted syntax at the command line:

pygmentize sms_me_last_line.sh

Copying from Terminal and pasting into Keynote worked for me. But it took me a while to find the formatting control for removing the background text color. (Click Format in the toolbar to show the formatting options, click Text, click Style, then click on the gear to show advanced formatting options. In that pop-up you will find "Character Fill Color".)