179

Is there way to switch on syntax highlighting in Google Docs?

Personally I use it for editing and collaborating on C++ files, but PHP or SQL syntax highlighting also would be interesting.

migrated from superuser.com Sep 22 '11 at 19:34

This question came from our site for computer enthusiasts and power users.

12 Answers 12

104

With the new Add-Ons for Google Drive, you can get code highlighting with the Code Pretty add-on.

After installing the add-on, you can access it by selecting the code you want to format, go to Add-ons → Code Pretty, and select Format Selection: Screenshot of using the add-on

The result looks like this: enter image description here

Lately, other alternatives have emerged, some of which have useful features. For example Code Blocks have support for different programming languages.

  • 1
    TIP: if you want to highlight code for a presentation, write the code in a doc then copy/paste into the presentation. – Árni St. Sigurðsson Dec 1 '14 at 10:11
  • 22
    Permissions : -View and manage your documents in Google Drive. -View and manage data associated with the application. Do you want to open my refigerator also ? – Hernán Eche Jul 6 '15 at 18:41
  • 6
    @HernánEche: How else do you think it can access the document to manipulate it to do its job? – Bdoserror Dec 3 '15 at 18:21
  • 6
    It should only ask for access to the document where doing the highlight, it has no sense to access all files for painting some letters in a single document, en.wikipedia.org/wiki/Principle_of_least_privilege – Hernán Eche Dec 4 '15 at 15:28
  • 7
    @HernánEche: It looks like it needs a full set of permissions in order to install itself in the menu bar: developers.google.com/apps-script/add-ons/… – Vidar S. Ramdal Dec 4 '15 at 17:14
26

I've found codepad.org which allows to share code snippets. It supports syntax highlighting and even compilation of code. And it supports wide range of programming languages — C, C++, D, Haskell, Lua, OCaml, PHP, Perl, Python, Ruby, Scheme, Tcl. And one more great feature — it does not require to login.

  • This is a good answer. A big missing language is Java. Would be nice to see C# there as well. Ruby? Com'on! – MikeMurko Oct 15 '14 at 15:26
  • what's wrong with Ruby ? (especially in 2014) – vasilakisfil Dec 18 '18 at 9:22
20

You can add-on Code Block for Google Docs.

1*- Google Docs --> Add-ons Tab --> 2*- Get Add-ons ... --> Search For: 'Code Blocks'

3*- Then you go to Add-ons --> Code Block --> Start

4*- Select Theme(50+)

enter image description here

  • unable to revert or change the Theme once it has been applied and also vary the highlighting differently on different code block – diEcho Sep 22 '17 at 10:08
10

No. For programming online when out and about, check out Mozilla Bespin

EDIT: Mozilla Bespin is dead, it was merged in Cloud 9 IDE.

  • 1
    I don't need hosting for whole project. I need to share one or two files. Google Docs is very simple to use and appropriate for that purpose. Except for syntax highlighting. – Kirill Jul 21 '09 at 14:08
  • Then only put on or two files on Bespin. Google Docs does not support syntax highlighting at all (and i cannot see if supporting it in the future) – Josh Hunt Jul 21 '09 at 14:47
  • Have to agree here, it's not really within the scope of Google Docs. – Stefan Thyberg Jul 21 '09 at 20:39
  • 1
    Bespin project are no longer exist. Cloud9 IDE, in which Bespin was merged by the moment of writing this supports only javascript. – om-nom-nom Mar 7 '12 at 13:05
  • 1
    FURTHER UPDATE: from mozillalabs.com/en-US/skywriter/... The Mozilla Skywriter project, formerly known as Bespin, has been merged into another project. If you're looking for a customizable editor that you can embed in your own projects, take a look at Ace.ajax.org If you're looking to do your coding in the cloud, take a look at Cloud9.IDE.com which builds on Ace (and Ace includes code from the Bespin/Skywriter project). – Eric Bishard May 17 '14 at 19:47
