【发布时间】:2026-01-08 01:35:01
【问题描述】:
这里的问题与Change the "send code to interpreter" (C-c |) command in python-mode 相关(但不相同)和补充。
我在 Mac 10.9.5、Emacs 24.4、Python 2.7.8 和 IPython 2.2.0 上工作。
我的想法是更改C-c C-r emacs 命令以将IPython 模式下的区域/代码行发送到C-RET,就像使用R 时一样。这是因为我通常使用R,但从现在开始,我我将使用 R 和 Python(尤其是 IPython,我真的很喜欢),C-RET --已经是 R 中的发送代码命令 -- 对我来说似乎更舒服。
在这个问题开头引用的链接中,他们建议将以下行添加到.emacs 文件中,以将C-c | 命令更改为C-c C-r:
(eval-after-load "python"
'(progn
(define-key python-mode-map (kbd "C-c C-r") 'python-shell-send-region)))
目前,.emacs 文件中的 python/IPython 配置如下所示:
;; Enable Python
(add-to-list 'load-path "/sw/lib/python-mode-1.0")
(load "python-mode")
(setq auto-mode-alist
(cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
(cons '("python" . python-mode)
interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)
;; Ipython. This python-mode takes the Key-map and the menu
(when (executable-find "ipython")
(setq
python-shell-interpreter "ipython"
python-shell-interpreter-args ""
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
python-shell-completion-setup-code
"from IPython.core.completerlib import module_completion"
python-shell-completion-module-string-code
"';'.join(module_completion('''%s'''))\n"
python-shell-completion-string-code
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n"))
这是两种并行运行的 Python 模式,第二种(IPython,我一直使用的一种)采用键映射和菜单(顺便说一下,欢迎任何关于更好配置的建议。IPython部分基于:How to open IPython interpreter in emacs?)。
我曾尝试在我的 python 配置末尾添加前面描述的(eval-after-load "python" '(progn ... 命令(当然,将C-c C-r 更改为C-RET 或C-ret 甚至C-<return>)。
我也尝试过不同形式的 when (executable-find "ipython") ... 块(例如简单的 (define-key python-mode-map (kbd "C-c C-r") 'python-shell-send-region) )。但似乎没有任何效果。
因此,我的问题是:鉴于我的 Python/IPython 配置,我必须在我的 .emacs 文件中包含什么才能将 C-c C-r 命令更改为 (C-RET)
非常感谢!
【问题讨论】:
标签: python macos emacs ipython