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?
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.
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.
Commented
Jan 23, 2015 at 17:16
switch-window
until I submitted ace-window
to MELPA.
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.
Commented
Feb 16, 2016 at 17:37
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.
(windmove-default-keybindings 'meta)
as suggested on EmacsWiki.
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.
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.
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.
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.
Commented
Dec 6, 2016 at 3:56
M-o
as well, and have M-O
bound to ace-window
for when I need the additional power.
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...
(windmove-default-keybindings 'meta)
!
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.
window-number
with M-<num>
to quickly switch b/w windows. Similar concept to this one
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).
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.
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.
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!
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.
(windmove-default-keybindings 'control)
.
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.
Windows are given by window-list
. Use select-window
to select a
window. For example, assuming there are at least 3 windows, use
`M-:` to run the following:
;; Select the 3rd window in the `window-list'
(select-window (nth 2 (window-list)))
It's fundamentally as simple as that :)
Obviously, raw calls to window-list
and select-window
don't make
for a great user experience. We probably want the following
properties (at least):
The window-list
function returns a list of windows in some
unspecified order (as of this writing). It would be nice if the list
had the same order every time we referenced it.
Use the WINDOW parameter to start the list at a specific window.
Here, we use window-at
to make the list always relative to the top
left window.
;; Create a window list ordered from the top left window
(window-list nil nil (window-at 1 1))
This allows us to create direct key-bindings, assuming the layout is always the same. For example, if you always do C-x 3 C-x 2 M-x other-window M-x other-window C-x 2
to get 4 windows then the following jump directly to each window.
;; Select the 1st window counter clock-wise from the top left
(global-set-key (kbd "C-x w 1") '(lambda () (interactive)
(select-window (nth 0 (window-list nil nil (window-at 1 1))))))
;; Select the 2nd window counter clock-wise from the top left
(global-set-key (kbd "C-x w 2") '(lambda () (interactive)
(select-window (nth 1 (window-list nil nil (window-at 1 1))))))
;; Select the 3rd window counter clock-wise from the top left
(global-set-key (kbd "C-x w 3") '(lambda () (interactive)
(select-window (nth 2 (window-list nil nil (window-at 1 1))))))
;; Select the 4th window counter clock-wise from the top left
(global-set-key (kbd "C-x w 4") '(lambda () (interactive)
(select-window (nth 3 (window-list nil nil (window-at 1 1))))))
Maybe you use several layouts. We can use interactive
to make the command callable with M-x
. When the prompt starts with an n
, it expects a number:
(defun my-select-window (n)
"Select window N from the list of windows ordered
counter-clockwise from the top left starting with 0."
(interactive "nSelect window: ")
(select-window (nth n (window-list nil nil (window-at 1 1)))))
This isn't the most user friendly. However, it's version 2.0 of our Minimal Viable Product. Maybe it's good enough.
Lists in Emacs Lisp are indexed starting at 0. The first window
listed by window-list
is (nth 0 (window-list))
. The second window
is (nth 1 (window-list))
. And so on.
Using window-list
directly requires knowing that windows are indexed
starting with 0 and that they proceed counter-clockwise from the top
left.
We can give users a visual cue about which window is which by creating a buffer over each window that shows the corresponding number. This function does that.
(defun my-select-window--display-number-buffer (win num)
"Create a new buffer in WIN displaying NUM."
(let ((buf (get-buffer-create (format "*%s*" num))))
(with-current-buffer buf
(insert (number-to-string num)))
(set-window-buffer win buf)
buf))
You can try it out like this:
(my-select-window--display-number-buffer
(select-window (nth 0 (window-list))) ; get the 0th window
0 ; display 0 in a buffer there
)
Unfortunately for us, the interactive
function is the first thing
called in a function. This prevents us from displaying the number
buffers before the prompt is given.
We can work around this by using read-number
(which is what
interactive
uses behind the scenes).
(defun my-select-window--ordered-window-list ()
"Return `window-list' ordered according to top left window."
(window-list nil nil (window-at 1 1)))
(defun my-select-window--display-number-buffer (win num)
"Create a new buffer in WIN displaying NUM."
(let ((buf (get-buffer-create (format "*%s*" num))))
(with-current-buffer buf
(insert (number-to-string num)))
(set-window-buffer win buf)
buf))
(defun my-select-window (&optional n)
"Select Nth window from the list of windows ordered
counter-clockwise from the top left starting with 0."
(interactive)
(let ((num 1)
overlay-buffers)
;; display visual cues
(dolist (win (my-select-window--ordered-window-list))
(push (my-select-window--display-number win num) overlay-buffers)
(setq num (1+ num)))
;; prompt user for selection
(select-window
(nth
(1- (or n (read-number "Select window: "))) ; correct index
(my-select-window--ordered-window-list)))
;; remove visual cues
(mapc 'kill-buffer overlay-buffers)))
Note: An expression like (or n 2)
says, "If n exists, use it, otherwise, use 2". This is because or
stops and returns its first non-nil argument.
This is a minimal, yet surprisingly useful way to switch windows. Some features you might also want would be:
C-g
)other-window
when there are only two windowsThe following does these:
(defun my-select-window--ordered-window-list ()
"Return `window-list' ordered according to top left window."
(window-list nil nil (window-at 1 1)))
(defun my-select-window--display-number (win num)
"Create a new buffer in WIN displaying NUM."
(let ((buf (get-buffer-create (format " *%s*" num)))) ; space intentional, see documentation
(with-current-buffer buf
(insert (concat "\n\n " (number-to-string num)))
(text-scale-increase 5))
(set-window-buffer win buf)
buf))
(defun my-select-window-1 (n)
"Select window N from the list of windows ordered
counter-clockwise from the top left starting with 1."
(cond ((< n (length (window-list)))
(select-window (nth n (my-select-window--ordered-window-list))))))
(defun my-select-window (&optional n display-numbers)
"Select Nth window numbered 1 through 9.
Windows are numbered starting at 1 and increase
counter-clockwise.
Show window numbers when DISPLAY-NUMBERS t (default). If N is
given, window numbers are not displayed."
(interactive)
(let ((display-numbers (if n nil (or display-numbers t)))
(num 1)
(n (if n (1- n))) ; convert base-1 to base-0
overlay-buffers)
(if (< (length (window-list)) 3)
(other-window 1)
(unwind-protect
(progn
(if display-numbers
(dolist (win (my-select-window--ordered-window-list))
(push (my-select-window--display-number win num) overlay-buffers)
(setq num (1+ num))))
;; read event to avoid needing to press return
(while (not n)
(let ((input (read-event "Select window: " nil 5)))
(unless (symbolp input)
(if (and (<= 47 input) (>= 57 input)) ; 1 to 9 in ASCII
;; convert ASCII to corresponding number and
;; convert to base-0
(setq n (- (- input 48) 1)))))))
(if display-numbers
(mapc 'kill-buffer overlay-buffers)))
(my-select-window-1 n))))
You can then map that C-x o
:
(global-set-key (kbd "C-x o") 'my-select-window)
Since we made the function callable from Lisp, we can create keys for each window:
(global-set-key (kbd "C-x w 1") '(lambda () (interactive) (my-select-window 1)))
PS: To give credit where due, this is basically how switch-window.el
works. This post is based off commit 4a19d5b8 with my own modifications.
PPS: In case you don't know, use Control+h
f
(C-h f
) to see the documentation for a function and Control+h
v
(C-h v
) for variable documentation.
Happy hacking! :)
You can also decide to use switch-window as a visual replacement for C-x o
switch-window
over all the others?
Commented
Feb 14, 2018 at 19:51