{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "User Interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook describes the user interface of the IPython Notebook. This includes both mouse and keyboard based navigation and interaction.\n", "\n", "
\n", "As of IPython 2.0, the user interface has changed significantly. Because of this we highly recommend existing users to review this information after upgrading to IPython 2.0. All new users of IPython should review this information as well.\n", "
" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Modal editor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Edit mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Edit mode is indicated by a green cell border and a prompt showing in the editor area:\n", "\n", "\n", "\n", "When a cell is in edit mode, you can type into the cell, like a normal text editor.\n", "\n", "
\n", "Enter edit mode by pressing `enter` or using the mouse to click on a cell's editor area.\n", "
" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Command mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Command mode is indicated by a grey cell border:\n", "\n", "\n", "\n", "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.\n", "\n", "
\n", "Don't try to type into a cell in command mode; unexpected things will happen!\n", "
\n", "\n", "
\n", "Enter command mode by pressing `esc` or using the mouse to click *outside* a cell's editor area.\n", "
" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Mouse navigation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "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." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Keyboard Navigation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "The most important keyboard shortcuts are `enter`, which enters edit mode, and `esc`, which enters command mode.\n", "\n", "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:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `display_edit_shortcuts()` function used here is defined in the [Utilities section](#Utilities) at the bottom of this notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "display_edit_shortcuts()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
esc : command mode
ctrl+m : command mode
shift+enter : run cell
ctrl+enter : run cell, select below
alt+enter : run cell, insert below
alt+- : split cell
meta+s : save notebook
ctrl+s : save notebook
" ], "output_type": "display_data" }, { "javascript": [ "var help = IPython.quick_help.build_edit_help();\n", "help.children().first().remove();\n", "this.append_output({output_type: 'display_data', html: help.html()});" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two other keyboard shortcuts in edit mode that are not listed here:\n", "\n", "* `tab`: trigger \"tab\" completion\n", "* `shift+tab`: open the tooltip" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In command mode, the entire keyboard is available for shortcuts:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "display_command_shortcuts()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
enter : edit mode
shift+enter : run cell
ctrl+enter : run cell, select below
alt+enter : run cell, insert below
y : to code
m : to markdown
t : to raw
1 : to heading 1
2 : to heading 2
3 : to heading 3
4 : to heading 4
5 : to heading 5
6 : to heading 6
up : select previous cell
down : select next cell
k : select previous cell
j : select next cell
ctrl+k : move cell up
ctrl+j : move cell down
a : insert cell above
b : insert cell below
x : cut cell
c : copy cell
v : paste cell below
z : undo last delete
d : delete cell (press twice)
shift+= : merge cell below
s : save notebook
meta+s : save notebook
ctrl+s : save notebook
l : toggle line numbers
o : toggle output
shift+o : toggle output
h : keyboard shortcuts
i : interrupt kernel
. : restart kernel
" ], "output_type": "display_data" }, { "javascript": [ "var help = IPython.quick_help.build_command_help();\n", "help.children().first().remove();\n", "this.append_output({output_type: 'display_data', html: help.html()});" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here the rough order in which we recommend learning the command mode shortcuts:\n", "\n", "1. Basic navigation: `enter`, `shift-enter`, `up/k`, `down/j`\n", "2. Saving the notebook: `s`\n", "2. Cell types: `y`, `m`, `1-6`, `t`\n", "3. Cell creation and movement: `a`, `b`, `ctrl+k`, `ctrl+j`\n", "4. Cell editing: `x`, `c`, `v`, `d`, `z`, `shift+=`\n", "5. Kernel operations: `i`, `.`" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Keyboard shortcut customization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%javascript\n", "\n", "IPython.keyboard_manager.command_shortcuts.add_shortcut('r', {\n", " help : 'run cell',\n", " help_index : 'zz',\n", " handler : function (event) {\n", " IPython.notebook.execute_cell();\n", " return false;\n", " }}\n", ");" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "\n", "IPython.keyboard_manager.command_shortcuts.add_shortcut('r', {\n", " help : 'run cell',\n", " help_index : 'zz',\n", " handler : function (event) {\n", " IPython.notebook.execute_cell();\n", " return false;\n", " }}\n", ");" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a couple of points to mention about this API:\n", "\n", "* The `help_index` field is used to sort the shortcuts in the Keyboard Shortcuts help dialog. It defaults to `zz`.\n", "* 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 the `event` object or event handling, see the jQuery docs.\n", "* If you don't need a `help` or `help_index` field, you can simply pass a function as the second argument to `add_shortcut`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%javascript\n", "\n", "IPython.keyboard_manager.command_shortcuts.add_shortcut('r', function (event) {\n", " IPython.notebook.execute_cell();\n", " return false;\n", "});" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "\n", "IPython.keyboard_manager.command_shortcuts.add_shortcut('r', function (event) {\n", " IPython.notebook.execute_cell();\n", " return false;\n", "});" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Likewise, to remove a shortcut, use `remove_shortcut`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%javascript\n", "\n", "IPython.keyboard_manager.command_shortcuts.remove_shortcut('r');" ], "language": "python", "metadata": {}, "outputs": [ { "javascript": [ "\n", "IPython.keyboard_manager.command_shortcuts.remove_shortcut('r');" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want your keyboard shortcuts to be active for all of your notebooks, put the above API calls into your `/static/custom/custom.js` file." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Utilities" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use the following functions to generate the keyboard shortcut listings above." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Javascript, display, HTML\n", "\n", "t = \"\"\"var help = IPython.quick_help.build_{0}_help();\n", "help.children().first().remove();\n", "this.append_output({{output_type: 'display_data', html: help.html()}});\"\"\"\n", "\n", "def display_command_shortcuts():\n", " display(Javascript(t.format('command')))\n", "\n", "def display_edit_shortcuts():\n", " display(Javascript(t.format('edit')))\n", "\n", "display(HTML(\"\"\"\n", "\n", "\"\"\"))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 16 } ], "metadata": {} } ] }