2.5 KiB
View the original notebook on nbviewer
Keyboard Shortcut Customization
Starting with IPython 2.0 keyboard shortcuts in command and edit mode
are fully customizable. These customizations are made using the Jupyter
JavaScript API. Here is an example that makes the r
key
available for running a cell:
%%javascript
'r', {
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(help : 'run cell',
'zz',
help_index :
handler : function (event) {;
IPython.notebook.execute_cell()return false;
}}; )
"By default the keypress r
, while in command mode,
changes the type of the selected cell to raw
. This shortcut
is overridden by the code in the previous cell, and thus the action no
longer be available via the keypress r
."
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
'r', function (event) {
Jupyter.keyboard_manager.command_shortcuts.add_shortcut(;
IPython.notebook.execute_cell()return false;
; })
Likewise, to remove a shortcut, use remove_shortcut
:
%%javascript
'r'); Jupyter.keyboard_manager.command_shortcuts.remove_shortcut(
If you want your keyboard shortcuts to be active for all of your
notebooks, put the above API calls into your custom.js
file.
Of course we provide name for majority of existing action so that you
do not have to re-write everything, here is for example how to bind
r
back to it's initial behavior:
%%javascript
'r', 'ipython.change-selected-cell-to-raw-cell'); Jupyter.keyboard_manager.command_shortcuts.add_shortcut(