How to show (or hide) the Line Numbers in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. How to show (or hide) the Line Numbers in Jupyter Notebook
  2. Show the Line numbers in Jupyter Notebook via the Command Palette
  3. Show the Line numbers in Jupyter Notebook via the top menu
  4. Show the Line Numbers by default in JupyterLab
  5. Show the Line Numbers by default in Jupyter Notebook in VS Code
  6. Show the Line numbers in Jupyter Notebook in VS Code using settings.json
  7. Showing the line numbers only for the current project in VS Code

# How to show (or hide) the Line Numbers in Jupyter Notebook

The easiest way to show or hide the line numbers in Jupyter Notebook is to:

  1. Press the Esc key to enter command mode.
  2. Press the L key to toggle the line numbers.

show line numbers by pressing l

You can also use the Ctrl + M key combination to enter command mode and then press L to toggle the line numbers.

When you press Esc (or Ctrl + M), the left border should become blue. This is when you should press L.

press esc for left border to become blue

Note that pressing Esc and then L only shows the line numbers for the current cell.

show line numbers for current cell

You can press Esc and then press Shift + L to toggle the visibility of the line numbers for all cells.

show line numbers for all cells

If you use Jupyter Lab, simply press Shift + L to toggle the visibility of the line numbers.

# Show the Line numbers in Jupyter Notebook via the Command Palette

You can also use the Command Palette to show the line numbers:

  1. Press Ctrl + Shift + P (or Cmd + Shift + P on macOS).
  2. Type line numbers into the search field.

search line numbers in command palette

  1. Click on toggle all line numbers if you want to show the line numbers for all cells.

If you only want to show the line numbers for the current cell, click on toggle line numbers.

When using the Command Palette, the keyboard shortcuts of the commands are shown on the right-hand side.

Notice that there are also hide all line numbers and show all line numbers commands that aren't bound.

Here is a short clip that demonstrates how this works.

show line numbers using command palette

If you need to view a list of all of the available keyboard shortcuts:

  1. Press Ctrl + m and release the keys.
  2. Press h.

view all keyboard shortcuts in jupyter notebook

# Show the Line numbers in Jupyter Notebook via the top menu

You can also use the top menu to show or hide the line numbers:

  1. Click on View in the top menu.
  2. Click on Toggle Line Numbers.

click view toggle line numbers

# Show the Line Numbers by default in JupyterLab

If you use JupyterLab, you can show the line numbers by default by editing your settings.

  1. Click on Settings and then click on Advanced configuration.

  2. Click on the JSON settings editor button in the top right corner of the Advanced Settings Editor to show the User Overrides editor.

  3. Add the following code in the User Overrides editor to the right.

user-overrides.json
{ "codeCellConfig": { "lineNumbers": true } }

# Show the Line Numbers by default in Jupyter Notebook in VS Code

To show the line numbers in Jupyter Notebook in VS Code:

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type notebook line numbers and set the Notebook: Line Numbers setting to on.

show line numbers jupyter notebook vscode

  1. Restart VS Code if necessary.

# Show the Line numbers in Jupyter Notebook in VS Code using settings.json

You can also show the line numbers in Jupyter Notebook in VS Code by editing your settings.json file.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Add the following key-value pair to the settings object.
settings.json
"notebook.lineNumbers": "on",

Make sure to remove the trailing comma if the property comes last.

show line numbers using settings json

# Showing the line numbers only for the current project in VS Code

You can use a local settings.json file if you only want to show the Jupyter Notebook line numbers for the current project.

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Add the following code to your settings.json file.

.vscode/settings.json
{ "notebook.lineNumbers": "on" }

show line numbers only for current project

Note that any settings you add to your local .vscode/settings.json file only apply to the current project and override any global configuration.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.