Built-in Shortcuts and Workflow Tips for Jupyter Lab You Need To Know

Eric Heidbreder
Towards Data Science
6 min readSep 27, 2020

--

Before I started data science, I was working with audio constantly: editing podcasts, mixing 50-track sessions of middle school band players, arranging the “Fairytale” theme from Shrek, and the list goes on. As an audio editor, you 100% absolutely need shortcuts or you will decompose in your chair while working on a large project. There’s just not enough time to click everything. So, when I learn a new technology like Jupyter Lab, one of the first things I do is find every shortcut I can so I don’t wind up clicking my way to an early grave.

Pictured: Me trying to clean up a Jupyter Notebook without using shortcuts. (Photo by Anne Nygård on Unsplash)

Let’s dig into the shortcuts, shall we? Note that I’m going to be mentioning the Windows keys Control and Alt, and those equate to Command and Option, respectively, on Mac.

1. Mouse Modifiers: Ctrl (Command) and Alt (Option)

Mouse modifiers are keys that change what the mouse does. Most software I’ve used does something when you control + click or alt + click, whether it’s useful or not is another story.

In Jupyter Lab, you can do some amazing things with Control and Alt:

  • Holding down control and clicking will let you edit in multiple places within a cell. Double clicking will also highlight a word. Combine these two, and you can make quick work out of removing methods you don’t need.
Holding Ctrl, double clicking on `values` and deleting.
  • Holding down alt and dragging will let you edit across vertical lines. I use this to add prefixes/suffixes to variables in a cell. (Note: you can press Ctrl + F to replace, as well, but I’ve developed trust issues with the ‘Replace All’ function after it betrayed me.)
Holding alt, dragging down vertically, adding a prefix to a variable name
Holding alt, dragging a box and replacing multiple variable prefixes at once
  • And to answer your next question, yes! You can hold Alt + Ctrl to highlight and edit in multiple places within a cell. But I only use this when I’m feeling chaotic.

2. Cell Operations

Cells can be great for storytelling with data but annoying when you need to better organize the code within them. Here are some operations you can perform on cells themselves to clean up your notebook in a snap!

Each of these operations must be performed when the cell is selected, so you shouldn’t be editing the cell. You can select the cell by clicking in the area to the left of the cell where you typically see the input/output numbers.

  • Copy: Just press c
  • Paste: Just press v
  • Cut: Just press x
  • Delete: Press dd
  • Move: Click and drag the cell

Now watch me do this very quickly — wow! I’m so fast! Wow! Impressive! Wow! I didn’t have to practice this at all! (…)

Merge and Split Cells

This will help us solve the following problems:

  1. You’ve written too much in one cell and want to split it into multiple cells
  2. You need to share code with someone but it’s across 8 different cells and copying it will be tedious for you and the recipient
  • Splitting Cells: Put your blinking cursor at the beginning of the line after the line you want to split, then press Ctrl + Shift + - to split them
Splitting cells with Ctrl + Shift + -
  • Merge Cells: Select all the cells you want to merge by holding Shift and selecting the range of cells you want to merge (click cell 1, then cell 8 to select cells 1–8), then press Shift + M to merge them all into one cell
Merging Cells together with Shift + M
  • Undo last cell operation: This one’s for every data scientist who has thought they ruined their entire night by deleting a complex function cell on accident. Press z to undo the last cell operation and lower your heart rate. Press Shift + z to redo the last cell operation, too!

3. Navigating and Editing text with Ctrl

This is something that spans many programs, text editors, and browsers. I recommend everyone use this as it will save you time navigating lines of text.

  • Navigate by word: hold ctrl and pressing the left or right arrows
  • Delete a whole word: hold ctrl and press delete or backspace depending on the direction you want to delete. This will also work if you want to delete from the middle of a word to the end/beginning of the word. Just place your cursor in the middle and use the appropriate delete or backspace key
  • Highlight a word: hold ctrl + shift and press the left or right arrows to highlight a word. This is not super useful in Jupyter Lab, but it is helpful in text editors where you can use shortcuts to bold, highlight, or underline text.
Aside from the initial click, no mouse was used in the making of the above gif.

The concept of navigating by word is a perfect tool if you have a programmable gaming keyboard/mouse — I use the ctrl + arrows idea to assign quick macros that will automatically bold the first word in each line of a markdown data dictionary with the press of a button by combining some of the above techniques.

Yes, you need to get a gaming keyboard right now. I’m not sponsored by Logitech, but I should be.

And finally…Parallel notebook tabs

One of the most frustrating things to do when I first started using Jupyter Lab was creating README files in markdown. I had to use 3rd-party platforms to see my markdown edits. Fear not! For I have a workflow improvement!

  • Double click to open your README.md file with the editor.
  • Right click and go to Open with > Markdown Preview
  • Click on the preview tab at the top of your screen and drag it to the right side of your screen to position the two side-by-side and see realtime updates to your markdown.

Please excuse the fullscreen gif…

Other uses for this:

  • Pull up the contextual help menu (Under “Help”) so you can view a function’s docstring without needing to press Shift + Tab
  • Pull up a funcs.py script that contains your custom functions so you can remember how they work without needing to flip back and forth.

That’s all, folks!

If you have any other frustrations with Jupyter Lab, leave a comment and I’ll see if I can think of a way to solve it with built-in functionality! And thanks to my colleagues for the encouragement to write this blog post after I saved someone’s night with the “Undo last cell operation” function.

--

--

I’m a Chicago-based analytics consultant at Analytics8 who is certified in Looker and Qlik. I’m also a bassoonist and songwriter. Someday, I’ll own a Pug.