5 Jupyter Notebook Shortcuts That Will Make You More Efficient
Jupyter Notebook is a popular development environment in the Python data science community. It consists of a sequence of independent cells, each of which holds a chunk of Python code. When we execute a cell, Jupyter reveals the code’s output directly below the cell. The design encourages an active trial-and-error approach to data analysis. We can try one approach, observe the results, and modify our implementation accordingly.
Jupyter Notebook has dozens of keyboard shortcuts available to accelerate developer productivity. They assist with everything from everyday operations like copying and pasting to complex shortcuts for restarting the IPython kernel that powers the Notebook. In this article, we’ll introduce the most important Jupyter Notebook shortcuts you need to know to get up and running with this powerful technology.
1. Edit Mode
Jupyter Notebook operates in 2 modes: Command and Edit Mode.
Edit Mode is activated whenever we click into a cell. We use this mode to enter and edit text content into a cell. Jupyter highlights the currently active cell in green.
Figure 1: Jupyter Notebook operating in Edit Mode
Press Shift + Enter to execute a cell and advance the cursor to the next available cell. If a cell does not exist, Jupyter will create one below.
The complementary Control + Enter shortcut executes a cell but does not advance to the next one.
Figure 2: An executed Python code cell in Jupyter Notebook
2. Command Mode
In Command Mode, our keyboard shortcuts translate to notebook-level actions. These actions are more “global” in nature and affect the Notebook as a whole.
To activate Command Mode, press the Escape key. Jupyter will outline the current cell in blue instead of green.
Figure 3: Jupyter Notebook operating in Command Mode
Command Mode provides several edit keyboard shortcuts for common notebook-level actions. Here are some that I use daily:
- Press the a key to create a cell above the currently selected one.
- Press the b key to create a cell below the currently selected one.
- Press the c key to copy the contents of a cell.
- Press the x key to cut the contents of a cell.
- Press the v key to paste the previously copied or cut contents.
- Press the d key twice in a row to delete the selected cell.
Jupyter Notebook ships with an autosave feature, but it never hurts to save your work frequently. The Cmd + S (macOS) and Ctrl + S (Windows) shortcuts save the Notebook. You can also access the File menu at the top of the view and select Save and Checkpoint.
3. Restarting the IPython Kernel
Jupyter Notebook employs IPython to execute its Python code. IPython is an “enhanced” version of the original Python shell with extra features for debugging, history, magic methods, and more. See this article for some helpful tips for getting started with IPython.
At some point in your programming career, a piece of faulty code will trigger a Notebook slowdown. Thankfully, Jupyter makes it easy to recover from it.
The Kernel menu on the navigation menu offers options to Interrupt the currently executing code cell or Restart the IPython kernel completely. You can also restart the Notebook by pressing 0 twice in a row in Command Mode.
Figure 4: The Kernel Menu in Jupyter Notebook
Note that the Restart options wipe all saved Python variables/constants from memory. You’ll need to re-execute the relevant cells again to bring them back into the program.
The Kernel menu offers additional options to clear cell output and run all cells in the Notebook. It’s a convenient way to “reset” when things go wrong.
4. The Command Palette
The Command Palette is an input box where you can issue commands to the Notebook. You no longer need to worry about remembering what navigation menu holds the command you’re looking for. Simply type a keyword, and Jupyter will filter the results to those that match the search term.
Press Command + Shift + P (macOS) or Ctrl + Shift P (Windows) to see the Command Palette. Click on a command to execute it.
Figure 5: The Command Palette in Jupyter Notebook
Markdown is a markup language for formatting text. We can use it to add paragraphs, headers, bullet lists, and more within our Jupyter Notebook.
In Command Mode, press the m key to designate a cell as a Markdown cell. If you need to convert a cell to a Python Code cell, press the y key instead.
Here are some helpful Markdown operations:
- Use hashtags to create bold headers. One hashtag (#) produces the largest header, and six hashtags (######) creates the smallest header.
- Use dashes to create bulleted lists.
- Use line breaks to separate paragraphs.
Figure 6: A Markdown cell in Edit Mode
Once you’re done editing a Markdown cell, press Shift + Enter in Command Mode to execute it. Jupyter will display the formatted text output in the cell’s location.
Figure 7: A Markdown cell after execution
6. Even more shortcuts
Curious to learn more? In Command Mode, press the h key to reveal a modal of keyboard shortcuts. Some cool key bindings are available for operations like deleting the next word in line or moving the cursor to the start of the line. I frequently consult this list to learn new tips and tricks to accelerate my Notebook productivity.
Figure 8: The keyboard shortcuts modal in Jupyter Notebook
Jupyter Notebook remains a powerful and effective tool for experimenting with Python code. It also allows you to tell a story with your code, the “how” and the “why.” You can use Markdown cells to describe your work and Python cells to show how you arrived at your result. Because of these benefits, many Udemy courses rely on Jupyter to teach Python.
Additional Python development tools are available, like Spyder. I like to view each development environment as a separate tool in my technical toolbox. There is no harm in learning the ins and outs of as many tools as you want. Regardless of which environment you work with, I hope you continue to make your work more efficient. See you next time!
Top courses in Data Analysis
Data Analysis students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.