The text is released under the CC-BY-NC-ND license, and code is released under the MIT license. If you find this content useful, please consider supporting the work by buying the book!
如果你经常使用电脑的话,你会发现快捷键可以让你的操作更便捷。最常见的应该是基本在所有系统和应用中都有的复制粘贴快捷键 Cmd-C 和 Cmd-V 吧(在 Windows 里是 Ctrl-C 和 Ctrl-V)。更厉害的会使用 Emacs,Vim 这样的编辑器,它们为用户提供了强大的快捷键系统。
If you spend any amount of time on the computer, you've probably found a use for keyboard shortcuts in your workflow. Most familiar perhaps are the Cmd-C and Cmd-V (or Ctrl-C and Ctrl-V) for copying and pasting in a wide variety of programs and systems. 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.
IPython 不至于这么复杂,但是它提供了足够多的快捷键可以帮助用户快速的定位和查找。实际上 IPython 所提供的快捷键也不是自己的,IPython 以来一个 GNU 的输入库,它提供了一系列的快捷键。并且下面展示的一些快捷方式可能根据您的系统配置而有所不同。虽然其中一些快捷方式也支持在浏览器中运行的 jupyter notebook,但是本节主要是讨论在 IPython shell 中的快捷键操作。
The IPython shell doesn't go this far, but does provide a number of keyboard shortcuts for fast navigation while typing commands. These shortcuts are not in fact provided by IPython itself, but through its dependency on the GNU Readline library: as such, some of the following shortcuts may differ depending on your system configuration. Also, while some of these shortcuts do work in the browser-based notebook, this section is primarily about shortcuts in the IPython shell.
一旦你熟悉了这套操作,在执行一些命令的时候你甚至都不用把自己的双手移出你的键盘(译者注:推测作者的意思是不需要鼠标或者让你的双手离开打字时两个食指默认的 F 和 J 太远的地方吧)。如果你是 Emacs 用户或者用过类 Linux 的 shell 下面的操作你会非常的熟悉。我们会把快捷键按照以下几个类型一一讲解:导航快捷键,文本编辑快捷键,命令历史快捷键,其他快捷键。
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. If you're an Emacs user or if you have experience with Linux-style shells, the following will be very familiar. We'll group these shortcuts into a few categories: navigation shortcuts, text entry shortcuts, command history shortcuts, and miscellaneous shortcuts.
大多数人都是用左右箭头做索引,但是有其他更容易的方法:这样你就不需要让右手食指离开默认的 J 键位了:
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:
输入 | 作用 |
---|---|
Ctrl-a |
移动光标到行首 |
Ctrl-e |
移动光标到行尾 |
Ctrl-b or the left arrow key |
光标向后移动一个字符 |
Ctrl-f or the right arrow key |
光标向前移动一个字符 |
Keystroke | Action |
---|---|
Ctrl-a |
Move cursor to the beginning of the line |
Ctrl-e |
Move cursor to the end of the line |
Ctrl-b or the left arrow key |
Move cursor back one character |
Ctrl-f or the right arrow key |
Move cursor forward one character |
Backspace 是大多数人都知道的文本删除键,你需要把你的右手小指去敲击它,并且它一次只能删除一个字符。在 IPython 有一些其他的操作可以更快的删除文本,比如删除一整行文本。在了解了下面的快捷键并稍加联系,你会发现 Ctrl-b 和 Ctrl-d 一起使用要比 Backspace 更方便。
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. 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. 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!
输入 | 作用 |
---|---|
Backspace key | 删除前一个字符 |
Ctrl-d |
删除后一个字符 |
Ctrl-k |
删除光标到行尾的字符 |
Ctrl-u |
删除从行首到光标的字符 |
Ctrl-y |
粘贴之前拷贝的字符 |
Ctrl-t |
交换前两个字符的位置 |
Keystroke | Action |
---|---|
Backspace key | Delete previous character in line |
Ctrl-d |
Delete next character in line |
Ctrl-k |
Cut text from cursor to end of line |
Ctrl-u |
Cut text from beginning of line to cursor |
Ctrl-y |
Yank (i.e. paste) text that was previously cut |
Ctrl-t |
Transpose (i.e., switch) previous two characters |
在这部分最棒的快捷键可能就是这里要讲的查找历史命令的快捷键了。并且这里讲的操作历史也不仅仅是当前这个 IPython 回话里的操作,而是你全部的历史命令,因为它们都保存在了 IPython 的 SQLite 数据库里了。当然,使用上下箭头就可以获取这些命令了,但是还有一些其他的快捷键:
Perhaps the most impactful shortcuts discussed here are the ones IPython provides for navigating the command history. This command history goes beyond your current IPython session: your entire command history is stored in a SQLite database in your IPython profile directory. The most straightforward way to access these is with the up and down arrow keys to step through the history, but other options exist as well:
输入 | 作用 |
---|---|
Ctrl-p (or the up arrow key) |
前一个命令 |
Ctrl-n (or the down arrow key) |
后一个命令 |
Ctrl-r |
搜索之前的命令 |
Keystroke | Action |
---|---|
Ctrl-p (or the up arrow key) |
Access previous command in history |
Ctrl-n (or the down arrow key) |
Access next command in history |
Ctrl-r |
Reverse-search through command history |
搜索命令非常使用。在上一章节我们创建了一个 square
函数,让我们通过搜索命令快捷键找回这个定义:当你键入 Ctrl-r 你会看到类似的弹出框:
The reverse-search can be particularly useful.
Recall that in the previous section we defined a function called square
.
Let's reverse-search our Python history from a new IPython shell and find this definition again.
When you press Ctrl-r in the IPython terminal, you'll see the following prompt:
In [1]:
(reverse-i-search)`':
如果你继续输入字符,IPython 会帮你自动补全为符合当前输入的最近一次调用的命令(当然,如果没有就不会补全):
If you start typing characters at this prompt, IPython will auto-fill the most recent command, if any, that matches those characters:
In [1]:
(reverse-i-search)`sqa': square??
你可以任意补全这个搜索词,或者是再键入 Ctrl-r 来查找下一个符合当前搜索词的命令。如果你有执行上一章节的操作,键入 Ctrl-r 后的结果可能会是这样的:
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 in the previous section, pressing Ctrl-r twice more gives:
In [1]:
(reverse-i-search)`sqa': def square(a):
"""Return the square of a"""
return a ** 2
在找到了你想要的命令之后回车就能结束这次搜索。接下来这行我们找到的这个命令:
Once you have found the command you're looking for, press Return and the search will end. We can then use the retrieved command, and carry-on with our session:
In [1]: def square(a):
"""Return the square of a"""
return a ** 2
In [2]: square(2)
Out[2]: 4
Ctrl-p/Ctrl-n 或者上下箭头都能用于搜索历史操作,但是它们只能按照首字符串来匹配。比如你键入了 def
然后 Ctrl-p 它只会找到以 def
开头的历史命令。
Note that Ctrl-p/Ctrl-n or the up/down arrow keys can also be used to search through history, but only by matching characters at the beginning of the line.
That is, if you type def
and then press Ctrl-p, it would find the most recent command (if any) in your history that begins with the characters def
.
还有一些其他常用的快捷键:
Finally, there are a few miscellaneous shortcuts that don't fit into any of the preceding categories, but are nevertheless useful to know:
输入 | 动作 |
---|---|
Ctrl-l |
清屏 |
Ctrl-c |
终止当前 IPython 命令 |
Ctrl-d |
退出当前 IPython 回话 |
Keystroke | Action |
---|---|
Ctrl-l |
Clear terminal screen |
Ctrl-c |
Interrupt current Python command |
Ctrl-d |
Exit IPython session |
Ctrl-c 经常用于中断一个运行时间很长的命令。
The Ctrl-c in particular can be useful when you inadvertently start a very long-running job.
乍看这些快捷键可能觉得很繁琐,但是稍加联系就会觉得它们非常实用。一旦你建立了肌肉记忆你会希望在其他的系统也能用到这些方法。
While some of the shortcuts discussed here may seem a bit tedious at first, they quickly become automatic with practice. Once you develop that muscle memory, I suspect you will even find yourself wishing they were available in other contexts.