Jumpstart the Jupyter Notebook productivity with essential shortcuts, magic commands, and workflow tips that will transform your data science and development experience.
Jupyter Notebook has become the de facto standard for interactive computing, data analysis, and machine learning workflows. Whether you’re working with Python for data science, experimenting with AI models, or prototyping code, mastering Jupyter’s keyboard shortcuts and magic commands can dramatically boost your productivity.
This cheatsheet covers the most essential commands and shortcuts that every Jupyter user should know, from basic cell navigation to advanced magic commands that streamline your workflow. If you’re new to Python development, you…
Jumpstart the Jupyter Notebook productivity with essential shortcuts, magic commands, and workflow tips that will transform your data science and development experience.
Jupyter Notebook has become the de facto standard for interactive computing, data analysis, and machine learning workflows. Whether you’re working with Python for data science, experimenting with AI models, or prototyping code, mastering Jupyter’s keyboard shortcuts and magic commands can dramatically boost your productivity.
This cheatsheet covers the most essential commands and shortcuts that every Jupyter user should know, from basic cell navigation to advanced magic commands that streamline your workflow. If you’re new to Python development, you might also find our Python Cheatsheet helpful for understanding Python language constructs that work seamlessly within Jupyter cells.
Understanding Command Mode vs Edit Mode
Jupyter Notebook operates in two distinct modes that determine which keyboard shortcuts are available:
- Command Mode (activated by pressing
Esc): Controls cell-level operations like creating, deleting, and converting cells - Edit Mode (activated by pressing
Enter): Allows you to edit the content within a cell
The most important shortcut to remember is pressing H in command mode, which displays the complete keyboard shortcuts help dialog. This is your quick reference when you forget a specific shortcut.
Essential Command Mode Shortcuts
Cell Navigation and Execution
The core workflow in Jupyter revolves around running cells efficiently:
Shift + Enter: Run the current cell and automatically select the cell below. This is the most frequently used shortcut for executing code sequentially.Ctrl + Enter: Run the current cell without moving to the next one. Perfect when you want to re-execute a cell multiple times.Alt + Enter: Run the current cell and insert a new cell below. Ideal for iterative development where you need to add new cells frequently.
Cell Management
Managing your notebook structure efficiently is crucial for maintaining clean, organized notebooks:
A: Insert a new cell above the current cellB: Insert a new cell below the current cellD, D(pressDtwice): Delete the current cell. Be careful with this one!Z: Undo the last cell deletion. A lifesaver when you accidentally delete important cells.Shift + M: Merge selected cells. Select multiple cells usingShift + JorShift + Kto navigate, then merge them.
Cell Type Conversion
Quickly switching between cell types is essential for creating well-documented notebooks:
Y: Convert the cell to CodeM: Convert the cell to MarkdownR: Convert the cell to Raw (not executed, useful for notes)1to6: Convert the cell to Heading levels 1 to 6 for structured documentation
Kernel Operations
When working with long-running computations or debugging, kernel control is essential:
I, I(pressItwice): Interrupt the kernel. Critical when you need to stop a runaway computation.0, 0(press0twice): Restart the kernel. Use this when you need to clear all variables and start fresh.
Display and Navigation
L: Toggle line numbers in the current cell. Helpful for debugging and referencing specific lines.O: Toggle cell output. Hide output to keep your notebook clean when presenting.H: Show the keyboard shortcuts help dialog
Essential Edit Mode Shortcuts
When editing code within a cell, these shortcuts mirror common text editor functionality:
Text Editing
Tab: Code completion or indent. Jupyter’s autocomplete is powerful and context-aware.Shift + Tab: Show tooltip/documentation for the object under the cursor. This is incredibly useful for exploring APIs without leaving the notebook.Ctrl + ]: Indent selected linesCtrl + [: Dedent selected linesCtrl + /: Toggle comment on selected lines. Essential for quickly commenting out code during debugging.
Navigation
Ctrl + A: Select all text in the cellCtrl + Z: UndoCtrl + Shift + ZorCtrl + Y: RedoCtrl + Home: Go to cell startCtrl + End: Go to cell endCtrl + Left: Move cursor one word leftCtrl + Right: Move cursor one word right
Mode Switching
Esc: Switch to Command Mode (from Edit Mode)
Magic Commands: Supercharge Your Workflow
Magic commands are special directives that extend Jupyter’s functionality. They come in two flavors: line magics (prefixed with %) that operate on a single line, and cell magics (prefixed with %%) that operate on the entire cell.
Performance Profiling
Understanding code performance is crucial for optimization. Jupyter provides built-in timing commands:
# Time a single execution
%time sum(range(1000000))
# Time multiple executions with averaging (more accurate)
%timeit sum(range(1000000))
# Time an entire cell
%%time
total = 0
for i in range(1000000):
total += i
The %timeit command is particularly valuable as it runs the code multiple times and provides statistical information about execution time, making it ideal for benchmarking different approaches to the same problem.
Running External Scripts
The %run magic allows you to execute external Python scripts within your notebook, making it easy to modularize code:
%run my_script.py
This is especially useful when working with larger projects where you want to keep reusable functions in separate files. If you’re managing Python environments, you might be using tools like uv or venv for environment management, and %run works seamlessly with these setups.
Package Management
You can install and manage Python packages directly from your notebook:
!pip install numpy pandas matplotlib
!conda install scipy
This eliminates the need to switch between terminal and notebook, streamlining your development workflow. The ! prefix executes shell commands, which we’ll explore more below.
Visualization
For data visualization with matplotlib, use:
%matplotlib inline
This ensures plots are rendered directly within the notebook cells, providing a seamless visualization experience. You can also use %matplotlib widget for interactive plots in JupyterLab.
File Operations
The %%writefile cell magic writes cell content to a file:
%%writefile example.py
def hello_world():
print("Hello, World!")
return True
This is useful for generating scripts or saving code snippets directly from your notebook. Conversely, you can read files using standard Python file operations or shell commands.
Shell Commands
Execute shell commands directly from notebook cells:
# Single line shell command
!ls -l
!pwd
!git status
# Multi-line shell commands
%%bash
echo "Hello from Bash"
ls -l
find . -name "*.py" | head -10
This integration is powerful for file management, version control operations, and system administration tasks without leaving the notebook environment.
Exploring Available Magics
To discover all available magic commands:
# List all magic commands
%lsmagic
# Get help on a specific magic
%timeit?
%matplotlib?
The ? operator after any magic command displays its documentation, parameters, and usage examples. This is an excellent way to explore Jupyter’s capabilities interactively.
Advanced Tips and Best Practices
Organizing Your Notebook
Use Markdown cells liberally: Well-documented notebooks are easier to understand and maintain. Use heading levels (1-6) to create clear structure. 1.
Keep cells focused: Each cell should have a single, clear purpose. This makes debugging easier and improves readability. 1.
Use cell execution numbers: The execution numbers (In [1], In [2], etc.) help you track the order of execution, which is crucial when cells can be run out of order.
Working with Large Notebooks
When notebooks grow large, these techniques help maintain organization:
- Use the table of contents extension (if available) for navigation
- Split related sections into separate notebooks
- Use
%%writefileto extract reusable code into Python modules - Regularly restart the kernel and run all cells to ensure reproducibility
Integration with Other Tools
Jupyter Notebooks work excellently with modern Python tooling. If you’re working with LLMs and need structured output, you can integrate notebooks with tools like Ollama for structured LLM outputs. For web scraping and data processing, you might find yourself using libraries that convert HTML to Markdown, similar to what we covered in our HTML to Markdown conversion guide.
Debugging Workflow
- Use
%debugmagic after an exception to enter the debugger - Use
%pdb onto automatically enter debugger on exceptions - Print statements and
%timeithelp identify performance bottlenecks - Restart kernel (
0, 0) when variables get into unexpected states
Customization
You can customize keyboard shortcuts by going to Help → Keyboard Shortcuts in the Jupyter Notebook menu. This allows you to tailor the environment to your specific workflow preferences.
Conclusion
Mastering Jupyter Notebook shortcuts and magic commands transforms it from a simple code editor into a powerful interactive computing environment. Start by memorizing the most frequently used shortcuts (Shift+Enter, Esc, A, B, D+D, M, Y), then gradually incorporate magic commands into your workflow. The time invested in learning these commands pays dividends in productivity and workflow efficiency.
Remember: press H in command mode anytime to see the complete shortcuts reference, and use %lsmagic to explore all available magic commands. Happy coding!
Useful links
- Python Cheatsheet
- uv - New Python Package, Project, and Environment Manager
- venv Cheatsheet
- LLMs with Structured Output: Ollama, Qwen3 & Python or Go
- Converting HTML to Markdown with Python: A Comprehensive Guide