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?
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?
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.
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)
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.
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.
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...
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.
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.
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).
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.
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.
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.
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.
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.
You can also decide to use switch-window as a visual replacement for C-x o