If you’re a developer, an instructor or anyone in the IT field who might need to include various code blocks to Google documents, what do you do? Do you write the code and format it, using the standard built-in formatting functions in Google Docs? You might, but that’s not always the most efficient way to go. Your code could be malformed, which does you no favors — especially when you’ve spent time crafting elegant and clean code.

Add an extension: Code Blocks

Fortunately, there are plenty of tools in the Google Workspace Marketplace that will do the job. The add-on I’ve found to be the best is Code Blocks. With this extension, you can select a block of code and format it to your liking within your document. It’s easy to use and best of all, it’s free — all you need is a Google account.

The Code Blocks feature set includes:

  • Syntax highlighting.
  • Automatic language detection.
  • Plenty of color themes to choose from.
  • Ability to format code inline or as blocks.
  • Preview function.

Let me show you how to install and use this Google Docs add-on, so you can include your code in cloud-based documents.

How to install Code Blocks

Installing Code Blocks is quite simple:

  1. Open a browser that’s logged in to your Google account, and point it to the Code Blocks page in the Google Workspace Marketplace.
  2. Click Install, and when prompted, click CONTINUE. You will be required to select an account for the add-on.
  3. Once you’ve selected your account, you’ll then have to accept the necessary permissions for Code Blocks, which include:
  • View and manage documents in the application.
  • Allow the application to run when you aren’t present.
  • View and manage data associated with the application.
  • Display and run third-party web content in prompts and sidebars inside Google applications.

If you’re OK with those permissions, scroll down and click Allow. Code Blocks will install and is ready to use.

SEE: Learn how to check the word count in Google Docs.

How to use Code Blocks in Google Docs

Using Code Blocks is equally simple. Open Google Docs and create a new document. With the document open, click Extensions | Code Blocks and then select Start from the menu. A new right sidebar will open (Figure A), where you can make use of the tool.

Figure A

Code Blocks is installed and ready to go.
Code Blocks is installed and ready to go.

To use Code Blocks, write or paste your code in the document. Once you have the code in place, select the complete section of code and then:

  1. Select the language from the Language dropdown.
  2. Select a theme from the Theme dropdown.
  3. Click Preview to view the code in the preview pane (Figure B).

Figure B

Formatting your code in Code Blocks.
Formatting your code in Code Blocks.
  1. Make any necessary changes (such as selecting a different theme).
  2. When the code looks exactly how you want, click Format.

The code block should now be formatted, exactly how you like it, in your document (Figure C). You’re ready to move on to the next section of text or a new block of code.

Figure C

An in-line code block, generated by the Code Blocks add-on.
An in-line code block, generated by the Code Blocks add-on.

One nice thing about the code block you just added is that you can edit it. To do so:

  1. Click inside the block and change any text you need.
  2. After you’re done editing your code block, click the X in the upper right corner of the Code Blocks add-on to close the sidebar.

And that’s all there is to adding code blocks to your Google Docs cloud-based documents. If you’ve struggled to format your code into presentable blocks, you can now breathe easy and do so with a couple of clicks.

Benefits of adding Code Blocks to Google Docs

One of the main benefits of adding a code block in a Google Document is that these blocks follow syntax rules according to the language; this means that with a code block you’ll have proper indentation and highlighting.

SEE: Explore the top programming languages for systems admins.

If you copy and paste your code directly into the document, you must manually format the code so it looks correct. Also, if someone copies your code from the document, the formatting may not stick. However, with a code block, the formatting should transfer in the copy and paste. Code blocks can be edited without messing up the formatting or highlighting.

Use the built-in Google Workspace option: Code block

Google added the built-in ability to insert a code block to a Google document in late 2022 for select editions of Google Workspace: Business Standard and Plus, Enterprise Standard and Plus, Education Standard and Plus and Nonprofits.

To add a code block, open a Google Doc in a web browser, select Insert | Building blocks | Code block. Then, select from one of five options:

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

Once the block has been created, you may enter your code in the code block directly. The built-in building block feature has fewer display options than the Code Blocks add-on, as shown in Figure D, although it doesn’t require giving a third-party add-on access permissions.

Figure D

The Code Blocks add-on offers many themes that highlight code in varying colors (first code block in the document), while the built-in code block option changes the color of fewer elements (second code block shown).
The Code Blocks add-on offers many themes that highlight code in varying colors (first code block in the document), while the built-in code block option changes the color of fewer elements (second code block shown).

Improve your Google Docs experience

Whether you prefer the Code Blocks add-on or the built-in code block option, you can now easily highlight code, so it stands out from the rest of your document in Google Docs.

Optimize your Google Docs workflow with one hour of content on “editing, formatting, styling, & more” with this deal from TechRepublic Academy.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays