在IPython模式下更改Emacs“将代码发送到解释器” Cc Cr命令


问题内容

这里的问题与(但不完全相同)相关,并且与在python-mode中更改“将代码发送到解释器”(Cc
|)命令有关

我在Mac 10.9.5,Emacs 24.4,Python 2.7.8和IPython 2.2.0上工作。

我的想法是将C-c C-remacs命令更改为在IPython模式下将代码的区域/行发送为C-RET,就像使用R时那样。这是因为我通常使用R,但是从现在开始,我将使用R和Python(特别是IPython,我真的很喜欢),而且-C-RET已经使用R中的send
code命令-对我来说似乎更舒适。

在此问题开头引用的链接中,他们建议在.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部分基于on:如何在emacs中打开IPython解释器?)。

我试图添加(eval-after-load "python" '(progn ...在我的蟒配置结束之前描述的命令(当然,改变C-c C-rC-RETC-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)
,我必须在文件中包含哪些内容?

提前谢谢了!


问题答案:

采用 (kbd "RET")

尝试使用python.el

(eval-after-load "python"
  '(define-key python-mode-map [(control c)(kbd "RET")] 'python-shell-send-region))

WRT python-mode.el:

(eval-after-load "python-mode"
  '(define-key python-mode-map [(control c) (kbd "RET")] 'py-execute-region))

顺便说一句,除非需要IPython-
exclusiv功能,否则建议通过通用Python从Emacs执行代码。IPython实现了很多很酷的东西,这些东西看起来与Emacs正交,Emacs也实现了很多很酷的东西。