123

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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Sibi
  • 3,603
  • 2
  • 22
  • 35
  • Possible duplicate of [Better window navigation in Emacs?](http://stackoverflow.com/q/4858958/324105) and [Is there a better way to switch between multiple windows in emacs gdb besides C-x-o?](http://stackoverflow.com/questions/9137523/is-there-a-better-way-to-switch-between-multiple-windows-in-emacs-gdb-besides-c) and [Emacs move around split windows in a specified direction?](http://stackoverflow.com/q/16607791/324105) – phils Nov 14 '14 at 12:33

13 Answers13

145

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.

François Févotte
  • 5,917
  • 1
  • 24
  • 37
  • 27
    This should be the accepted answer ! – rowman Feb 17 '15 at 10:26
  • 1
    OMG, why does no tutorial mention shift + arrow keys to move around window! Its so much better then ctrl + o – Didier A. Mar 07 '18 at 02: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. –  Aug 09 '18 at 00:07
  • This creates lot of conflict with other keybinding (or with window managers). I prefer ace-window. – azzamsa Sep 22 '18 at 03: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 '18 at 13:45
  • If you use Org-mode in emacs - check out this answer too: https://emacs.stackexchange.com/questions/22286/shiftarrow-to-change-window-does-not-work-in-org-mode – Henrik K Aug 25 '19 at 05:33
  • what does `S` stands for meta? – alper Jul 22 '20 at 03:21
  • @alper `S` stands for `Shift` – François Févotte Jul 22 '20 at 14:14
  • I didn't know I could start keybindings with `Shift` , for left I am using Ctrl-b so for example: keybinding would be `shift` + `ctrl` + `p` right? – alper Jul 22 '20 at 18:59
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.

abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • 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. – milkypostman Jan 23 '15 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 '15 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 '16 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 '16 at 02:45
  • 5
    What's the benefit of ace-window? Why should I use it over the 14 other solutions proposed on this page? – Gilles 'SO- stop being evil' Oct 15 '17 at 17:17
  • Typo? I am guessing you meant `M-o` instead of `M-p` here (especially since you explicitly recommend `M-o` in other documentation because it's not bound by default to something commonly used, `o` is memorably using the same letter as `C-x o`, and `M-p` is used extensively by various modes). – mtraceur Apr 28 '23 at 08:24
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.

zck
  • 8,984
  • 2
  • 31
  • 65
  • 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 '14 at 03: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... – Andreas Spindler Jul 04 '15 at 08:20
  • Simple, easy to use, easy to implement. This needs more votes. – stewSquared Feb 03 '16 at 16:39
  • 3
    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. – Yasushi Shoji Dec 06 '16 at 03: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 '17 at 13:13
  • This conflicts with Org-mode key bindings, I haven't been able to find a workaround for how to get this to work with org-mode yet. – Rahul Nov 29 '17 at 20:06
  • @Rahul you can bind other keys to these methods, if you prefer. Nothing about this answer requires you to use `C-.`. – zck Nov 30 '17 at 03:48
  • @zck I agree, the comment was just to warn if someone tries exactly this and it doesn't work (as in my case). So the person knows the reason. – Rahul Nov 30 '17 at 17:27
  • Thanks, `(other-window -1)` was exactly what I needed. For those familiar with iTerm2 on Mac, I recommend trying `(global-set-key (kbd "s-[") 'prev-window) (global-set-key (kbd "s-]") 'other-window)` so you can switch with `⌘-[` and `⌘-]` – hraban Apr 08 '21 at 09:21
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...

user2542
  • 221
  • 1
  • 3
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.

chen bin
  • 4,781
  • 18
  • 36
  • 2
    Yup. This concept is great. I use package `window-number` with `M-` to quickly switch b/w windows. Similar concept to this one – mike3996 Nov 13 '14 at 10:14
  • 1
    Note that [window-numbering.el is deprecated in favor of winum](https://github.com/nschum/window-numbering.el/issues/16), which is [here](https://github.com/deb0ch/emacs-winum). FWIW the window-numering for me didn't even work. – Hi-Angel Oct 23 '18 at 07:58
  • I might switch to winum soon. But window-numbering still works fine on emacs24/25/26. – chen bin Oct 24 '18 at 04: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.

Dan
  • 32,584
  • 6
  • 98
  • 168
  • When you have multiple windows what order do they follow? – stsquad Nov 12 '14 at 22:10
  • The order is the [cyclic window ordering](http://www.gnu.org/software/emacs/manual/html_node/elisp/Cyclic-Window-Ordering.html). – Dan Nov 12 '14 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 '14 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 '14 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).

smonff
  • 1,575
  • 1
  • 15
  • 20
Drew
  • 75,699
  • 9
  • 109
  • 225
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.

Philip Daniels
  • 221
  • 1
  • 5
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.

Zavior
  • 591
  • 1
  • 4
  • 7
  • 2
    Isn't this more or less @abo-abo's answer? – Dan Nov 13 '14 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 '14 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 '14 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 '14 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 06 '16 at 02: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.

Dan
  • 32,584
  • 6
  • 98
  • 168
  • This seems like it should just be combined with [your other answer](https://emacs.stackexchange.com/a/3461/5296): it's the same thing, just with evil-specific bindings. – npostavs May 05 '19 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.

Mallory-Erik
  • 265
  • 1
  • 7
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.

Travis
  • 41
  • 3
-1

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

csantosb
  • 1,045
  • 6
  • 15