Open In App

How to Add Code Block in Google Docs

Last Updated : 20 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Writing code is an integral part of any software development or IT-related educational content. Google Docs, as one of the most widely used word processing tools, offers a variety of features to enhance the way we share and collaborate on documents. However, for developers and technical writers, one common challenge is adding formatted code blocks to their documents. This feature is crucial not just for clarity, but also for maintaining the integrity of the code being shared.

Here, 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.

How to Add Code Block 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.

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

Google Workspace 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 blocks

Install code block

Step 4: Allow Download

Give permissions to allow download by clicking on Continue.

Allow Download

click continue

This will prompt you to choose a Google account.

choose a Google account

choose a Google account

Step 5: Sign In

Click on Continue to sign in to your account.

sign in to your account

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.

Click Allow to proceed

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 the desired document

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.

Enter and Select the 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.

Go to the Extension Tab and Select 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 your preferred 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 the theme

select theme

Step 7: Add Code Block

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

Add Code Block

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:

  • Step 1: Go to the Insert tab
  • Step 2: Select Building Blocks
  • Step 3: 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.

Conclusion

In summary, 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.

Also Read: How to Add Text Boxes in Google Docs to learn more about this amazing word processor!

How to Add Code Block in Google Docs – FAQs

How do I insert code blocks in Google Docs?

To insert code blocks in Google Docs, follow these steps:

  1. Open your Google Docs document.
  2. Click on Add-ons in the menu.
  3. Choose Get add-ons.
  4. Search for a code block add-on like “Code Blocks” or another suitable add-on.
  5. Install the add-on and give it the necessary permissions.
  6. Once installed, go back to Add-ons, select the code block add-on, and choose Insert code block.
  7. Configure the code block settings if needed and paste your code.

Can you run code in Google Docs?

No, Google Docs does not support running code directly within documents. It is primarily a word processor for creating and editing documents.

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

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads