2008-12-19

easy switching between visible buffers

It's common to split the emacs frame1, so we can see multiple buffers at the same time. For maximum efficiency, we'd like to use the keyboard (instead of the mouse) to switch the focus from one buffer to the next. The 'traditional' way is to use C-x o for that ('other-window'); it will take you to the next window (remember, 'window' may be what you think1); C-x o also accepts prefix arguments to go to the n-th next window.

However, its much easier to switch between visible buffers using the windmove-package (which is part of emacs):

(require 'windmove)
(windmove-default-keybindings 'meta)

After this, you can move the focus to some other window using Alt+arrow-key; so Alt-up (M-up) will go to the window above the current one, Alt-Left (M-left) goes to the left, and so on. That's a lot more intuitive C-x o multiple times to find the right window.

Note, by default, windmove uses the Shift-key instead of the Meta-key; but using the shift key conflicts with e.g. cua-mode. Because of that, I prefer the meta key; this is what the (windmove-default-keybindings 'meta) line is for. You can of course use another key if you're already using the Meta/Alt key for something else.

More recently, I have started to use the Windows-key, because org-mode already uses of meta + arrow keys for other things. Under X, you can usually find the Windows key as super key, so, you would use:

(require 'windmove)
(windmove-default-keybindings 'super)

That might not work in all places though - not on the console and, ironically, not on Windows.

Footnotes:

1 frame and a buffer are typical emacs-speak, see emacs-terminology for an overview)

1 comment:

Anonymous said...

I have the windmove commands bound to M-P, M-N, M-B and M-F (with both meta and shift). It's reasonably mnemonic, it doesn't require moving your hand to the arrow keys, and it works in all window systems.