如何在emacs python-mode中取消解释器命令

7

当我在Python解释器中输入命令时,如果打错了,有时候只会看到...。例如,如果我输入help(random.unif然后按回车键,我就不能输入新的命令。我必须退出emacs并重新开始Python和解释器。有没有办法解决这个问题?

2个回答

8
作为Pavel Anossov所解释的,您想向Python发送CTRL-C来中断它。
但是在emacs中,默认情况下,CTRL-C是一个前缀键。
幸运的是,在大多数交互式shell模式中,包括python-mode和替代模式,连续两次按下CTRL-C会将ctrl-C发送到解释器。或者更准确地说,CTRL-CCTRL-C被绑定到comint-interrupt-subjob。(当然,您也可以以任何其他方式运行它,甚至是META-X comint-interrupt-subjob,如果您真的想这样做。)结果看起来像这样:
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(f
...   ^C ^C
KeyboardInterrupt
>>> 

另一种选择是使用quoted-insert命令,通常绑定到CTRL-Q,然后按下CTRL-C。但是,由于这不会中断通常的行输入,因此通常需要在其后面加上一个新行。结果看起来像这样:

Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help(f
... ^C
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> 

1
通常情况下,使用CTRL-C即可。不确定嵌入式emacs解释器是否适用。或者,只需向解释器提供它正在等待的内容(在您的示例中,)。

问题在于知道如何向emacs解释器发送<kbd>CTRL-C</kdb>(以及如何使它以与控制台中的<kdb>CTRL-C</kdb>相同的方式中断正常的readline-esque模式,尽管如果您未能这样做,只需发送一行什么也没有但是<kbd>CTRL-C</kdb>,基本上也可以工作)。 - abarnert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接