32

Possible Duplicate:
Emacs, switch to previous window

other-window advances me to the next window in the current frame, but I also want a way to move back to the previous window.

Emacs has next-buffer and previous-buffer, but no analogous interactive functions for window navigation. Just other-window.

Community
  • 1
  • 1
someguy
  • 1,923
  • 5
  • 21
  • 19
  • NOTE: `frame` and `window` are different beasts in Emacs terminology. Please, replace `frame` by `window` in your question to avoid confusion. – jfs Oct 01 '08 at 00:00
  • frame/window confusion fixed. – phils Feb 25 '11 at 12:46

6 Answers6

35

Provide a negative argument with C-u - ("Control+U" then "minus"), or even more simply C-- ("Control minus").

  • Move to previous window: C-- C-x o
  • Move to previous frame: C-- C-x 5 o

From code, (other-window -1) or (other-frame -1) will do the same thing.

Check out the help for the key you want to reverse (e.g. C-h k C-x o to show help for C-x o) and if it says "A negative argument..." you know you can use C--.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
Matt Curtis
  • 23,168
  • 8
  • 60
  • 63
  • A bit strange but when I have a layout 2x3 windows with windows 1-6 using `C-x o` moves from 3 to 4, but in 4 using `C-u - C-x o` moves from 4 to 2. What would move me from 4 to 3 ? – Zitrax Aug 05 '10 at 12:18
18

This is an old post, but I just wondered the same. It seems there now is a function for this in Emacs: previous-multiframe-window.

I have it bound to C-x O, as in uppercase letter o. Now I just throw in shift when I want to go backwards.

(global-set-key (kbd "C-x O") 'previous-multiframe-window)

ogrim
  • 968
  • 9
  • 17
  • 1
    One issue with this is that if you have multiple frames this will cycle between them all, when you might want to stay within the current frame only. – Malvineous Jan 15 '12 at 08:03
10

Put this in your .emacs, and bind it to whatever key you like

(defun back-window ()
  (interactive)
  (other-window -1))
Lawrence D'Anna
  • 2,998
  • 2
  • 22
  • 25
  • 2
    byte-code: Wrong number of arguments: (lambda nil (interactive) (other-window -1)), 3 – lawlist May 05 '13 at 19:13
  • @lawlist I got the same error trying to do the same thing as above, only I named my function "previous-window". The solution was to change the name of the function, because it seemed to conflict with some internal emacs define. Might help your case too. See here: http://stackoverflow.com/questions/33266109/emacs-defining-function-calling-other-window-with-an-argument-doesnt-work/33266299#33266299 – Colin Oct 22 '15 at 15:30
  • @Colin -- There is already a built-in function named `previous-window`, so you do **not** want to create your own using the same name -- pick a different name instead. I am not able to remember the circumstances surrounding my attempted usage of this answer, but I see from the date (May 5, 2013) that I was probably fairly new to Emacs and was just learning how to navigate windows. Based on my current level of Emacs abilities, I see nothing wrong with the code -- there is no built-in function named `back-window` in the latest developmental master/trunk that would cause a conflict, etc. – lawlist Oct 22 '15 at 16:01
6

A slightly less annoying shortcut available by default isC-- C-x o. That way you don't have to switch between Meta and Control while typing the prefixes.

remcycles
  • 1,246
  • 13
  • 14
5

Different from what you asked for, but the windmove package lets you move between windows according to their relative screen locations, which can be much easier than repeatedly doing C-x o.

Alex Coventry
  • 68,681
  • 4
  • 36
  • 40
3

Instead of C-u -, you can also give a negative prefix argument with just M-- (Meta-Minus) , i.e. switch to previous window with M-- C-x o.

Florian Jenn
  • 5,211
  • 4
  • 23
  • 18