How to Insert Code in Google Docs (Code Blocks and Formatting)

To insert code in Google Docs, click Insert > Building blocks > Code block, choose a programming language, and paste or type your code into the box. Docs formats it in a monospaced font inside a shaded block and adds color highlighting for supported languages. You can also type @code in an empty line and pick Code block from the menu. For short snippets inside a sentence, change the text to a monospaced font such as Courier New or Roboto Mono.

Below are the steps for each method, how to keep code from being “corrected” by Docs, and when an add-on is worth it.

Method 1: Use the built-in code block

  1. Open your document and click where you want the code.
  2. Click Insert > Building blocks > Code block.
  3. Choose a language from the list, such as Java, JavaScript or Python, or a plain option.
  4. Type or paste your code into the block. Indentation and line breaks are kept.
  5. Click outside the block to keep writing normal text.

Shortcut: type @code on a new line and press Enter on Code block. The list of languages is limited, so choose the closest option or a plain block for anything that isn’t listed.

Method 2: Format code with a monospaced font

This works for inline snippets, like a function name in a sentence, and for any language.

  1. Select the code.
  2. Open the Font menu on the toolbar and choose a monospaced font: Courier New, Consolas, Roboto Mono or Source Code Pro. Click More fonts to add one that isn’t listed.
  3. Optionally add a light background with the Highlight color tool to make it stand out.

Make a reusable code style

  1. Format one line of code the way you like it.
  2. Select it, click the style menu (it says Normal text), hover over a style you don’t use, such as Heading 6, and choose Update to match.
  3. From then on, apply that style to any code paragraph with one click.

Method 3: Put code in a one-cell table

For a boxed code block that works in every version of Docs:

  1. Click Insert > Table and choose a 1 x 1 table.
  2. Paste your code into the cell and set a monospaced font.
  3. Right-click the table, choose Table properties, and set a light gray Background color.

Stop Docs from changing your code

Google Docs’ automatic substitutions can turn straight quotes into curly quotes and two hyphens into a dash, which breaks code if someone copies it.

  • Go to Tools > Preferences and turn off Use smart quotes and Automatically detect links.
  • Under Substitutions in the same window, turn off the replacements you don’t want.
  • Turn off spell check for code with Tools > Spelling and grammar, or ignore the underlines.

Built-in code blocks avoid most of these issues automatically.

Method 4: Use a code formatting add-on

If you need syntax highlighting for a language that code blocks don’t support, add-ons such as Code Blocks from the Google Workspace Marketplace can format many languages and themes. Click Extensions > Add-ons > Get add-ons and search for one. Review the permissions it asks for before installing, especially on a work account.

Which method should you use?

Need Best method
A multi-line snippet in a common language Insert > Building blocks > Code block
A function or command name in a sentence Monospaced font
Many code samples in one document A custom code style
A boxed snippet that prints cleanly One-cell table
Highlighting for less common languages A code formatting add-on

Related: turn on word wrap in VS Code.

Related: remove space between paragraphs in Google Docs.

Frequently asked questions

Can I run code in Google Docs?

No. Docs only displays code. Use an editor such as VS Code, or Google Colab for Python, to run it.

Can I add code blocks in the Google Docs mobile app?

Building blocks are easiest to insert on a computer. Code blocks you add there display correctly in the mobile app.

Why does my pasted code lose its indentation?

Some sources paste with mixed tabs and spaces. Paste into a code block, or use Edit > Paste without formatting (Ctrl + Shift + V) and then apply a monospaced font.