User Interface¶
This notebook describes the user interface of the IPython Notebook. This includes both mouse and keyboard based navigation and interaction.
Modal editor¶
Starting with IPython 2.0, the IPython Notebook has a modal user interface. This means that the keyboard does different things depending on which mode the Notebook is in. There are two modes: edit mode and command mode.
Edit mode¶
Edit mode is indicated by a green cell border and a prompt showing in the editor area:
When a cell is in edit mode, you can type into the cell, like a normal text editor.
Command mode¶
Command mode is indicated by a grey cell border:
When you are in command mode, you are able to edit the notebook as a whole, but not type into individual cells. Most importantly, in command mode, the keyboard is mapped to a set of shortcuts that let you perform notebook and cell actions efficiently. For example, if you are in command mode and you press c
, you will copy the current cell - no modifier is needed.
Mouse navigation¶
All navigation and actions in the Notebook are available using the mouse through the menubar and toolbar, which are both above the main Notebook area:
The first idea of mouse based navigation is that cells can be selected by clicking on them. The currently selected cell gets a grey or green border depending on whether the notebook is in edit or command mode. If you click inside a cell's editor area, you will enter edit mode. If you click on the prompt or output area of a cell you will enter command mode.
If you are running this notebook in a live session (not on http://nbviewer.ipython.org) try selecting different cells and going between edit and command mode. Try typing into a cell.
The second idea of mouse based navigation is that cell actions usually apply to the currently selected cell. Thus if you want to run the code in a cell, you would select it and click the "Play" button in the toolbar or the "Cell:Run" menu item. Similarly, to copy a cell you would select it and click the "Copy" button in the toolbar or the "Edit:Copy" menu item. With this simple pattern, you should be able to do most everything you need with the mouse.
Markdown and heading cells have one other state that can be modified with the mouse. These cells can either be rendered or unrendered. When they are rendered, you will see a nice formatted representation of the cell's contents. When they are unrendered, you will see the raw text source of the cell. To render the selected cell with the mouse, click the "Play" button in the toolbar or the "Cell:Run" menu item. To unrender the selected cell, double click on the cell.
Keyboard Navigation¶
The modal user interface of the IPython Notebook has been optimized for efficient keyboard usage. This is made possible by having two different sets of keyboard shortcuts: one set that is active in edit mode and another in command mode.
The most important keyboard shortcuts are enter
, which enters edit mode, and esc
, which enters command mode.
In edit mode, most of the keyboard is dedicated to typing into the cell's editor. Thus, in edit mode there are relatively few shortcuts:
The display_edit_shortcuts()
function used here is defined in the Utilities section at the bottom of this notebook.
display_edit_shortcuts()
There are two other keyboard shortcuts in edit mode that are not listed here:
tab
: trigger "tab" completionshift+tab
: open the tooltip
In command mode, the entire keyboard is available for shortcuts:
display_command_shortcuts()
Here the rough order in which we recommend learning the command mode shortcuts:
- Basic navigation:
enter
,shift-enter
,up/k
,down/j
- Saving the notebook:
s
- Cell types:
y
,m
,1-6
,t
- Cell creation and movement:
a
,b
,ctrl+k
,ctrl+j
- Cell editing:
x
,c
,v
,d
,z
,shift+=
- Kernel operations:
i
,.
Keyboard shortcut customization¶
Starting with IPython 2.0 keyboard shortcuts in command and edit mode are fully customizable. These customizations are made using the IPython JavaScript API. Here is an example that makes the r
key available for running a cell:
%%javascript
IPython.keyboard_manager.command_shortcuts.add_shortcut('r', {
help : 'run cell',
help_index : 'zz',
handler : function (event) {
IPython.notebook.execute_cell();
return false;
}}
);
There are a couple of points to mention about this API:
- The
help_index
field is used to sort the shortcuts in the Keyboard Shortcuts help dialog. It defaults tozz
. - When a handler returns
false
it indicates that the event should stop propagating and the default action should not be performed. For further details about theevent
object or event handling, see the jQuery docs. - If you don't need a
help
orhelp_index
field, you can simply pass a function as the second argument toadd_shortcut
.
%%javascript
IPython.keyboard_manager.command_shortcuts.add_shortcut('r', function (event) {
IPython.notebook.execute_cell();
return false;
});
Likewise, to remove a shortcut, use remove_shortcut
:
%%javascript
IPython.keyboard_manager.command_shortcuts.remove_shortcut('r');
If you want your keyboard shortcuts to be active for all of your notebooks, put the above API calls into your <profile>/static/custom/custom.js
file.
Utilities¶
We use the following functions to generate the keyboard shortcut listings above.
from IPython.display import Javascript, display, HTML
t = """var help = IPython.quick_help.build_{0}_help();
help.children().first().remove();
this.append_output({{output_type: 'display_data', html: help.html()}});"""
def display_command_shortcuts():
display(Javascript(t.format('command')))
def display_edit_shortcuts():
display(Javascript(t.format('edit')))
display(HTML("""
<style>
.shortcut_key {display: inline-block; width: 15ex; text-align: right; font-family: monospace;}
.shortcut_descr {display: inline-block;}
div.quickhelp {float: none; width: 100%;}
</style>
"""))