122

How do I switch between windows quickly?

I often have multiple windows open and need to switch between them. Right now, I use C-x o to get to the one I want. Is there a more efficient workflow available for this?

1

13 Answers 13

79

I obviously prefer ace-window for this. You can switch between visible windows and frames. It's in MELPA, you can install it quickly. The only thing left to do is to bind it:

(global-set-key (kbd "C-x o") 'ace-window)

Or maybe use a shorter key chord, since switching windows is a common task:

(global-set-key (kbd "M-p") 'ace-window)

Summary of ace-window

It's a drop-in replacement for other-window. Unlike the latter, you never have to call ace-window two or more times to select a window: you select a window you want in one call (possibly across multiple frames).

Besides switching windows, ace-window can do more (delete or swap windows, etc), see README.

6
  • 2
    This is interesting. I used ace-window and then saw switch-window which seems to do roughly the same thing, except that the letters are more pronounced. seems funny we have both packages. Jan 23, 2015 at 17:16
  • You're right. There are only small differences between them. This is an issue that we have to face when we have a repository with 2000 packages. I wasn't aware of switch-window until I submitted ace-window to MELPA.
    – abo-abo
    Jan 23, 2015 at 17:25
  • 1
    thanks for ace-window, it has more customization than other packages I guess. Still one might be of help in the future is a key binding to a function that would in a similar way to the smart swap action but eat up that window resulting in having a larger window at point and a smaller in size chosen window (the eaten up window). The ratio should be determined in this case as one half, one fourth, or one third may be.
    – doctorate
    Feb 16, 2016 at 17:37
  • 5
    Please consider adding a description. You just say that you like it, and you link to it. That makes this essentially a link-only answer.
    – Drew
    Feb 20, 2016 at 2:45
  • 5
    What's the benefit of ace-window? Why should I use it over the 14 other solutions proposed on this page? Oct 15, 2017 at 17:17
142

One way is to use windmove. With its default keybindings, it allows switching to the window next to the currently active one.

It's built in Emacs, so there is no need to install anything; just activate it with the following minimal setup in your init file:

(windmove-default-keybindings)

You can then switch to neighbouring windows using the following keys (where the arrow used intuitively defines the direction in which you move): S-<left>, S-<right>, S-<up>, S-<down>.


If you'd prefer using another modifier instead of shift, you can provide it as an argument to windmove-default-keybindings. For example:

(windmove-default-keybindings 'meta)

or

(windmove-default-keybindings 'control)

More info at Emacs Wiki.

9
  • 27
    This should be the accepted answer !
    – rowman
    Feb 17, 2015 at 10:26
  • OMG, why does no tutorial mention shift + arrow keys to move around window! Its so much better then ctrl + o
    – Didier A.
    Mar 7, 2018 at 2:33
  • I'd say some of the other answers here contain redundant windmove configuration. Perhaps it's worth updating the answer to mention that the function accepts the modifier key as an argument e.g. (windmove-default-keybindings 'meta) as suggested on EmacsWiki.
    – user66
    Aug 9, 2018 at 0:07
  • This creates lot of conflict with other keybinding (or with window managers). I prefer ace-window.
    – azzamsa
    Sep 22, 2018 at 3:11
  • I'm using something alike (I'm moving around with Shift-arrow), but when you have a big monitor with lots of splits, such a movement quickly becomes unwieldy. It'd be more interesting to somehow enumerate windows/splits. Like ace-window, but with less key-presses, and an obvious algorithm to immediately figure out the number you want.
    – Hi-Angel
    Oct 22, 2018 at 13:45
29

You can bind other-window to an easier-to-type key sequence.

For example, I have C-. bound to other-window, and C-, bound to go to the previous window.

(global-set-key (kbd "C-.") #'other-window)
(global-set-key (kbd "C-,") #'prev-window)

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

This is especially useful for moving multiple windows; once you hold down Control, you only have to press a single button to move to the next window. This is much easier than three keypresses per move.

9
  • 3
    +1 C-x o for other-window is one of a few solid contenders for the title of "worst default emacs shortcut". My solution (similar to zck) was to rebind other window to a (way) easier key sequence - I have it bound to C-; (and my Caps-Lock is swapped with Ctrl so all it takes is a quick tap of the pinkies). Btw, I'm totally dependent on certain features of helm and icicles (i.e., I'm not "anti-packages" or anything); I just think that a simple solution works best here.
    – iceman
    Nov 13, 2014 at 3:43
  • 2
    +1 This should be the accepted answer AND the most simple answer too (alas, no such thing yet exists on StEx). Works out of the box, scales perfectly, simple to memorize. The latter I found especially important in the Emacs jungle. After surviving there 20+ years, sometimes I find myself yearningly lurking at the vi desert... Jul 4, 2015 at 8:20
  • Simple, easy to use, easy to implement. This needs more votes. Feb 3, 2016 at 16:39
  • 2
    This is also what I do. I use M-o for other-window. Some modes already have M-o binding, notably diff-mode, ggtag-mode, and ibuffer-mode. You have to manually unbind it in each mode map. unbind-key provided by bind-key.el is very useful. Dec 6, 2016 at 3:56
  • I use M-o as well, and have M-O bound to ace-window for when I need the additional power.
    – glucas
    Sep 15, 2017 at 13:13
15

Some people may remember the editor Brief (funnily enough produced by a company called Underware).

It had a neat way to switch between visible windows. M-arrow would move the cursor into the window directly in line with the direction of the arrow key.

So, I mapped the winmove-left, winmove-right, winmove-up and winmove-down functions to M-left, M-right, M-up and M-down keys as follows in my .emacs file -

(global-set-key [M-left] 'windmove-left)          ; move to left window
(global-set-key [M-right] 'windmove-right)        ; move to right window
(global-set-key [M-up] 'windmove-up)              ; move to upper window
(global-set-key [M-down] 'windmove-down)          ; move to lower window

Now I can move with ease between windows, regardless of how cluttered I have the configuration... And it can get quite cluttered with the size of displays nowadays...

3
  • Isn't this more or less @Francesco's answer?
    – Dan
    Nov 13, 2014 at 11:16
  • I agree. I did not notice before I posted mine.
    – user2542
    Nov 13, 2014 at 11:21
  • 4
    A much shorter way to get those keybindings is (windmove-default-keybindings 'meta)!
    – Omar
    Aug 4, 2018 at 6:28
14

window-numbering.el !

I assign the hotkey ",1", ",2", ",3" to switch specific window (please note I use evil-mode)

besides, I display the window number at the mode line in PINK color, underlined.

It's a visual hint you can never ignore,

That's quickest way to switch window.

check https://github.com/redguardtoo/emacs.d/blob/master/lisp/init-evil.el and https://github.com/redguardtoo/emacs.d/blob/master/lisp/init-modeline.el for my setup.

Sylvain Benner adopted my idea in spacemacs but keybindings are different. So if you use spacemacs, you get the quickest solution out of the box.

3
  • 2
    Yup. This concept is great. I use package window-number with M-<num> to quickly switch b/w windows. Similar concept to this one
    – mike3996
    Nov 13, 2014 at 10:14
  • 1
    Note that window-numbering.el is deprecated in favor of winum, which is here. FWIW the window-numering for me didn't even work.
    – Hi-Angel
    Oct 23, 2018 at 7:58
  • I might switch to winum soon. But window-numbering still works fine on emacs24/25/26.
    – chen bin
    Oct 24, 2018 at 4:17
13

Without using any additional add-ons, you can continue to use C-x o with an optional prefix to skip over that many windows. Eg: C-u 1 C-x o will go to the next window in the order (ie, what you would get with plain C-x o), C-u 2 C-x o goes two windows over, C-u 3 C-x o goes three windows over, and so on.

An excerpt from the other-window docstring (the function called by C-x o:

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.

4
  • When you have multiple windows what order do they follow?
    – stsquad
    Nov 12, 2014 at 22:10
  • The order is the cyclic window ordering.
    – Dan
    Nov 12, 2014 at 23:11
  • 2
    hmm "The ordering is determined by a depth-first traversal of the frame's window tree, retrieving the live windows which are the leaf nodes of the tree" isn't the most helpful description for the human brain trying to guess which window is next or how many to skip. I guess this is why I use ace-window.
    – stsquad
    Nov 13, 2014 at 10:52
  • 2
    @stsquad: oh, I agree with you -- it makes my head spin, too. I put this answer up here for the sake of completeness.
    – Dan
    Nov 13, 2014 at 11:03
7

If you use Icicles then C-x o lets you select windows by name (or by cycling).

  • With no prefix arg, C-x o is the same as usual, other-window, unless the frame has only one window, in which case it is other-frame.

  • With a prefix arg, C-x o is a multi-command that lets you navigate among windows or frames by name (or by cycling). Navigating among windows uses multi-command icicle-select-window; navigating among frames uses icicle-select-frame.

Which navigation is used depends on the prefix arg:

  • Zero prefix arg (e.g. C-0): If the selected frame has multiple windows, then this is icicle-select-window, with windows in the same frame as candidates. Otherwise (single-window frame), this is icicle-select-frame.

  • C-u: If the selected frame has multiple windows, then this is icicle-select-window, with windows from all visible frames as candidates. Otherwise, this is icicle-select-frame.

  • C-u C-u: icicle-select-window, with windows from all frames as candidates (including iconified or hidden frames).

As always in Icicles, you can combine (a) completion, typing text to narrow the set of matching candidates (window or frame names, in this case), with (b) cycling (among matching candidates).

4

Consider using a key-chord for quickly jumping between windows. I find it much quicker than reaching for a modifier key(and less straining on my hands, as well).

My choice of package to use is ace-window, which lets you navigate multiple windows easier than just other-window.

(require 'key-chord)
(key-chord-define-global "xo" 'ace-window)
(key-chord-mode +1)

Obviously to use this you need key-chord and ace-window, both are available in melpa.

5
  • 2
    Isn't this more or less @abo-abo's answer?
    – Dan
    Nov 13, 2014 at 11:06
  • 1
    @Dan his doesn't use key-chord, note the lack of meta or super keys in the chord. Wether you use ace-window/something else is a bit irrelevant, the point was to give an answer that doesn't use modifier keys for so commonly used operation.
    – Zavior
    Nov 13, 2014 at 11:54
  • 1
    I saw that, but I raised the question because ace-window is doing the work as in @abo-abo's answer. It strikes me that, in this context, the specific keybinding to use would make more sense as a comment rather than an answer. Just my two cents.
    – Dan
    Nov 13, 2014 at 12:03
  • @Dan, overall the question was about quickly switching the windows, and I do not think this solution has been brought up yet. Clarified the answer a bit in regards to that.
    – Zavior
    Nov 13, 2014 at 12:19
  • @Zavior raises an important point. The question is about switching windows quickly- for which the form of the binding is very important. Regardless of the library you prefer key-chord allows binding very accessible combinations of keys. The example binding of xo is faster than C-x o (depth of one versus two). This answer could be reworded to emphasize that the answer is orthogonal to the other answers about function selection (and ace-window is just one of many choices that could be bound in the described fashion). Keep this answer as distinct!
    – ebpa
    Jan 6, 2016 at 2:51
4

Some good answers here, I also am a fan of WinMove, but I bind it to C- because org-mode uses M- for manipulating headings

(define-key global-map (kbd "C-<up>") 'windmove-up)
(define-key global-map (kbd "C-<down>") 'windmove-down)
(define-key global-map (kbd "C-<left>") 'windmove-left)
(define-key global-map (kbd "C-<right>") 'windmove-right)

Where it gets really good is if you combine it with Hydra. Following the examples on their wiki I created my own monster:

(defhydra hydra-windows ()
  "C-arrow = switch, S-arrow = size, M-arrow = move"
  ("C-<left>" windmove-left nil)
  ("C-<right>" windmove-right nil)
  ("C-<up>" windmove-up nil)
  ("C-<down>" windmove-down nil)
  ("S-<left>" hydra-move-splitter-left nil)
  ("S-<right>" hydra-move-splitter-right  nil)
  ("S-<up>" hydra-move-splitter-up nil)
  ("S-<down>" hydra-move-splitter-down nil)
  ("M-<left>" buf-move-left nil)
  ("M-<right>" buf-move-right nil)
  ("M-<up>" buf-move-up nil)
  ("M-<down>" buf-move-down nil)
  ("p" previous-buffer "prev-buf")
  ("n" next-buffer "next-buf")
  ("1" delete-other-windows "1")
  ("d" delete-window "del")
  ("k" kill-buffer "kill")
  ("s" save-buffer "save")
  ("u" (progn (winner-undo) (setq this-command 'winner-undo)) "undo")
  ("r" winner-redo "redo")
  ("b" helm-mini "helm-mini" :exit t)
  ("f" helm-find-files "helm-find" :exit t)
  ("|" (lambda () (interactive) (split-window-right) (windmove-right)))
  ("_" (lambda () (interactive) (split-window-below) (windmove-down)))
  ("q" nil "cancel")
  )

(global-set-key (kbd "M-#") 'hydra-windows/body)

This lets me change windows, resize them, move buffers from window to window, cycle buffers, kill buffers and even undo window configurations via winner-mode, and it also integrates helm. To exit the hydra, just press arrow or some other key not in the hydra. You can find the "hydra-move" functions on the hydra examples The great thing about hydra is I don't have to remember all those keybindings and its "modeful", so once you are in the hydra everything is available as a single key press.

2
  • 2
    A shorter way to get the keybindings you mention at the beginning is (windmove-default-keybindings 'control).
    – Omar
    Aug 4, 2018 at 6:30
  • Nice answer and @Omar comment is nice also.
    – NAND
    Apr 20, 2020 at 4:51
3

If you use evil-mode, you can just use a numeric prefix with C-w C-w (evil-window-next) to pick the window you want. So: 1 C-w C-w picks the first window in cyclic order, 3 C-w C-w picks the third window in cyclic order, and so forth.

1
  • This seems like it should just be combined with your other answer: it's the same thing, just with evil-specific bindings.
    – npostavs
    May 5, 2019 at 14:06
1

Ace Jump lets you navigate your pointer to all words on a screen, regardless of what window or frame they're in, so it's not so different from using Ace Window or Switch Window.

An advantage could be that while jumping windows you can also place your pointer with some precision.

I still find it a little cumbersome or distracting, and find I'm using windmove more, because it's very simple, but still use Ace Jump when I've been reading in another window or frame and want to move to a spot there.

1

As usual there are a hundred different ways to accomplish this. My preferred way is using NumberedWindows. I then set the following keybind using Super and keys 1-9.

(global-set-key (kbd "s-1") (lambda() (interactive) (window-number-select 1)))

I go into a little more detail on this exact subject with a recent post of mine which you can read on Afternoon Coder.

2
-1

You can also decide to use switch-window as a visual replacement for C-x o

3
  • 1
    What's the benefit of switch-window? Why should I use it over the 14 other solutions proposed on this page? Oct 15, 2017 at 17:20
  • How says you should ? Emacs is all about alternatives, customisation and freedom to decide.
    – csantosb
    Feb 14, 2018 at 8:35
  • 2
    That's neither here nor there. Why would someone pick this particular alternative? Why would one decide to use switch-window over all the others? Feb 14, 2018 at 19:51

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