Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

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.

share|improve this question
2  
Use Pastebin –  Sathya May 28 '11 at 14:08
    
@Sathya Hm, I know, but I'd rather not, in order to keep things inline. –  slhck May 28 '11 at 15:45
1  
I know, that's why a comment than an answer :) –  Sathya May 28 '11 at 16:03

4 Answers 4

Try this online Syntax highlighter

http://tohtml.com/

it converts you code to html and uses inline styling highlight the syntax etc

share|improve this answer
    
Works quite well, thank you! –  slhck May 31 '11 at 8:41
    
@ slhck :) no problems –  Matt May 31 '11 at 9:12
    
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 '12 at 13:06
up vote 8 down vote accepted

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

share|improve this answer
    
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. –  Sergiy Byelozyorov Jan 14 at 12:34

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.

share|improve this answer
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 '11 at 18:57
    
Oh, what text editor do you use? –  R.K. May 30 '11 at 19:11
    
@R.K. TextMate, most of the time –  slhck May 30 '11 at 19:43
    
Added the TextMate solution. Do check it out :) –  R.K. May 30 '11 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 '11 at 20:58

Online Markdown Editor

I also deployed my own version of the Markdown editor used in StackExchange, together with a syntax highlighting library.

That way I can write my mail content, click "Select All", highlight the code if necessary, and then paste to Gmail.

http://homepage.univie.ac.at/werner.robitza/markdown/

enter image description here

share|improve this answer
    
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 Jun 27 '11 at 9:29
    
Thanks! Yeah I'll give it a try :) –  slhck Jun 27 '11 at 11:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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