Javier Javier - 1 year ago 92
Python Question

Change the Emacs “send code to interpreter” C-c C-r command in IPython-mode

The question here is related (but NOT identical) and complementary to Change the "send code to interpreter" (C-c |) command in python-mode .

I work on a Mac 10.9.5, Emacs 24.4, Python 2.7.8 and IPython 2.2.0.

My idea is to change the

C-c C-r
emacs command to send a region/line of code in IPython mode to
, as when using R. This is because I usually work with R, but from now on, I am going to be using R and Python (IPython in particular, which I really like), and
--already the send code command in R-- seems more comfortable to me.

In the link cited at the beginning of this question they suggest to add the following lines to the
file to change the
C-c |
command into
C-c C-r

(eval-after-load "python"
(define-key python-mode-map (kbd "C-c C-r") 'python-shell-send-region)))

At the moment, my python/IPython configuration in my
file looks like this:

;; 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)
(autoload 'python-mode "python-mode" "Python editing mode." t)

;; Ipython. This python-mode takes the Key-map and the menu
(when (executable-find "ipython")
python-shell-interpreter "ipython"
python-shell-interpreter-args ""
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
"from IPython.core.completerlib import module_completion"

Which are two python modes running in parallel and the second one (IPython, the one I use all the time) takes the key-map and the menu (by the way, any suggestion for a better configuration is welcome. The IPython section is based on: How to open IPython interpreter in emacs?).

I have tried to add the
(eval-after-load "python" '(progn ...
command described before at the end of my python configuration (of course, changing
C-c C-r
or even

I have also tried within the
when (executable-find "ipython") ...
chunk in different forms (such as simply
(define-key python-mode-map (kbd "C-c C-r") 'python-shell-send-region)
). But nothing seems to work.

Therefore, my question would be: Given my Python/IPython configuration, what do I have to include in my
file to change the
C-c C-r
command into (C-RET)

Many thanks in advance!

Answer Source

Use (kbd "RET")

Try this with 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))

BTW unless IPython-exclusiv features are needed, recommend to execute code from Emacs through a common Python. IPython implements a bunch of cool things, which might appear orthogonal to Emacs, which also implements a bunch of cool things.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download