How to Show Line Numbers in Google Colab

Data Scientist

Introduction

(Author's notes from November 2023)

When you write multi-line code in Google Colaboratory and go over it with other people in a meeting, you often want to refer to specific line numbers.

For work I often hold online meetings on Google Meet. There's no on-screen pen, so when commenting on someone else's code I had to describe positions by line, and without line numbers that was frustrating.

The setting in this post shows line numbers and solves the problem.

In short, there are two ways to show line numbers in Colab:

  • Keyboard shortcut: on Windows, press Ctrl + M then L; on Mac, press ⌘ + M (Ctrl + M also works) then L
  • Menu: "Tools" → "Settings" (or the gear icon at the top right) → "Editor" → check "Show line numbers" → "Save"

Updated December 2026: rechecked the steps in the current Colab UI, added a screenshot of the current settings dialog (gear icon → Editor), the Mac shortcut notation, and the fact that the setting carries over to other notebooks.

Before: no line numbers

This is how a code cell looks before the change. Line numbers are off by default.

Google Colab code cell without line numbers (2023 UI)
Default code cell with no line numbers (2023) (UI labels translated to English)

Option 1: keyboard shortcut

Mac: press command + m, then l (lowercase L)

Windows: press Ctrl + m, then l (lowercase L)

The trick is to press Ctrl (⌘) + M, release, and then press L. They are not pressed together. In Colab's keyboard shortcut list ("Tools" → "Keyboard shortcuts"), this action is registered as "Toggle line numbers", shown as "⌘/Ctrl+M L" on a Mac.

  • Repeat the shortcut to hide line numbers again (it's a toggle).
  • If nothing happens, press Esc to leave cell editing mode and try again.
  • You can change the key binding in the same shortcut list.

Option 2: the settings menu

Click Tools in the menu and choose Settings. In the current Colab you can also open the same dialog with the gear icon at the top right.

Google Colab (2023 UI) with the Tools menu open and Settings highlighted
Tools → Settings (2023) (UI labels translated to English)

Then check Show line numbers and click Save.

Colab settings dialog (2023 UI) with the Show line numbers checkbox
Checking "Show line numbers" (2023) (UI labels translated to English)

The two screenshots above are from 2023. As of December 2026, the settings dialog has a menu on the left with "Site", "Editor", "Subscription", "GitHub" and "Miscellaneous". Select "Editor" and scroll down a little to find the "Show line numbers" checkbox.

Colab settings dialog, Editor tab, with Show line numbers checked
Settings → Editor → "Show line numbers" (December 2026)

Here is the code cell after the change.
With line numbers visible, it's much easier to talk about the code.

Google Colab code cell with line numbers shown (2023 UI)
Code cell with line numbers (2023) (UI labels translated to English)

In the December 2026 UI, the line numbers 1, 2, 3… appear on the left of each code cell like this:

Google Colab code cell with line numbers shown
Code cell with line numbers on (December 2026)

The setting carries over to other notebooks

"Show line numbers" is not a per-notebook setting; it applies to the Colab editor as a whole. When we tested it in December 2026, it behaved like this:

  • Toggling with the shortcut (Ctrl + M → L) also toggled the "Show line numbers" checkbox in the settings dialog.
  • After turning it on in one notebook, line numbers also appeared when opening a different notebook.
  • Line numbers stayed on after reloading the page.

On the other hand, it's a display setting for your own screen, so it doesn't affect the people you share the notebook with. If you want to say "line 5" in a meeting, ask the others to turn it on too, or share your screen while you explain.

Other Colab shortcuts worth knowing

These shortcuts use the same "Ctrl (⌘) + M, then a key" pattern as line numbers (checked against the shortcut list in December 2026).

ActionShortcut
Toggle line numbersCtrl + M → L
Insert code cell belowCtrl + M → B
Insert code cell aboveCtrl + M → A
Delete cell/selectionCtrl + M → D
Convert to text cell / Convert to code cellCtrl + M → M / Ctrl + M → Y
Show/hide outputCtrl + M → O
Show keyboard shortcutsCtrl + M → H

Summary

This post showed how to display code line numbers in Google Colab. With the shortcut, press Ctrl (⌘) + M → L; from the menu, go to Settings → Editor → "Show line numbers". Once set, line numbers also appear in your other notebooks.
Colab has many other handy shortcuts, so it's worth looking them up, for example in the article below (Japanese).

【Google Colaboratory】作業が捗るショートカットキーを厳選して紹介!
Google Colaboratory(略称:Google Colab)において、AI・機械学習の講師が、便利なショートカットキーのみ厳選して、GIF や画像を盛り込んで解説しています。Google Colab を使用している方々の参考になれば幸いです!

For more on using Colab, see how to set up an analysis environment with Google Drive and Colab, how to upload and read files in Google Colab and Google Colab pricing and free tier limits.

I hope this helps.

Title and URL copied