, , , Posted on 22 April 2025 by

Weekly Emacs tip #19: Emacs window management — easily moving between Emacs windows with windmove

Two tips ago, I mentioned the built-in windmove package as one way to make moving between Emacs windows easier. Both the awkwardness of the C-x o shortcut for the other-window command and the fact that it isn’t always clear which of the visible windows actually is the “other window”, call for a more intuitive way to move the focus between windows, which is what windmove provides.

The Emacs manual clearly states what the windmove package is about (emphasis mine):

The Windmove package defines commands for moving directionally between neighboring windows in a frame. M-x windmove-right selects the window immediately to the right of the currently selected one, and similarly for the left, up, and down counterparts.

The one thing about windmove that doesn’t work well in a modern-day computing environment is the fact that by default the windmove functions are bound to shift + arrow keys. Most people are used to selecting text that way, and in Emacs Org mode, these keys have different functionality as well. That’s why I ended up setting the variable windmove-default-keybindings to super ctrl. The super key is what most people know as the Windows key on the keyboard. So pressing super ctrl up will move the focus to the window above the current one.

Emacs_3_windows_windmove_1_small.png

Figure 1: Three Emacs windows before using windmove-swap-states-down.

Another feature provided by windmove is the ability to move the buffer (i.e. the window contents) in the current window to an adjacent window. This is what the windmove-swap-states-up and its sibling functions do. To illustrate this, have a look at the screenshot above (Figure 1). The top-right window with the *Help* buffer for the windmove-swap-states-up function has the focus. By invoking windmove-swap-states-down, the window is moved down and the Org mode buffer with this text is moved to where the *Help* buffer used to be (effectively swapping the contents of the two windows), as shown in the screenshot below (Figure 2). Note that the focus stays in the *Help* buffer, so it effectively moves along with the swap.

Emacs_3_windows_windmove_2_small.png

Figure 2: Three Emacs windows after using windmove-swap-states-down. The *Help* buffer and the Org mode buffer have changed places.

Here is the use-package block from my ~/.emacs config that accomplishes the above.

;;;;;;;;;;;;;;;;;;;;
;; Windmove is a way to simplify moving between windows.
(use-package windmove
  :config
  ;; By default you can use shift-up, shift-dn, etc. to move between
  ;; windows. This changes the default modifier keys to use
  ;; Ctrl+Super+arrow_keys to move cursor around split panes
  (windmove-default-keybindings '(super ctrl))
  ;; When pressing Alt as well, move the buffer to the window in the
  ;; given direction.
  (windmove-swap-states-default-keybindings '(super ctrl meta))
  :custom
  ;; When encountering an edge, move to the other side, as in a
  ;; toroidal space
  (windmove-wrap-around t)
  )

The :custom section sets the windmove-wrap-around variable to true. This does what you would expect from a variable with such a name: when the currently focused window is at the top and you invoke windmove-up, the window is moved to the bottom of the frame. And similarly for the left-right direction, as if the windows exist on a large donut.

Karthink’s post on Emacs windows management also has a section on windmove: have a look, it contains informative drawings of windmove‘s behaviour that, perhaps, explain it better than my attempt here.

No Comments

Let us keep you updated!

Once a month we’ll send you an overview of our newest articles. No spam, we promise.

Thank you for signing up! We've send you an confirmation mail.