Moving Between Emacs Windows

As I mentioned in my winner-mode post, I recently configured Emacs to start with a frame-width of 162 columns. I’ve also added a call to split-window-right in my init.el to get the two columns of windows that I prefer. This introduces the problem of how to navigate between windows easily. If there’s only two windows, the usual 【Ctrl+x o】 works nicely but it quickly becomes cumbersome when there are 3 or more windows in the selected frame.

It turns out that there is a really nice solution to this although it’s a bit hard to find. If you add

(windmove-default-keybindings)

to your .emacs or init.el file, you can move between windows with the 【Shift+】/ 【Shift+】/ 【Shift+】 / 【Shift+】 key sequences. You can change the modifier by specifying a different one as the argument to windmove-default-keybindings.

If, like me, you’re a heavy org-mode user, you’ll find that these key bindings won’t work in org-mode buffers because org-mode takes them over. Happily you can solve this by adding the line

(setq org-replace-disputed-keys t)

to your .emacs or init.el. This will cause org-mode to replace the shift-arrow keys with others for its operations. You can also tell org-mode to replace the windmove keys only when the point is not at a place when org-mode would want to use them as explained in the Org Mode Manual.

All this may not sound like much but they are a huge win for me. I can have several windows open in a frame and still move among them easily. Another demonstration of the power and configurability of Emacs.

Update: windmove-default-bindingswindmove-default-keybindings

This entry was posted in General and tagged . Bookmark the permalink.

10 Responses to Moving Between Emacs Windows

  1. I use windmove too and it’s great. I also find the plugin ‘switch-window’ quite handy when dealing with ‘a lot’ of windows: http://tapoueh.org/emacs/switch-window.html

  2. Alan Clucas says:

    I had to use (windmove-default-keybindings). Thanks for this, very useful.

  3. I had to use:
    (when (fboundp ‘windmove-default-keybindings)
    (windmove-default-keybindings))

  4. Phil says:

    WindMove is wonderful, and Trey Jackson’s FrameMove library makes it even better, by enabling those same bindings to move to the next frame in the given direction after you’ve reached the edge of the frame you were in. It’s seamless and highly recommended (unless you only ever run Emacs in a terminal, of course).

    I didn’t realise org-mode provided that compatibility option. I resolved that issue by adding the windmove bindings to my custom minor-mode keymap (which is an extremely useful technique for preventing other modes from clobbering bindings that you want to always keep available).

  5. Cezar says:

    The problem I have with windmove is that you need to move your hand over the cursor keys. So I like the switch-window plugin much better for this.

    • Phil says:

      You don’t need to use the default windmove bindings. Instead of calling windmove-default-keybindings, you can simply bind the functions (windmove-left|right|up|down) to your preferred keys (the functions are all autoloaded, so that’s literally all you need to do).

  6. Justin says:

    On a slightly tangential note, do people really use open-line (which is bound to C-o by default)? I recently rebound other-window to C-o because I just use return. But perhaps I haven’t been absorbing the emacs ways for long enough.

  7. Chris says:

    I have a package generalizing windmove, called win-switch.el (on Marmalade or EmacsWiki), that you might like. It defines a dynamic, transient mode that makes navigation among windows (and frames) really easy and efficient. It’s highly customizable too. Having used it for a while, I find it hard to live without.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>