Open In App

How to Add Code Block in Google Docs

Last Updated : 29 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Ready to add some code to your Google Docs? It’s easier than you think! Whether you’re a coding pro or just want to jazz up your text, we’ll walk through the simple steps to sprinkle in some code magic. Let’s make your document a coder’s playground in no time!

Using these methods is okay if you are working with a few lines of code. But formatting a lot of code like this would become quite tedious. Also, you cannot guarantee that the spaces will be preserved correctly, which is a problem, especially for languages like Python where indentation error is a thing. In this post, we will see how we can add code in Google Docs such that it not only stands out from the rest of the document but also preserves the syntax of the code just like in a code editor. And the best part? All this without having to spend hours!

How-To-Add-Code-In-Google-Docs

Benefits of Adding Code Block in Google Docs

Look at the few lines of code we have added to this document. We have added the required indentation manually and also highlighted the code to make it stand out from the rest of the document:

add-as-text

manually highlighted and indented text

Here is another code that we have added inside a table row. Note that although the table border itself helps the code stand out, we still have to add the indentation in the code manually:

add-in-table

code added in the table

Adding code in both these ways is quite simple but only when you are working with a few lines of code. Also, these methods do not guarantee that the formatting and indentation would transfer while copying and pasting this code into a code editor. Thus, to avoid all these hassles, we must use a Code Block while adding code in Google Docs.

How to Add Code in Google Docs

You can add code in Google Docs using a third-party add-on. There is also an in-built tool that enables one to add code in Google Docs but it is not available for all kinds of Google accounts. Read on to know more.

How to Use the Code Block Add-On to Add Code in Google Docs

You can find a number of tools to add code in Google Docs from the Google Workspace Marketplace, but Code Blocks is one of the most widely used add-ons. The following features make Code Blocks such a popular choice:

  • Free of cost
  • Syntax highlighting
  • Supports a range of languages
  • Provides a number of color themes
  • Allows inline as well as block formatting
  • Preview option before making changes

Follow these steps to install and add this extension to Google Docs:

Step 1: Go to the Google Workspace marketplace

Use this link to go to the Google Workspace Marketplace home page:

https://workspace.google.com/marketplace

marketplace

Google Workspace Marketplace

Make sure to log in to your Google account if you are not already logged in.

Step 2: Search Code Blocks add-on

Search for the Code Blocks add-on in the search bar and press enter. Select the first add-on.

code-blocks-add-on

Search code block add on

Step 3: Install Code Blocks

Click on the Install option to add the Code Blocks extension.

install-code-block

Install code block

Step 4: Allow Download

Give permissions to allow download by clicking on Continue.

continue

click continue

This will prompt you to choose a Google account.

sing-in

choose a Google account

Step 5: Sign In

Click on Continue to sign in to your account.

sign-in

click continue

Once you are signed in, you will see the list of things Code Block can do with your data. Click Allow to proceed with the download.

allow

click allow

You must see this window once the add-on is downloaded:

add-on-added

Add-on installed

How to Add Code to Google Docs Using Code Blocks

Now you are all set to start using it on your Google docs. Follow these steps to add code in Google Docs using the Code Block add-on:

Step 1: Open the desired document

Open the desired document from the Google Docs home page.

open-doc

open the desired document

Step 2: Enter and Select the code

You can add the code by directly typing it or by simply copying and pasting it from a code editor.

select-code

select code

Step 4: Go to the Extension Tab and Select Code Blocks

Go to the Extensions menu. You will see the added extensions in the list. Click on Code Blocks and select Start.

start-code-blocks

start code blocks

Step 5: Select your preferred language

In the side pane that opens, select the desired language from the drop-down.

select-language

select language

Step 6: Select the theme

To change the colors, you can choose a different theme from the Theme drop-down. Turn on the preview option to see how a particular theme looks. Note that you will have to click on the Preview button every time you select a theme.

select-theme

select theme

Step 7: Add Code Block

Click on Format to add the code block to your document.

preview-button

format button

Here is what the code block looks like:

result

result

How to Use the Built-In Code Block Tool to Add Code in Google Docs

There is an in-built code block tool that was added to Google Docs in 2022 for a few kinds of Google accounts like – Business Standard, Business Plus, Enterprise Standard, Enterprise Plus, Education Standard, Education Plus Customers, and Nonprofits.

To add a code block using this tool, follow these steps:

Go to the Insert tab -> Select Building Blocks -> Choose the Code Block options.

You can then further select an option from the given five:

  • C/C++
  • Java
  • JavaScript
  • Python
  • Unset

On selecting an option, a code block will be added to the document where you can add the desired code.

Although this option is not available to users with a personal account, that is hopefully going to change in the near future.

Summary

In this post, we saw how we can add code in Google Docs such that the formatting and indentation of the code are preserved and we do not have to do things manually for long lines of code. You can use third-party add-ons to add code in Google Docs and you can also use the in-built tool that Google Docs provides. Note that the in-built tool is not available for use to all kinds of Google accounts, which will hopefully change in the near future.

Read How to Remove the green line in Google Docs to learn more about this amazing word processor!

FAQs

I am unable to find the code block option from the insert menu of Google Docs. What to do?

The in-built Code Block feature of Google Docs is not available for all Google accounts. You can only use this option if you are have one of the following accounts:

  • Business Standard
  • Business Plus
  • Enterprise Standard
  • Enterprise Plus
  • Education Standard
  • Education Plus Customers
  • Nonprofits

The feature is yet to be rolled out on January 3, 2023.

Can the code formatting and highlighting be preserved using Google Docs?

You can preserve the code formatting and highlighting by using third party add-ons like Code Blocks. Another add-on that works in a similar way is Code Pretty.

Can I import code from an external file into Google Docs?

No, you cannot import code from an external file but you can insert code in a Google doc by simply copying and pasting. You can then apply formatting using the code blocks extension.

Are there any shortcuts to add code in Google Docs?

No, there are no such shortcuts. You can simply add your code by copy pasting or typing and then applying the code format on it using the code blocks add-on.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads