I just love this about working in Jupyter notebooks! It's so easy to gradually build what I want, and check my work every step of the way. I make a lot of mistakes, so this is really helpful to me...

---- Jeremy Howard

Command Mode Shortcuts

There are a couple of useful keyboard shortcuts in Command Mode that you can leverage to make Jupyter Notebook faster to use. Remember that you can switch back and forth between Command Mode and Edit Mode with Esc and Enter.

  • h:: Show all shortcuts
  • xcv:: cut copy paste
  • m:: Convert cell to Markdown
  • y:: Convert cell to Code
  • d+d:: Delete cell
  • z:: Undo cell deletion
  • o:: Toggle between hide or show output
  • shift+/:: Select multiple cells. Once you have selected them you can operate on them like a batch (run, copy, paste etc).
  • shift+m:: Merge selected cells
  • ctrl+shift+-:: Split cell

Cell Tricks

There are also some tricks that you can code into a cell:

  • ?function-name:: Shows the definition and docstring for that function
  • ??function-name:: Shows the source code for that function
  • doc(function-name):: Shows the definition, docstring and links to the documentation of the function (only works with fastai library imported)
  • Shift+Tab (press once):: See which parameters to pass to a function
  • Shift+Tab (press three times):: Get additional information on the method

Line Magics

Line magics are functions that you can run on cells. They should be at the beginning of a line and take as an argument the rest of the line from where they are called. You call them by placing a '%' sign before the command. The most useful ones are:

  • %matplotlib inline:: Ensures that all matplotlib plots will be plotted in the output cell within the notebook and will be kept in the notebook when saved.

This command is always called together at the beginning of every notebook of the fast.ai course.

%matplotlib inline
  • %timeit:: Runs a line ten thousand times and displays the average time it took to run.
%timeit [i+1 for i in range(1000)]
37.6 µs ± 3.71 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%debug: Inspects a function which is showing an error using the Python debugger. If you type this in a cell just after an error, you will be directed to a console where you can inspect the values of all the variables.

Here are some other features that are very useful in Jupyter notebooks:

  • At any point, if you don't remember the exact spelling of a function or argument name, you can press Tab to get autocompletion suggestions.
  • When inside the parentheses of a function, pressing Shift and Tab simultaneously will display a window with the signature of the function and a short description. Pressing these keys twice will expand the documentation, and pressing them three times will open a full window with the same information at the bottom of your screen.
  • In a cell, typing ?func_name and executing will open a window with the signature of the function and a short description.
  • In a cell, typing ??func_name and executing will open a window with the signature of the function, a short description, and the source code.
  • If you are using the fastai library, we added a doc function for you: executing doc(func_name) in a cell will open a window with the signature of the function, a short description and links to the source code on GitHub and the full documentation of the function in the library docs.
  • Unrelated to the documentation but still very useful: to get help at any point if you get an error, type %debug in the next cell and execute to open the Python debugger, which will let you inspect the content of every variable.