Launch py-shell command in another emacs window

176 views Asked by At

When using python-mode in Emacs, I first split the screen via C-x 3. I'd like to be able do C-c ! to launch py-shell in the other window, not in the window currently active. How can I configure Emacs to do that without having to switch windows with C-x o before launching the shell?

I'm using Emacs 24.3.1, and I've got all my configuration files in ~/.emacs.d.

I just installed the python-mode package using package-install with the Marmalade repository, and I haven't yet edited any .el file related to python-mode.

2

There are 2 answers

0
itsjeyd On BEST ANSWER

As @BleedingFingers says you can simply use a macro and bind that to a key. It's up to you whether or not you want to re-use the C-c ! binding for the macro or bind it to a different key.

Here's how to proceed should you decide to go with the macro option, starting with Emacs showing only a single window:

Define macro

F3

C-x 3

C-x o

M-x py-shell RET

C-x o

F4

Assign name to macro

M-x name-last-kbd-macro RET py-shell-other-window RET

You can replace py-shell-other-window with whatever name you would like to use for the macro.

Add macro to your configuration

Open your configuration file, move point (cursor) to an empty line and do

M-x insert-kbd-macro RET

This will insert the macro definition into your configuration file.

Bind macro to key

Add the following code to your configuration file to bind the macro to a key in python-mode:

(require 'python-mode) ; Make sure python-mode-map is available
                       ; for modification
(define-key python-mode-map (kbd "C-c !") nil) ; Unset default binding 
                                               ; for C-c !
                                               ; (not necessary if you choose an
                                               ; unused binding)
(define-key python-mode-map (kbd "C-c !") 'py-shell-other-window) ; Bind macro to C-c !

Turn key binding on

Mark the lines added in the previous step and run M-x eval-region RET, or simply restart Emacs.

Celebrate :)

0
PythonNut On

Advice lets you redefine code in other libraries on-the-fly.

(defadvice py-shell (around auto-split activate)
    (split-window-right)
    (other-window)
    ,ad-do-it
    (other-window))

Variations apply.