Jupyter Shortcuts & Magic Commands
Why shortcuts and magics matter
Section titled “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)
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
Most useful shortcuts (Command mode)
Section titled “Most useful shortcuts (Command mode)”A→ insert cell aboveB→ insert cell belowD, D→ delete selected cellX→ cut cellC→ copy cellV→ paste cellZ→ undo deleteM→ convert cell to MarkdownY→ convert cell to CodeShift + Up/Down→ select multiple cells
Most useful shortcuts (Edit mode)
Section titled “Most useful shortcuts (Edit mode)”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
What are magic commands?
Section titled “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
Section titled “Common line magics”%time and %timeit
Section titled “%time and %timeit”Use these to measure execution time.
%time sum(range(10_000_00))%timeit runs the expression multiple times and gives an average:
%timeit sum(range(1_000_00))%who and %whos
Section titled “%who and %whos”Show variables currently defined.
%whos%pwd and %cd
Section titled “%pwd and %cd”%pwdshows current working directory%cdchanges it
%pwd%cd data%ls (list files)
Section titled “%ls (list files)”%lsPlotting magics
Section titled “Plotting magics”%matplotlib inline
Section titled “%matplotlib inline”Classic way to render plots inside the notebook.
%matplotlib inline%matplotlib notebook (interactive)
Section titled “%matplotlib notebook (interactive)”This can enable interactive plots in the classic notebook.
%matplotlib notebookUseful cell magics
Section titled “Useful cell magics”%%writefile (save code to a file)
Section titled “%%writefile (save code to a file)”You can write a cell’s content into a Python script.
%%writefile my_script.py
print("Hello from a file")%%bash (run bash commands)
Section titled “%%bash (run bash commands)”If your Jupyter supports it, you can run bash scripts:
%%bash
ls -laIf %%bash isn’t available, you can use ! shell escapes instead.
Shell escapes with !
Section titled “Shell escapes with !”Prefix a command with ! to run it in the OS shell.
!python --version!pip list | headGetting help quickly
Section titled “Getting help quickly”Add ? to functions
Section titled “Add ? to functions”import pandas as pd
pd.DataFrame?Use help()
Section titled “Use help()”help(pd.DataFrame)Visualize it
Section titled “Visualize it”The % prefix tells IPython how much of the input to treat as a magic command versus plain Python.
flowchart TD
A["Line typed in a cell"] --> B{"Starts with?"}
B -->|"%%"| C["Cell magic: applies to the WHOLE cell"]
B -->|"%"| D["Line magic: applies to ONE line"]
B -->|"!"| E["Shell escape: runs in the OS shell"]
B -->|"nothing special"| F["Normal Python code"]
Common mistakes
Section titled “Common mistakes”Mistake 1: Using magics in .py files
Section titled “Mistake 1: Using magics in .py files”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.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Classify a Magic Command
Section titled “Exercise 1 – Classify a Magic Command”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.coffeeCtapch.feedbackHeading
pch.feedbackSubheading