Use code blocks to add code to Google Docs

If you’re having trouble adding well-formatted code blocks to Google Docs, check out this easy-to-use free add-on from the Google Workspace Marketplace.

Image: iStockphoto/monstArrr_

If you’re a developer, an instructor, or someone in the IT field who may need to include various blocks of code in Google Docs, what do you do? Are you writing the code and formatting it using standard built-in formatting features in Google Docs? You might, but it’s not always the most efficient way to go. Your code can be buggy, which doesn’t do you any favors—especially when you’ve spent time creating elegant and clean code.

Fortunately, there are plenty of tools in the Google Workspace Marketplace that will get the job done. The plugin that I have found to be the best is called Code Blocks. This extension allows you to select a block of code and format it however you like in your document. It’s easy to use and best of all it’s free.

The feature set of code blocks includes:

  • Syntax marking
  • Automatic language detection
  • Lots of color themes to choose from
  • Format code inline or as blocks
  • Preview feature

Benefits of adding code blocks to Google Docs

One of the main advantages of adding a code block to a Google Doc is that these blocks follow syntax rules according to the language; this means that with one block of code you get proper indentation and markup. If you just copy and paste your code directly into the document, you’ll need to manually format the code to look correct. Also, if someone copies your code from the document, the formatting may not stick. But with a code block, the formatting should be transferred in copy/paste. Code blocks can be edited without destroying the formatting or markup.

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

LOOKS: Top 5 Programming Languages ​​for System Administrators to Learn (Free PDF) (TechRepublic)

What you need to add code to Google Docs with code blocks

All you need is a Google account. This extension works with either the free version of Google Docs or the one included in Google Workspace.

How to install code blocks

Installing code blocks is pretty easy. Open a browser signed in to your Google Account and point to the Code Blocks page of the Google Workspace Marketplace. Click Install and click CONTINUE when prompted. You will need to select an account for the add-on. After selecting your account, you must accept the required permissions for code blocks, which include:

  • View and manage documents in the application.
  • Allow the program to run when you are not present.
  • View and manage data associated with the application.
  • Display and run third-party web content in notifications and sidebars in Google applications.

If you are OK with these permissions, scroll down and click Allow. Code blocks will be installed and ready to use.

How to use code blocks in Google Docs?

Using code blocks is just as easy. Open Google Docs and create a new document. With the document open, click Extensions and select Code Blocks from the menu. A new right sidebar will open (Picture A), where you can use the tool.

Picture A

Code Blocks are installed and ready to use.

To use code blocks, type or paste your code into the document. Once you have the code in place, select it and then:

  1. Select language from the Language drop-down menu.
  2. Select a theme from the Theme drop-down menu.
  3. Click Preview to see the code in the preview window (Picture B).
  4. Make necessary changes (such as choosing a different theme).
  5. When the code looks just the way you want it, click Format.

Picture B

Format your code in code blocks.

The code block should now be formatted, exactly as you want it, in your document (Picture C). You are ready to move on to the next section of text or a new block of code.

Picture C

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

A nice thing about the code block you just added is that you can edit it. Click inside the block and change the text you need.

When you’re done adding your code blocks, click the X in the upper right corner of the code block extension to close the sidebar.

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *