Skip to content

Jupyter Shortcuts & Magic Commands

Why shortcuts and magics matter

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)

Jupyter has two main modes:

  • Command mode (press EscEsc): navigate and manage cells
  • Edit mode (press EnterEnter): edit inside a cell

Most useful shortcuts (Command mode)

  • AA → insert cell above
  • BB → insert cell below
  • D, DD, D → delete selected cell
  • XX → cut cell
  • CC → copy cell
  • VV → paste cell
  • ZZ → undo delete
  • MM → convert cell to Markdown
  • YY → convert cell to Code
  • Shift + Up/DownShift + Up/Down → select multiple cells

Most useful shortcuts (Edit mode)

  • Ctrl + EnterCtrl + Enter → run cell (stay)
  • Shift + EnterShift + Enter → run cell (move down)
  • Alt + EnterAlt + Enter → run cell (insert new cell below)
  • TabTab → autocomplete (when available)
  • Ctrl + /Ctrl + / → toggle comment in selection

What are magic commands?

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.

Common line magics

%time%time and %timeit%timeit

Use these to measure execution time.

time
%time sum(range(10_000_00))
time
%time sum(range(10_000_00))

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

timeit
%timeit sum(range(1_000_00))
timeit
%timeit sum(range(1_000_00))

%who%who and %whos%whos

Show variables currently defined.

who
%whos
who
%whos

%pwd%pwd and %cd%cd

  • %pwd%pwd shows current working directory
  • %cd%cd changes it
pwd
%pwd
pwd
%pwd
cd
%cd data
cd
%cd data

%ls%ls (list files)

ls
%ls
ls
%ls

Plotting magics

%matplotlib inline%matplotlib inline

Classic way to render plots inside the notebook.

matplotlib-inline
%matplotlib inline
matplotlib-inline
%matplotlib inline

%matplotlib notebook%matplotlib notebook (interactive)

This can enable interactive plots in the classic notebook.

matplotlib-notebook
%matplotlib notebook
matplotlib-notebook
%matplotlib notebook

Useful cell magics

%%writefile%%writefile (save code to a file)

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

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

%%bash%%bash (run bash commands)

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

bash
%%bash
ls -la
bash
%%bash
ls -la

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

Shell escapes with !!

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

shell-escape
!python --version
shell-escape
!python --version
pip-list
!pip list | head
pip-list
!pip list | head

Getting help quickly

Add ?? to functions

help
import pandas as pd
pd.DataFrame?
help
import pandas as pd
pd.DataFrame?

Use help()help()

help2
help(pd.DataFrame)
help2
help(pd.DataFrame)

Common mistakes

Mistake 1: Using magics in .py.py files

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

Mistake 2: Forgetting to rerun earlier cells

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

Next

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

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did