How to insert a code block in Google Docs

By Jake Marsh · Updated

To insert a code block in Google Docs, click where it should go, open Extensions, Syntaxwell, Open code editor, paste the code, pick the language and theme, and press Insert. The block goes in at your cursor as a one-cell table of colored text. This works on any Google account. Google's own code blocks (Insert, Building blocks, Code blocks) are only on some paid Google Workspace plans, which is why many people can't find them.

Why the Building blocks option may be missing

If Code blocks isn't under Insert, Building blocks, your account's plan doesn't include it. Personal Google accounts don't have it, and neither do several Google Workspace plans. Where it does exist, every block has one fixed look, with no themes, line numbers or highlighted lines.

Steps

  1. Click in the Doc where the code should go, on an empty line.
  2. Open Extensions, Syntaxwell, Open code editor.
  3. Paste the code and pick a Language and Theme. Light themes such as GitHub Light print well; for a printed handout, try one of the grayscale print themes.
  4. Set the Font and Size. The sidebar tells you how many characters fit across the page at that size, and long lines wrap.
  5. Tick Line numbers if readers will refer to lines.
  6. Press Insert.

Convert code that's already in the Doc

Select the lines of code, press Use it in the blue banner in the sidebar, then Convert selection. The plain lines are replaced with a highlighted block.

Code inside a sentence

To color a short bit of code inside a paragraph, such as a function name or a command, select those words and press Format inline. They're set in the code font with the theme's colors, and the rest of the sentence stays as it was.

Edit a block later

Click into the block. The sidebar loads its code and settings; change anything and press Update block. You can also type a fix straight into the block; Syntaxwell reads the text back next time.

What it costs

Syntaxwell is $2.99 a month or $19.99 a year per Google account. Free trial: 25 code blocks or 14 days, whichever comes first. No card needed. Every plan and the install link are on the Syntaxwell page.

Syntaxwell: Code Highlighting for Slides and Docs

Syntax-highlighted code blocks for Google Slides and Docs, with line numbers, highlighted lines and click-to-edit $2.99 a month or $19.99 a year per Google account.


Questions

Can readers copy the code?

Yes. The block is text in a table cell, so it copies like any other text, and Docs' search finds words inside it.

What happens if I copy a block into another Doc?

It keeps its colors. Select it in the new Doc and press Convert selection to make it a Syntaxwell block you can edit there.

Will it export to Word or PDF?

Yes. A block is an ordinary table with styled text, so it comes along in a .docx or PDF download.

Can I add blocks from the Google Docs phone app?

No. Google Docs add-ons run in a browser on a computer. Blocks you've made show up in the phone app like any other table and text.


More Syntaxwell guides


More Google guides