There is a few questions regarding inserting and appropriately formatting code in Google Docs (here and here for example). Indeed there is a very useful Google-Docs add-on called Code Blocks which works very well.

However, I would like the option to number the lines of code.

I got the idea from the output seen on this page (scroll down a bit and you 'll see what I mean). I am writing student notes for a beginners class I am teaching and it would be helpful to have similar numbers in the code formatted by Code Blocks within Google Docs.

I searched for a solution (this thread seemed promising, but no real solution).

Does anyone know of a way to have formatted code with numbered lines in Google Docs?

An (easily attainable) output like this would be ideal:

enter image description here


PS - I also searched for a 'reverse' solution, i.e. copy code WITH line numbers. My IDE is Visual Studio 2017 Community edition - and it doesn't seem to provide this option, I have only found extensions which work for Visual Studio Code (an altogether different application). However, it would be better to find a way to 'add' line numbers in google docs - this way one would not be restricted by the IDE/editor. Copying code and pasting in an editor and then re-copying with line numbers would be a choice, but way too cumbersome.

3 Answers 3


The workaround I use is codepad.org.
I paste the code, select the language and then the website outputs the code in a colour coded way along with the line numbers.

  1. I paste the code:

I pasted my code here.

  1. Then Copy the formatted code (If you want the line numbers, then include them in your selection too):

enter image description here

  1. Pasting it in Google Docs will add it as a table preserving all the formatting.

enter image description here

You can use other "syntax highlighters" too, but when pasting the code in Google Docs, you may have to adjust some line numbers manually.

  • Interesting idea, however it's a pity that codepad has support only for a few languages - I 'm quite unlucky, cause the two languages I was interested in, C# and Java, don't seem to be supported ..
    – thomas
    Commented Jan 22, 2020 at 23:00
  • @thomas , then maybe you can try other "syntax highlighter". Another thing that I have noticed is, that I have my PyCharm's theme dark, and when I copy-pasted the code to Docs, it was copied along with the text colors and the dark background color (but not the line numbers though)
    – PlopMon
    Commented Jan 24, 2020 at 16:00

What I do:

  • Prepare a list of numbers each in a new line (1, 2, 3, ...), on an editor.
  • Create a 1x2 table in Google Docs.
  • Paste the code to the second column.
  • Paste the numbers to the first column.
  • Adjust the column widths.

Result: Result


I used vim to open up the file and then use the mouse to highlight the text and copy and paste. The mouse needs to be used to highlight everything as vim's inbuilt highlight will not include the line numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.