Skip to content

Jupyter Shortcuts & Magic Commands

In data analytics, you often:

  • Rerun code many times
  • Explore different plots and transformations
  • Measure performance
  • Run shell commands

Jupyter shortcuts and magic commands help you iterate much faster.

Keyboard shortcuts (classic Jupyter Notebook)

Section titled “Keyboard shortcuts (classic Jupyter Notebook)”

Jupyter has two main modes:

  • Command mode (press Esc): navigate and manage cells
  • Edit mode (press Enter): edit inside a cell
  • A → insert cell above
  • B → insert cell below
  • D, D → delete selected cell
  • X → cut cell
  • C → copy cell
  • V → paste cell
  • Z → undo delete
  • M → convert cell to Markdown
  • Y → convert cell to Code
  • Shift + Up/Down → select multiple cells
  • Ctrl + Enter → run cell (stay)
  • Shift + Enter → run cell (move down)
  • Alt + Enter → run cell (insert new cell below)
  • Tab → autocomplete (when available)
  • Ctrl + / → toggle comment in selection

Magic commands are special Jupyter commands that start with:

  • % for line magics
  • %% for cell magics

They are not standard Python syntax; they’re handled by IPython/Jupyter.

Use these to measure execution time.

time
%time sum(range(10_000_00))

%timeit runs the expression multiple times and gives an average:

timeit
%timeit sum(range(1_000_00))

Show variables currently defined.

who
%whos
  • %pwd shows current working directory
  • %cd changes it
pwd
%pwd
cd
%cd data
ls
%ls

Classic way to render plots inside the notebook.

matplotlib-inline
%matplotlib inline

This can enable interactive plots in the classic notebook.

matplotlib-notebook
%matplotlib notebook

You can write a cell’s content into a Python script.

writefile
%%writefile my_script.py
print("Hello from a file")

If your Jupyter supports it, you can run bash scripts:

bash
%%bash
ls -la

If %%bash isn’t available, you can use ! shell escapes instead.

Prefix a command with ! to run it in the OS shell.

shell-escape
!python --version
pip-list
!pip list | head
help
import pandas as pd
pd.DataFrame?
help2
help(pd.DataFrame)

The % prefix tells IPython how much of the input to treat as a magic command versus plain Python.

diagram Magic command dispatch mermaid
How IPython decides between a line magic, a cell magic, and a shell escape

Magics only work in Jupyter/IPython, not in normal Python scripts.

Mistake 2: Forgetting to rerun earlier cells

Section titled “Mistake 2: Forgetting to rerun earlier cells”

Notebook state is incremental. If you restart the kernel, rerun setup cells.

Continue to: Google Colab Walkthrough to learn cloud notebooks and GPU/TPU options.

Exercise 2 – Time a Function With timeit

Section titled “Exercise 2 – Time a Function With timeit”

Exercise 3 – Strip the ! From a Shell Escape

Section titled “Exercise 3 – Strip the ! From a Shell Escape”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading