Jupyter Notebook tips and shortcuts

Share this post:

In our previous article, we covered the basics of launching Jupyter Notebook. Now, let’s move on to explore its features and some of the most commonly used keyboard shortcuts.

Types of cells

Jupyter Notebook relies heavily on the concept of cells. The 2 main types of cells are Code and Markdown.

  • Code cell: for running code snippets, e.g. python code
  • Markdown cell: for displaying formatted text and media assets (learn about Markdown syntax here)

Interface of Jupyter Notebook
Differences of Code cell and Markdown cell in Jupyter Notebook

Apart from using the menu bar options, you may also use the following keyboard shortcuts to change the cell type:

Press y : Change cell to code
Press m : Change cell to markdown

Keyboard shortcuts to change the cell type of Jupyter Notebook
Change cell type in Jupyter Notebook using keyboard shortcut

Keyboard input modes (Edit / Command)

Jupyter Notebook has 2 keyboard input modes:

Edit mode (cell with green border)
Allow you to type code or text into a cell. You can enter into edit mode by clicking the text area of the selected cell.

Jupyter Notebook Edit mode
Edit mode in Jupyter Notebook

Command mode (cell with blue border)
Allow you to perform actions like selecting multiple cells, copying and pasting cells, adding or deleting cells, etc. To perform the above actions, you need to first enter into command mode by pressing ESC or clicking the left border.

Jupyter Notebook Command mode
Command mode in Jupyter Notebook

Add/Delete cell

To add new cell, press the “+” button on the menu bar.

Click the + button to add cell in Jupyter Notebook
Click the + button to add cell in Jupyter Notebook

For keyboard shortcuts:
(1) Select the target cell
(2) PressESC to switch the cell to command mode
(3) Use the following shortcuts:

  • • Press a: Insert a cell above the selected cell
  • • Press b: Insert a cell below the selected cell
  • • Press d d: Delete the selected cell(s)

Use keyboard shortcuts to add and delete cells
Use keyboard shortcuts to add and delete cells

Run cell

Click the “Run” button on the menu bar to run individual cell.

run cell button in Jupyter notebook
The “Run” button in Jupyter Notebook

For keyboard shortcuts:
Press Ctrl+Enter: Run selected cells
Press Shift+Enter: Run cell, select below
Press Alt+Enter: Run cell and insert a cell below

Use keyboard shortcuts to run cell

If you would like to run all the cells in the notebook at once, simply go to “KernelRestart & Run all” on the menu bar.

Run all cells at once in the notebook
Run all cells at once in the notebook

Shortcuts for the “Run All” action:
1. Press ESC: Enter into command mode
2. Press Ctrl+A: Select all cells
3. Press Ctrl+Enter: Run selected cells

Run all cells at once in the notebook using shortcuts
Run all cells at once in the notebook using keyboard shortcuts

You may also create a custom shortcut for this action by going to “HelpEdit Keyboard Shortcuts” on the menu bar.

Edit keyboard shortcuts in Jupyter Notebook

Move cell

You may click the up and down arrow buttons on the menu bar to rearrange the order of the cells.

Move cell in Jupyter Notebook

Shortcuts summary

Below are some commonly used keyboard shortcuts.
You may view all the default shortcuts by going to “Help” → “Keyboard Shortcuts” on the menu bar or by clicking the shortcut h.

For Mac users, please map your keystrokes as follows:
Ctrl⌘ Command
Shift⇧ Shift
Alt⌥ Option

Shortcuts Actions
ESC Switch from Edit mode to Command mode
y Change to Code cell
m Change to Markdown cell
a Insert a cell above the selected cell
b Insert a cell below the selected cell
dd Delete cell
x Cut cell
c Copy cell
v Paste cell below
Shift+v Paste cell above
Shift+m Merge selected cells / If only one cell is selected, merge current cell with cell below
Ctrl+Enter Run selected cells
Shift+Enter Run cell, select below
Alt+Enter Run cell and insert a cell below
Ctrl+/ Comment
Tab Indent
Shift+Tab Dedent
Ctrl+d Delete whole line

Let’s try it out! Increase your efficiency and productivity levels when using Jupyter Notebook.

 

– By Holly Chan, Library

August 3, 2023