I would like to implement an interactive function that, in the "canonical" case, selects the window that was selected before the currently selected one1.

But my plan founders immediately on this question: how can my future function find out, to begin with, what the previously selected window is???

1 There are a few edge cases, e.g. when no other window was previously selected, that I won't get into.

  • 2
    C-h i g(elisp)Selecting windows has some information that might be helpful.
    – NickD
    Aug 17, 2021 at 14:29

1 Answer 1


Since Emacs 27.1, the previously selected window is recorded and can be obtained with old-selected-window. The previously selected window in each frame can be obtained with frame-old-selected-window. If you want older information, you can record it with a hook in window-selection-change-functions.

In older versions of Emacs, I don't think Emacs remembers any historical information about window selections by default, and there's no hook for window selection. You would have to advise at least select-window, and quite possibly other built-in functions that change the window selection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.