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?
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)))
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))))
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.