{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Keyboard Shortcuts in the IPython Shell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you spend any amount of time on a computer, you've probably found a use for keyboard shortcuts in your workflow.\n", "Most familiar perhaps are Cmd-c and Cmd-v (or Ctrl-c and Ctrl-v), used for copying and pasting in a wide variety of programs and systems.\n", "Power users tend to go even further: popular text editors like Emacs, Vim, and others provide users an incredible range of operations through intricate combinations of keystrokes.\n", "\n", "The IPython shell doesn't go this far, but does provide a number of keyboard shortcuts for fast navigation while typing commands.\n", "While some of these shortcuts do work in the browser-based notebooks, this section is primarily about shortcuts in the IPython shell.\n", "\n", "Once you get accustomed to these, they can be very useful for quickly performing certain commands without moving your hands from the \"home\" keyboard position.\n", "If you're an Emacs user or if you have experience with Linux-style shells, the following will be very familiar.\n", "I'll group these shortcuts into a few categories: *navigation shortcuts*, *text entry shortcuts*, *command history shortcuts*, and *miscellaneous shortcuts*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Navigation Shortcuts\n", "\n", "While the use of the left and right arrow keys to move backward and forward in the line is quite obvious, there are other options that don't require moving your hands from the \"home\" keyboard position:\n", "\n", "| Keystroke | Action |\n", "|---------------------------------|--------------------------------------------|\n", "| Ctrl-a | Move cursor to beginning of line |\n", "| Ctrl-e | Move cursor to end of the line |\n", "| Ctrl-b or the left arrow key | Move cursor back one character |\n", "| Ctrl-f or the right arrow key | Move cursor forward one character |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Text Entry Shortcuts\n", "\n", "While everyone is familiar with using the Backspace key to delete the previous character, reaching for the key often requires some minor finger gymnastics, and it only deletes a single character at a time.\n", "In IPython there are several shortcuts for removing some portion of the text you're typing; the most immediately useful of these are the commands to delete entire lines of text.\n", "You'll know these have become second-nature if you find yourself using a combination of Ctrl-b and Ctrl-d instead of reaching for Backspace to delete the previous character!\n", "\n", "| Keystroke | Action |\n", "|-----------------------------|--------------------------------------------------|\n", "| Backspace key | Delete previous character in line |\n", "| Ctrl-d | Delete next character in line |\n", "| Ctrl-k | Cut text from cursor to end of line |\n", "| Ctrl-u | Cut text from beginning of line to cursor |\n", "| Ctrl-y | Yank (i.e., paste) text that was previously cut |\n", "| Ctrl-t | Transpose (i.e., switch) previous two characters |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Command History Shortcuts\n", "\n", "Perhaps the most impactful shortcuts discussed here are the ones IPython provides for navigating the command history.\n", "This command history goes beyond your current IPython session: your entire command history is stored in a SQLite database in your IPython profile directory.\n", "The most straightforward way to access previous commands is by using the up and down arrow keys to step through the history, but other options exist as well:\n", "\n", "| Keystroke | Action |\n", "|-----------------------------------|--------------------------------------------|\n", "| Ctrl-p (or the up arrow key) | Access previous command in history |\n", "| Ctrl-n (or the down arrow key) | Access next command in history |\n", "| Ctrl-r | Reverse-search through command history |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The reverse-search option can be particularly useful.\n", "Recall that earlier we defined a function called `square`.\n", "Let's reverse-search our Python history from a new IPython shell and find this definition again.\n", "When you press Ctrl-r in the IPython terminal, you'll see the following prompt:\n", "\n", "```ipython\n", "In [1]:\n", "(reverse-i-search)`': \n", "```\n", "\n", "If you start typing characters at this prompt, IPython will autofill the most recent command, if any, that matches those characters:\n", "\n", "```ipython\n", "In [1]: \n", "(reverse-i-search)`sqa': square??\n", "```\n", "\n", "At any point, you can add more characters to refine the search, or press Ctrl-r again to search further for another command that matches the query. If you followed along earlier, pressing Ctrl-r twice more gives:\n", "\n", "```ipython\n", "In [1]: \n", "(reverse-i-search)`sqa': def square(a):\n", " \"\"\"Return the square of a\"\"\"\n", " return a ** 2\n", "```\n", "\n", "Once you have found the command you're looking for, press Return and the search will end.\n", "You can then use the retrieved command and carry on with your session:\n", "\n", "```ipython\n", "In [1]: def square(a):\n", " \"\"\"Return the square of a\"\"\"\n", " return a ** 2\n", "\n", "In [2]: square(2)\n", "Out[2]: 4\n", "```\n", "\n", "Note that you can use Ctrl-p/Ctrl-n or the up/down arrow keys to search through your history in a similar way, but only by matching characters at the beginning of the line.\n", "That is, if you type **`def`** and then press Ctrl-p, it will find the most recent command (if any) in your history that begins with the characters `def`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Miscellaneous Shortcuts\n", "\n", "Finally, there are a few miscellaneous shortcuts that don't fit into any of the preceding categories, but are nevertheless useful to know:\n", "\n", "| Keystroke | Action |\n", "|-----------------------------|--------------------------------------------|\n", "| Ctrl-l | Clear terminal screen |\n", "| Ctrl-c | Interrupt current Python command |\n", "| Ctrl-d | Exit IPython session |\n", "\n", "The Ctrl-c shortcut in particular can be useful when you inadvertently start a very long-running job." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While some of the shortcuts discussed here may seem a bit obscure at first, they quickly become automatic with practice.\n", "Once you develop that muscle memory, I suspect you will even find yourself wishing they were available in other contexts." ] } ], "metadata": { "anaconda-cloud": {}, "jupytext": { "formats": "ipynb,md" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }