64

In the normal Jupyter notebooks, we can add line numbers by pressing the L key after selecting the cell. All the subsequent cells for that notebook will have line numbers automatically.

But this is missing in VSCode. Every time a new cell is created, we need to select the cell and press the L key to enable the numbering for that cell which is very tiresome.

Is there a way to automatically enable line numbering for all the cells in the VSCode Jupyter notebook instead of manually pressing the L key for each and every cell?

Thank you.

11 Answers11

91

In order to toggle on/off showing the line number for all the cells(code):

  1. Click on the blank area (left side of any cell).
  2. Press Shift + L.
Hadij
  • 3,661
  • 5
  • 26
  • 48
  • 3
    It's surprising how much the user experience is improved by having the keyboard pictograms. Thanks @Hadij – John Jul 20 '23 at 05:40
59

In VS Code Jupyter Notebook, you can toggle line numbers by pressing L. The trick is to select the current cell first. You could select your cell by clicking in the blank area on the left margin of the cell (when it's selected the bar on the left turns blue). Now if you press L on your keyboard, you would be able to turn the line numbers on/off.

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
Kumar Nishant
  • 733
  • 4
  • 7
  • 13
    Great, but do you know how to activate line numbers for the whole notebook? – Veliko Nov 18 '20 at 10:43
  • I have not figured that out yet @Veliko. In Jupyter we can do that in the settings but we don't have that option in the VS Code extension. – Kumar Nishant Nov 30 '20 at 06:53
  • 5
    The OP is already aware that you can press `L`. He's asking about enabling it for the entire notebook at once. – Daniel Feb 08 '21 at 14:00
18

Adding to this as still comes out the top result for the problem without a proper solution.

Assuming you have the Jupyter Notebook extensions installed for VSCode, in version 1.62.3, you can go into the Settings (Ctrl+,) search for "line numbers" and find the option for "Notebook:Line Numbers" and toggle this on.

This will show line numbers for all code cells in the Notebook by default so no need to keep pressing L

screen shot of Notebook line number toggle option

user17571854
  • 181
  • 1
  • 2
  • 1
    I just used this answer on my Mac (open Preferences, Settings using Command ,). It works fine for me. – Jack Chidley Jan 10 '22 at 09:15
  • 1
    This should be the accepted answer. It is complete, with version numbers for when features were added, and answers the original question in a more permanent way than all the other answers here. – twieg May 18 '23 at 16:24
8

As of 2021-01-04, this is sadly not possible.

Subscribe to the GitHub issue (Feature Request) Line Numbers - All Cells #1593 for updates.

Related: Line numbers should be enabled for current cell and subsequent cells when turned on #1659

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
8

I always try any combination with modifiers keys. Found out that pressing Shift + L enables the lines number for all the notebook at once

7

There is an unbound command to toggle line numbers:

notebook.cell.toggleLineNumbers

BTW, beginning in v1.75 the setting

notebook.lineNumbers  can show relative line numbers 

VS Code can now use all editor line number options when notebook.lineNumbers is turned on. For example, with "notebook.lineNumbers": "on" and "editor.lineNumbers": "relative", relative line numbers are displayed in cell editor.

from v1.75 Release Notes: Cell Editor Polish


You can now temporarily toggle the line numbers of a cell in the current session from the cell toolbar, or change the visibility of line numbers for all notebooks through the notebook.lineNumbers setting.

Under the ... in the floating menu is a line number toggle. [The gif is too large for SO.]

release notes cell line numbers

Mark
  • 143,421
  • 24
  • 428
  • 436
5

1- Open VSCode. 2- Press Ctrl+Shift+P 3- Type "open user settings". Then type "notebook: line numbers".

So you can configure line numbers settings for your notebook. enter image description here

Berkay Cihan
  • 51
  • 1
  • 2
2

Adds example for @mark answer, you can enable for current work space using:

  1. Make new folder name .vscode
  2. Make a setting file inside that folder called settings.json
  3. Copy paste code in the bottom of this post.
{
    // other setting can be placed here
    "notebook.lineNumbers": "on"
}

Edit:

To open settings.json without manually creating the file is by opening workspace or user settings:

  1. open Command Palette (Ctrl+Shift+P)
  2. Type Preferences: Open User Settings (JSON) or Preferences: Open Default Settings (JSON)
  3. Copy paste the snippet setting.
Muhammad Yasirroni
  • 1,512
  • 12
  • 22
2

As for 2022:

In the upper left corner, select the config-weel. One of the options you can choose is 'Show Notebook Line Numbers' Sweet and easy.enter image description here

1

I tried CTRL + A (outside cells): select all cells and right clicked "show cell line number". It displayed line number for every cell.

adheir
  • 11
  • 1
  • This does not provide an answer to the question. You can [search for similar questions](https://stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](https://stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](https://stackoverflow.com/tour) – cursorrux Mar 15 '22 at 11:46
1

Enter empty space on Jupiter notebook .ipynb file

Click:

F1

Then write:

Show Cell File Numbers