Useful Jupyter Notebook Shortcuts
One thing I have been obsessed with since starting to learn Python in Jupyter Notebooks has been watching how people type and move their cursors around with ease. I am not a great typer in the first place, but once you start to learn how to code, it gets even slower. You are using different symbols, that you would never use all the time like “_” for example. After you have been practicing for a while, like anything, it becomes second nature. But there are some very useful shortcuts and skills you can learn to make navigating a Jupyter Notebook much easier and faster.
There are a couple of different modes that we have to discuss briefly before getting into the short cuts themselves. The two different modes are called “Command” and “Edit”. The first mode “Command” is for moving around the notebook without editing the text inside of the cells. You can add cells, delete cells and merge cells etc. The second mode is “Edit”, which allows you to edit the code inside the cells and write functions etc. You start in “Command” mode as you navigate around the notebook. To enter “Edit” mode, you hit the return button on your highlighted cell or click inside the cell with your mouse. To know you have entered the cell or “Edit” mode, you will see the cursor blinking inside the desired cell.
Now that we have discussed the two different modes, let’s talk about the hot keys or short cuts. My newest short cut I learned while researching this topic is the H key. While in “Command” mode, if you hit just the H key, it will bring up a window below with all the shortcuts.
Now please understand these commands above are for Mac. The commands are going to be slightly different if you are using Windows. I want to discuss some useful tips that aren’t on the list above. These aren’t necessarily shortcuts or hotkeys, but useful tips to make life easier and more efficient. This first trick is one of the coolest I have seen thus far. Let’s say you have two functions or lines you want to change something on simultaneously. A good example would be a couple of nested for loops. Let’s say you want to type the sum() function infront of both lines of code at the sametime. You can click in the first position then hold down the command key and click in the second position. Then you can write “sum()” in both positions at the sametime!
The next two tricks go together nicely. You will learn once you start coding that it is much quicker to use the keyboard to get around instead of using the mouse like we are primarily used to these days. If you hold command and press the right arrow or left arrow it will move your cursor from the beginning of a line or to the end. This helps you move around very quickly without having to reach over and use your mouse. If you hold command, shift and use the arrow keys, it will move you to the beginning or end of a sentence while highlighting it. This is helpful if you want to delete part of a sentence or code block. The last tip is for writing functions or methods and understanding what each different parameter does. If you write out a function or method, for example “df.dropna(inplace=True)”, you can put your cursor inside the first space of the parentheses. If you hit shift and tab at the same time it opens a window that explains what all the parameters of the function or method can be tailored to do with additional input. See below the example of the window that appears for the “.drop()” method.
Signature: heart.dropna(axis=0, how=’any’, thresh=None, subset=None, inplace=False)
Remove missing values.
The final tip is a general shortcut for any window you have open, be that an internet browser of Jupyter Notebook. If you hit command and F the mac will open a small search bar in the top right corner of the screen where you can enter your search parameters. Once you enter a word or number you are looking for, you can hit the up and down arrows to scroll through where each instance of the word is located on the program or page you are looking at. You can very quickly find words etc. Imagine you are looking for a certain word in a ten page single spaced word document. This can save you lots of time! See below for what the search bar looks like!
I hope you all find these tricks of the trade helpful. If I had something like this to reference when I first started out, it would have saved me a lot of time and frustration! Happy coding!