thor
thor

Reputation: 22480

emacs switch to last active window?

I was wondering if there is a way in Emacs lisp to jump back to the last active window, just as popd would do in Linux?

The reason I ask is that in some environments for evaluating code (e.g. babel-repl) the editing area for the source code loses focus to the REPL once the REPL is launched. I'd like to change its behavior and switch the focus back to the editing area, e.g., by adding an additional command in elisp to jump back to the last active window before launching the REPL.

Upvotes: 1

Views: 1002

Answers (2)

James Anderson
James Anderson

Reputation: 589

If you are interested in switching just the active buffer, you can use:

(defun prev-window ()
  (interactive)
  (other-window -1))
(global-set-key [(f12)] 'prev-window)

(global-set-key "\C-cp" 'prev-window)
(global-set-key "\C-cn" 'other-window)

I have this in my ~/.emacs file. CTRL+C then p (or F12) to go to the previous buffer. CTRL+C then n to go to the next buffer. The function other-buffer lets you revisit the last visited buffer. By supplying a negative number you can reverse the order that it goes through the buffer list.

Upvotes: 0

Alejandro C.
Alejandro C.

Reputation: 3801

previous-window or get-mru-window should do what you want. You can then switch with

(select-window (previous-window))

Upvotes: 4

Related Questions