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 aboveBB→ insert cell belowD, DD, D→ delete selected cellXX→ cut cellCC→ copy cellVV→ paste cellZZ→ undo deleteMM→ convert cell to MarkdownYY→ convert cell to CodeShift + 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 sum(range(10_000_00))%time sum(range(10_000_00))%timeit%timeit runs the expression multiple times and gives an average:
%timeit sum(range(1_000_00))%timeit sum(range(1_000_00))%who%who and %whos%whos
Show variables currently defined.
%whos%whos%pwd%pwd and %cd%cd
%pwd%pwdshows current working directory%cd%cdchanges it
%pwd%pwd%cd data%cd data%ls%ls (list files)
%ls%lsPlotting magics
%matplotlib inline%matplotlib inline
Classic way to render plots inside the notebook.
%matplotlib inline%matplotlib inline%matplotlib notebook%matplotlib notebook (interactive)
This can enable interactive plots in the classic notebook.
%matplotlib notebook%matplotlib notebookUseful cell magics
%%writefile%%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")%%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
ls -la%%bash
ls -laIf %%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.
!python --version!python --version!pip list | head!pip list | headGetting help quickly
Add ?? to functions
import pandas as pd
pd.DataFrame?import pandas as pd
pd.DataFrame?Use help()help()
help(pd.DataFrame)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 coffeeWas this page helpful?
Let us know how we did
