Some major modes have code and key-bindings to toggle focus back and forth between their inferior processes and the major-mode buffer. Is there an Emacs function which lets me do this between the current and previous window such that calling the function repeatedly would only switch back between the last two used windows?
-
I've set up my function key (on the Apple keyboard) and arrows (with the `fn` key depressed) to go to the window in whatever direction arrow I press, including in and out of the minibuffer. I suppose if I had 5 or 6 windows and I wanted to jump between 2 and 5 (on a regular basis), a toggle function might be helpful. Here is an example of **fn+right-arrow**: `(if (window-in-direction 'right) (select-window (window-in-direction 'right)) (other-window 1))` It is either `'left`, `'right`, `'above`, or `'below`; and, of course, a **negative** argument on the `other-window` for left and down. – lawlist Jan 15 '15 at 16:53
2 Answers
Here's a quick implementation using other-window
and a variable to track the direction.
(defvar last-window-direction 1)
(defun last-window ()
(interactive)
(other-window last-window-direction)
(setq last-window-direction (- 0 last-window-direction)))
REVISED
After a few iterations using hooks to keep track of the last window, I found that Emacs already has the get-mru-window
function for this (of course) . I'm replacing the previous example with this function that works across multiple frames. I have not tested extensively but it seems pretty handy so far.
(defun switch-to-last-window ()
(interactive)
(let ((win (get-mru-window t t t)))
(unless win (error "Last window not found"))
(let ((frame (window-frame win)))
(select-frame-set-input-focus frame)
(select-window win))))

- 3
- 1

- 20,175
- 1
- 51
- 83
-
1This looks great. I wonder if `previous-multiframe-window` might be helpful for handling multiple frames. – wdkrnls Jan 15 '15 at 22:36
-
1@wdkrnls Turns out there is a `get-mru-window` function that can provide the most-recently used window across frames. I've change the answer to provide a nicer solution based on that. Going to add this to my own config too. :) – glucas Jan 16 '15 at 14:16
-
This comment works exactly as I want after viewing several pages, a lot of solutions like `C-- C-x o` will not loop between two windows. – CodyChan Jan 05 '16 at 07:16
-
Is it me or with multi-frames the focus changes but the cursors stays in the last window? So, when I start typing I still type at on the current window. – Arktik Sep 24 '18 at 17:29
-
-
I am using 26.1; I wonder if it has anything to do with Evil. I can see that the last window gets selected on the other frame, but it seems the buffer doesn't get selected and when I start typing the window focus switches back to where it was, – Arktik Sep 26 '18 at 18:30
-
-
Adding `(select-frame-set-input-focus frame)` line after `(select-window win)` solved the problem of cursor staying on the current window. But there is another problem now: imagine I have two frames, one with one window w1 and the other with two w2 and w3. When I go from w2 to w1 the switch to last window works fine. But when I go w2 -> w1 -> w3, I can see as I move to w3 cursor appears in w2 and quickly jumps to w3. So the next switch to the last window goes to w2 instead of w1. – Arktik Sep 28 '18 at 20:38
-
This was sometimes switching between frames for me - which was not the behaviour I wanted. I used `(get-mru-window nil t t)` to avoid this behaviour. – Att Righ Dec 02 '22 at 12:18
From documentation:
C-x o runs the command other-window, which is an interactive compiled Lisp function in `window.el'.
It is bound to C-x o. (other-window COUNT &optional ALL-FRAMES)
Select another window in cyclic ordering of windows. COUNT specifies the number of windows to skip, starting with the selected window, before making the selection. If COUNT is positive, skip COUNT windows forwards. If COUNT is negative, skip -COUNT windows backwards. COUNT zero means do not skip any window, so select the selected window. In an interactive call, COUNT is the numeric prefix argument. Return nil.
So, you can call other-window
function with -1
as argument to go to the previous window: C-u - 1
C-x o
. You will probably want a custom shortcut.

- 6,341
- 2
- 22
- 35
-
As far as I can tell `other-window` only moves up and down the stack. The behavior I'm looking for is toggling between two windows when more than 2 are visible. – wdkrnls Jan 15 '15 at 16:43
-
1Right, to use `other-window` you would need to alternate between `C-x o` and `C-- C-x o`. – glucas Jan 15 '15 at 16:47