112

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

72

Try this online Syntax highlighter:

http://tohtml.com/

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

0
45

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.');
```

1
  • 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. Commented Jan 14, 2014 at 12:34
16

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.

4
  • 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
    Commented May 30, 2011 at 20:58
  • This might seem convoluted but have you tried pasting it to KeyNote then copying it from there and pasting to Gmail?
    – R.K.
    Commented May 30, 2011 at 21:03
  • I can copy a whole text box and it's pasted as an image. The formatted text however will be pasted as plain text. Copying HTML works though..
    – slhck
    Commented May 30, 2011 at 21:19
  • If anyone is looking for a solution for Windows and Notepad++, select the text, go to Plugins -> NppExport -> Copy HTML to Clipboard and then paste into GMail. Works a treat!
    – clayRay
    Commented Mar 14, 2022 at 5:06
8

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
  • I like it.. you could also add this code.google.com/p/zeroclipboard to be able to copy to clipboard with a button if flash is available :)
    – Lipis
    Commented Jun 27, 2011 at 9:29
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
    Commented Jun 22, 2015 at 9:57
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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