How to add 2-space indentation quickly in Google Docs

I need to write code in Google Docs for my class. My instructor requires us to use Google Docs instead of coding software. I know it sounds weird but that’s what we have to do.

I always use 2 spaces for indenting my code instead of tabs. But I can’t figure out how to do this fast in Google Docs. Is there a keyboard shortcut or maybe some kind of plugin that can help?

Here’s my problem: when I write an if statement and press Enter to go to the next line, the cursor goes back to the start of the line. I want it to stay at the same position so I can just add 2 more spaces for the code inside the if block. Right now I have to count spaces manually every time and it takes forever.

Does anyone know a trick to make this easier?

Indeed, Google Docs can be limiting when it comes to code formatting. A useful workaround is to utilize the ruler for defining precise indentation. Navigate to Format > Align & indent > Indentation options to adjust your indentation to match the 2 spaces you need. Additionally, the shortcuts Ctrl+] and Ctrl+[ can facilitate quick indenting and outdenting, respectively. I’ve found it effective to prepare templates with pre-configured text blocks, such as using invisible tables for managing code indentation more efficiently without the hassle of counting spaces. Consider establishing your document structure first to streamline your process.

I create custom paragraph styles with exact spacing. First, go to Format > Align & indent > Indentation options and set left indent to 0.14 inches (roughly 2 spaces). Then hit Format > Paragraph styles > Normal text > Update to match. For nested blocks, make another style at 0.28 inches for 4 spaces, etc. Once you’ve saved these styles, just use the styles dropdown instead of manually spacing each line. Takes some setup upfront but saves tons of time on longer code blocks. Just make sure you get the measurements right for your font size.

docs isn’t the best for coding, but try using a monospace font like Courier New. You can also set custom indents by dragging the markers on the ruler. It’s not ideal, but way better than counting spaces!