99

In Emacs, C-x o takes me to the next window.

What keyboard macro takes me to the previous window in Emacs?

Tyler
  • 9,872
  • 2
  • 33
  • 57
Roger C S Wernersson
  • 6,362
  • 6
  • 34
  • 45

13 Answers13

110

You might also want to try using windmove which lets you navigate to the window of your choice based on geometry. I have the following in my .emacs file to change windows using C-x arrow-key.

(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <right>") 'windmove-right)
(global-set-key (kbd "C-x <left>") 'windmove-left)
Nate
  • 6,779
  • 8
  • 28
  • 21
  • coool~~ you have my vote. I am new to emacs (but love it... genius). BTW, what is the 'kbd' standing for?? – lkahtz Feb 11 '11 at 17:22
  • BTW, that's similar to [ratpoison](http://www.nongnu.org/ratpoison/)'s commands for [moving to other "frames"](http://www.nongnu.org/ratpoison/doc/Default-Key-Bindings.html#Default%20Key%20Bindings): `C-t Up`, `C-t Down`, etc. – imz -- Ivan Zakharyaschev May 22 '11 at 08:49
  • 15
    Note that calling `(windmove-default-keybindings)` binds these functions to SHIFT+up/down/left/right, which I think is more convenient than your `C-x` bindings (which conflict with the also-useful default bindings for `previous-buffer` and `next-buffer`. lkahtz: the `(kbd)` function lets you specify keys in string notation in the more-readable syntax which is also used by Emacs when you use `C-h k` or `C-h c` to describe a binding. – phils Feb 16 '12 at 06:51
  • 4
    I don't approve of using arrow keys. But I upvoted for the helpfulness anyway. :smug: – Bjorn Aug 11 '13 at 17:06
  • Can't find windmove on MELPA, but window-jump works the same way. http://melpa.milkbox.net/#/window-jump – apennebaker Jan 23 '14 at 20:33
  • 2
    @phils SHIFT + meddles with shift selection that is enabled by default from Emacs 23 (shift-selection-mode). – legends2k Jul 22 '14 at 12:23
  • 2
    legends2k: True; although you can also pass an argument to `windmove-default-keybindings` to specify a different modifier to use in conjunction with the arrow keys; so that function can still be handy for people who use shift-selection-mode. – phils Jul 22 '14 at 14:05
  • 1
    @phils Agreed, _meta_ seems to be a good alternative; [as seen in Emacs Wiki](https://www.emacswiki.org/emacs/WindMove). – legends2k Jun 02 '17 at 18:16
  • If you use windmove [I do], I highly suggest using framemove too. – Bret Weinraub Dec 19 '17 at 12:17
94

That'd be C-- C-x o

In other words, C-x o with an argument of -1. You can specify how many windows to move by inserting a numeric argument between C-u and the command, as in C-u 2 C-x o. (C-- is a shortcut for C-u - 1)

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
JB.
  • 40,344
  • 12
  • 79
  • 106
32

Personally I prefer to use window-number.el

To select a different window, use Ctrl-x, Ctrl-j n

Where n is the number of the window, the modeline of each window shows it's number, as shown in the screenshot.

Just download window-number.el, place it in your emacs load-path and use the following in your .emacs

 (autoload 'window-number-mode "window-number"
   "A global minor mode that enables selection of windows according to
 numbers with the C-x C-j prefix.  Another mode,
 `window-number-meta-mode' enables the use of the M- prefix."
   t)

There's another similar mode called switch-window.el which gives you big numbers in the windows... (pressing the number switches the window and reverts the display.)


(source: tapoueh.org)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ocodo
  • 29,401
  • 18
  • 105
  • 117
  • 2
    Nice, but I'd suggest not binding something to `C-x C-j`, as that's the default binding for `dired-jump` if you `(require 'dired-x)`. (See `M-x customize-group RET dired-keys RET` if you want to override that.) – phils Jun 25 '12 at 06:28
  • 1
    After posting this I decided to give `switch-window.el` a go, it uses `C-x o` instead, and when there's only two windows active, it just selects the other window. I think it's wisest to rebind `C-x o` to do the `window-number` trick, you're quite right about `dired-jump`. Personally I do less window switching and use `C-x b` myself, but having the enhanced `C-x o` is quite pleasing. – ocodo Jun 26 '12 at 02:45
  • wow amazing Emacs theme in first screenshot. how can I get it? – semente Jul 16 '12 at 22:30
  • @semente on my Emacs related page http://jasonm23.github.com/EmacsFodder/Emacs/2012/06/29/two-new-emacs24-themes/ – ocodo Jul 17 '12 at 06:26
  • @Slomojo the url to your emacs related page ends up in a 404. I'm two years later, I know, but do you have any other page to look at? thanks. – petrux Nov 25 '14 at 16:15
  • http://jasonm23.github.io or http://emacsfodder.github.io should have what you are looking for. Please note the mode line formatter shown (main-line) isn't maintained, (no power-line version is, that I know of.) – ocodo Nov 25 '14 at 23:13
  • 1
    @petrux http://emacsfodder.github.io/blog/two-new-emacs24-themes/ I think this is probably one of the two themes. - btw. For a cool mode line try https://gist.github.com/jasonm23/8554119 – ocodo Nov 25 '14 at 23:18
14

If you work with multiple emacs windows (>3) a lot and you will want to save some keystrokes add this to your init file and you'll be better off:

(defun frame-bck()
  (interactive)
  (other-window-or-frame -1)
)
(define-key (current-global-map) (kbd "M-o") 'other-window-or-frame)
(define-key (current-global-map) (kbd "M-O") 'frame-bck)

Now just cycle quickly thru the windows with M-o

octi
  • 1,470
  • 1
  • 17
  • 28
  • 1
    This is exactly the solution I needed. All I wanted was for the window switching command to be a single repeating keystroke. Thanks! I did have to change other-window-or-frame to other-window to make it work, FYI. I'm on Emacs 24.2.1. – Geoff Jun 09 '13 at 02:16
  • I think these are very good bindings (especially when you have 2+ windows), thanks. As said also hat to change to `other-window` to make it work. – Ev Dolzhenko Sep 08 '14 at 13:32
13

There are some very good and complete answers here, but to answer the question in a minimalist fashion:

 (defun prev-window ()
   (interactive)
   (other-window -1))

 (define-key global-map (kbd "C-x p") 'prev-window)
bnzmnzhnz
  • 151
  • 1
  • 2
4

Just to add to @Nate, @aspirin and @Troydm's answer I find this to be a very helpful addition if you decide to bind the windmove commands to whatever key combination you choose:

(setq windmove-wrap-around t)

With the default configuration you will get an error when you get to attempt to move to a window that doesn't exist which becomes kind of annoying after a while. However when windmove-wrap-around is set then attempting to move off the bottom of the frame for example will instead select the topmost window in the frame. This may be a more intuitive behaviour for you.

Hal
  • 1,125
  • 1
  • 11
  • 11
4

M-n and M-p makes the most sense to me, since they are analogous to C-n (next-line) and C-p (previous-line):

(define-key global-map (kbd "M-p") 'previous-multiframe-window)
(define-key global-map (kbd "M-n") 'other-window)

(inspired by to this and that)

Community
  • 1
  • 1
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • I've upvoted and used this config. The problem is that `M-n` and `M-p` are already used in terminals (gdb, python, ielm, etc.) so you'll have to switch back to another method to jump out of the terminal buffer. – mihai Nov 16 '15 at 11:55
  • What are they used for in those environments? – tshepang Nov 16 '15 at 14:39
  • That is the best one I seen for the moment, it does not goes against other stuff and works like charm! Thanks! – aurelien Nov 07 '17 at 06:08
4

Base on idea from @Nate but slightly modified to support backwards cycling between windows

;; Windows Cycling
(defun windmove-up-cycle()
  (interactive)
  (condition-case nil (windmove-up)
    (error (condition-case nil (windmove-down)
         (error (condition-case nil (windmove-right) (error (condition-case nil (windmove-left) (error (windmove-up))))))))))

(defun windmove-down-cycle()
  (interactive)
  (condition-case nil (windmove-down)
    (error (condition-case nil (windmove-up)
         (error (condition-case nil (windmove-left) (error (condition-case nil (windmove-right) (error (windmove-down))))))))))

(defun windmove-right-cycle()
  (interactive)
  (condition-case nil (windmove-right)
    (error (condition-case nil (windmove-left)
         (error (condition-case nil (windmove-up) (error (condition-case nil (windmove-down) (error (windmove-right))))))))))

(defun windmove-left-cycle()
  (interactive)
  (condition-case nil (windmove-left)
    (error (condition-case nil (windmove-right)
         (error (condition-case nil (windmove-down) (error (condition-case nil (windmove-up) (error (windmove-left))))))))))

(global-set-key (kbd "C-x <up>") 'windmove-up-cycle)
(global-set-key (kbd "C-x <down>") 'windmove-down-cycle)
(global-set-key (kbd "C-x <right>") 'windmove-right-cycle)
(global-set-key (kbd "C-x <left>") 'windmove-left-cycle)
Troydm
  • 2,642
  • 3
  • 24
  • 35
3

In reference to Nate's answer, I replaced the arrow keys to use the traditional p for going up, n for going down, f for going right and b for going left. I also replaced the Ctrl with Super key as C-p, C-n, C-f and C-b are the default movement keys. This combination with M lets you jump characters and lines instead of going through just one by one after each keystroke. Thus Super key felt the best choice to keep it an easy key binding. Also, now you don't have to take your hand off the home row any more!

(global-set-key (kbd "s-p") `windmove-up)
(global-set-key (kbd "s-n") `windmove-down)
(global-set-key (kbd "s-f") `windmove-right)
(global-set-key (kbd "s-b") `windmove-left)

Hope it helps!

Indradhanush Gupta
  • 4,067
  • 10
  • 44
  • 60
2
(global-unset-key (kbd "M-j"))
(global-unset-key (kbd "M-k"))
(global-set-key (kbd "M-j") (lambda () (interactive) (other-window 1)))
(global-set-key (kbd "M-k") (lambda () (interactive) (other-window -1)))

altj and altk will cycle through your visibles buffers. Forwards and backwards, to be exact.

sjas
  • 18,644
  • 14
  • 87
  • 92
1

There is already a package that lets you switch windows by using M-. check this website. Add this to your init file:

(require 'windmove)
(windmove-default-keybindings 'meta) ;; or use 'super to use windows key instead alt
aspirin
  • 347
  • 1
  • 5
0
(global-set-key (kbd "C-x a") 'ace-swap-window)  
(global-set-key (kbd "C-x q") 'ace-select-window)

download ace-window from the melpa repo if you don't know how to do that
put this in your .emacs file if you don't have one create it 

(package-initialize)                                                                                                                                                                     

(require 'package)                                                                                                                                                                       
(add-to-list 'package-archives '("melpa" , "http://melpa.org/packages/"))                                                                                                                

(package-initialize) 

then "m-x list-packages"
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
0

The fastest method I have found for switching to the previous window is to mash a couple keys together as a "key-chord". The following lets you use your left pinky+ring fingers together to go to previous window:

(key-chord-define-global "qw" 'prev-window)
(key-chord-define-global "'y" 'other-window) ; bonus for my colemak, adjust otherwise
(key-chord-define-global ";'" 'other-window) ; probably normal

(This is possible because Emacs key chords are order independent, meaning that qw is the same as wq.)

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54