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?
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))))
-
1This looks great. I wonder if
previous-multiframe-window
might be helpful for handling multiple frames.– wdkrnlsJan 15, 2015 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. :)– glucasJan 16, 2015 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.– CodyChanJan 5, 2016 at 7: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.– ArktikSep 24, 2018 at 17:29
-
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.
-
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.– wdkrnlsJan 15, 2015 at 16:43 -
1Right, to use
other-window
you would need to alternate betweenC-x o
andC-- C-x o
.– glucasJan 15, 2015 at 16:47
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 theother-window
for left and down.