9

You can make use of the the question or answer fields on Stack Exchange sites to help out.

  1. Paste your code snippet into a blank text field.
  2. Select it, and use the "code sample" (the one with curly braces) button to ensure it gets formatted as code.
  3. Wait a little while to let the syntax highlighting to do its thing.
  4. Copy the previewed question/answer code-block.
  5. In your Google Doc, on a new line, set the paragraph style to block-quote.
  6. Paste the previewed code-block.
  7. That's it.

Google Docs seems to reset the font for some reason, but preserves the syntax highlighting. Good for including code snippets into documentation.

  • 6
    That's very tedious, and you'd have to go through this process again every time you make any non-trivial change to your code. – musicfreak Feb 10 '10 at 23:05
6

Just to be clear for rookies like me,

No, there is no code highlighting natively in Google Docs.

There are add ons that can do code highlighting.

Code Pretty is mentioned above and has adequate reviews.

Code Block is also mentioned and has good reviews.

If you don't want to install add ons it gets harder. http://markup.su/highlighter/ is mentioned above as being good, and it does a pretty good job. You just have to copy and paste the code each time there is a change.

If you want code formatting just for documentation purposes, these solutions will work.

If you want the real time collaboration, there are much better solutions for code than Google Docs.

Check out solutions like https://codeshare.io/

  • It is already known. But we're looking for a solution to that. – Rexford Jul 19 '16 at 19:46
4

I faced this problem too, and I didn't want to add any extension to my Google Docs account. I found http://markup.su/highlighter/ , an online code syntax highlighter, you just past your code and can chose between different styles.

I personally use the style "Dawn", then past the output in a tab in the doc, remove the borders and set a background to this tab. The result is good.

3

I've found out that copying pastes from Pastebin works perfectly, and as an added bonus it adds line numbers that are recognized by Google Docs as numbered list.

You go to Pastebin, create an unlisted paste (if you don't want anyone else to see it), select syntax highlighting from large number of available languages and syntaxes, hit Submit, then select and copy highlighted text into your Google Document.

2

I tried Code Pretty recommended in here, it's OK. I wish it were open source so somebody could EASILY program in a hot-key to use like others said. Makes it very useful for the professional world, e.g., coding up how-to or wiki-like documents teaching others how to automate/code things.

I tried Code Blocks instead. I like it much more. Much easier to tell code apart from the document for training or writing your own notes when learning another new language.

1

For something simple try rextester. It allows to collaborate real-time on code snippets written in many languages. Go to 'run code', choose your language and then hit 'Live cooperation' button.

  • Why would someone want to choose that option? – ale May 23 '12 at 17:48
  • @Al Everett Because it allows to collaborate on code snippets real-time. Although this doesn't answer the question directly, but suggests an alternative. – ren May 23 '12 at 20:26
  • Then please edit your answer to include that information. – ale May 24 '12 at 13:04
1

Vim can output its syntax-highlighted buffer to an HTML file:

http://vim.wikia.com/wiki/Pasting_code_with_syntax_coloring_in_emails

or this one is also nice:

https://superuser.com/questions/565050/export-file-syntax-highlighting-to-html-using-vim

This file can be opened in your browser, from where you can copy and paste the text over to Google Docs/Drive.

1

Well there is a web app that was projected to be included on Google Docs but got canceled :/ It's name is Collide. You can see it in action here

  • 1
    It doesn't help much to give an answer that offers a solution that is already been cancelled. Our goal on all of the StackExchange Network is to provide helpful and relevant solutions. One way to help is to think if you asked the question would you like an answer that tells you that there was almost a solution but it was cancelled, however, you can still see what it would have been like "here". – Bradley A. Tetreault Oct 24 '12 at 16:12
  • 1
    My mistake, it got canceled 'by google' but now is an open source project. Check the Collide link. – P3t3rU5 Dec 23 '12 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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