4

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
5

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 a 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)))
      (raise-frame frame)
      (select-frame frame)
      (select-window win))))
  • This 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 5 '16 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. – A. Blizzard Sep 24 '18 at 17:29
  • @A.Blizzard I’m not seeing that. What version of Emacs are you using? – glucas Sep 26 '18 at 0:04
0

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. – wdkrnls Jan 15 '15 at 16:43
  • 1
    Right, 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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