113

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.

Blindspots
  • 7,737
  • 5
  • 20
  • 39
slhck
  • 3,946
  • 3
  • 26
  • 32

6 Answers6

72

Try this online Syntax highlighter:

http://tohtml.com/

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

serenesat
  • 10,042
  • 30
  • 34
  • 50
Matt
  • 2,575
  • 18
  • 25
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.');
```

slhck
  • 3,946
  • 3
  • 26
  • 32
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.

R.K.
  • 1,425
  • 12
  • 24
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

slhck
  • 3,946
  • 3
  • 26
  • 32
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.

ale
  • 52,972
  • 42
  • 165
  • 314
Pez Picacious
  • 51
  • 1
  • 1
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.

YoCoh
  • 109
  • 2