Skip to content

Toggling between Python buffers

by mickey on February 23rd, 2011

I work with Python a lot, and as any swashbuckling Pythoneer knows, using the Python REPL is a big part of the day-to-day routine.

I often find myself switching from one python buffer to the shell (also known in Emacs lingo as the inferior python process) and back again. That means copious use of C-x b or winner-mode yet.. it just doesn’t feel right. Sure, I could use C-c C-z (in Emacs’ own Python mode) but to me that’s a different use case.

I wanted something easier, so I wrote this a while back to toggle from a Python buffer to the shell, and from the shell you can go back to the last buffer you toggled from. In effect, it works like a very localized “alt-tab”.

Now I can quickly toggle between buffer and shell with a keypress. I’ve set it up so it won’t split windows but will instead switch the active buffer — like when you use C-x b.

I bind the toggle command to F12 in all Python buffers and in the shell, so it’s easy to reach. Feel free to change the keybind if you like (read Mastering Key Bindings in Emacs to learn how.)

Note: I can’t promise it’ll work in the non-Emacs Python mode!

  1. srlh permalink

    Thank you for sharing.
    I have read the documentation for ‘make-variable-buffer-local’ but still couldn’t understand it. Could you please tell me what it means to make a variable ‘buffer local’?

    • mickey permalink

      Well, a buffer local variable is a variable where, when you assign a value to it, its value is local to only the buffer it was assigned from.

      If you type M-: (info "(emacs) Locals") RET Emacs will open an info manual page on the subject for you to read further.

  2. Tom Regner permalink


    I found that intriguing :-) I generalized the idea, and now there’s a minor mode available at MELPA — repl-toggle — that allows you to associate major-modes with repl commands, and switch back and forth like you described here, with the same key-stroke everywhere (‘C-c C-z’ per default, it was suggested to me that this is what all the modern repl modes use :-) )

    So thanks for inspiration and kind regards,

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS