109

I often write mails that partly consist of source code, like Java or C (or any other arbitrary code).

In order to get the formatting right, I always have to:

  • Paste the code
  • Indent it correctly
  • Change the font to a monospaced one
  • Maybe apply boldface to some keywords to improve readability
  • Hit Enter a few times and change the font back to sans-serif

Now, there are wonderful code highlighting libraries out there. Isn't there an easy way to be able to send code that is easily and beautifully formatted within mails composed in Gmail?

Update: I have some solutions now, one involving copying and pasting to an online syntax highlighter. The real solution I was hoping for is some kind of extension maybe that highlights pasted code in place, meaning I'd only have to paste to Gmail, select the code, and then click a button or hit a keyboard shortcut.

I'm on OS X, if that matters, although I guess this is easier to do with a browser-based solution.

6

6 Answers 6

69

Try this online Syntax highlighter:

http://tohtml.com/

It converts your code to HTML and uses inline styling highlight the syntax etc.

4
  • @ slhck :) no problems
    – Matt
    May 31, 2011 at 9:12
  • 1
    Sorry for the unaccept, but I've switched to a more streamlined tool, where I don't even have to copy and paste — see below!
    – slhck
    Jul 22, 2012 at 13:06
  • Perfect answer, just what I needed - once I stopped pasting the raw HTML and pasted the actual nice output :).
    – dwanderson
    Jan 19, 2017 at 21:23
  • :-( missing bash... Feb 5, 2019 at 10:56
43

Markdown Here

I've been using Markdown Here for quite some time now and it beautifully accepts Github-flavored Markdown. It will automatically prettify code one you convert your mail to Markdown.

For example:

```
javascript
alert('Hello syntax highlighting.');
```

2
  • 1
    unfortunately it removes existing formatting such as text in italics, bold or underlined when applying the markdown. Furthermore, I've noticed that it removes hyperlinks when I send the email (but not before). The workaround is not to apply it to the whole message, but only to pieces of text, which requires selecting them manually and clicking on the button several times. Jan 14, 2014 at 12:34
  • Pretty cool! I'll give this a whirl for a while. Feb 2, 2015 at 0:48
14

On Windows:

You could use Notepad++ to achieve the desired effect.

  1. Open the source file in Notepad++.
  2. Highlight the text you want to copy.
  3. Right click and select Plugin commands > Copy Text with Syntax Highlighting
  4. Paste the text into Gmail.

On OS X:

TextMate has a copy-as-RTF that people are raving about. They use it to paste code with syntax highlighting on KeyNote.

From their GitHub page:

Need to copy + paste some text and keep the syntax highlighting?

Yes you do. All the time. For Keynote presentations. And probably other reasons.

Install this bundle, and after selecting some pretty syntax highlighted text, use 
Ctrl+Alt+Cmd+R to copy it as RTF (rich text format) Now you can paste it directly  
into Keynote presentations. And other places where RTF is supported.

Should work on Gmail's composer too.

9
  • 1
    I'd have to add I'm on OS X, so a OS-independent solution would be nice, but this is a good idea nevertheless!
    – slhck
    May 30, 2011 at 18:57
  • Oh, what text editor do you use?
    – R.K.
    May 30, 2011 at 19:11
  • @R.K. TextMate, most of the time
    – slhck
    May 30, 2011 at 19:43
  • Added the TextMate solution. Do check it out :)
    – R.K.
    May 30, 2011 at 20:14
  • That's a nice find - I loved that bundle right away. But unfortunately, it doesn't work for Gmail. Googling a bit suggests that simply pasting as RTF is not feasible.
    – slhck
    May 30, 2011 at 20:58
7

StackEdit

This is a great, fully-featured online Markdown editor. Features include:

  • Import/Export
  • Sharing
  • HTML to Markdown
  • Various syntax highlighting themes
  • Document management

enter image description here

1
5

A slightly unorthodox approach, but you can copy some code from Stack Overflow

like this

and then paste that into Gmail. Then just put what you want to send into the clipboard, highlight over "like this" and then 'paste and match style'. It's a bit inelegant and doesn't have syntax highlighting but it looks quite professional in the actual email, has its own scroll bar, etc.

This is my go-to method.

1
  • 1
    SE for the win :)
    – Samveen
    Jun 22, 2015 at 9:57
1

For those of you using Mac (in my case copying code from xCode), a simple copy/paste into Apple's default "Mail" client keeps all my code's formatting and colors intact.

Not the answer you're looking for? Browse other questions tagged or ask your own question.