0

I want to speed up when switching between different windows - sometimes C-x-o is just too slow when I have multiple windows. Say, I have 4 windows now, and I want to switch to the upper one by C-x-up arrow, to the left by C-x-left arrow, to the bottom by C-x-down arrow, to the right by C-x-right arrow. How should I code in .emacs?

Thanks very much!

lsheng
  • 3,539
  • 10
  • 33
  • 43

2 Answers2

3

The windmove package is designed for this.

By default, you can switch windows using Shift with the arrow keys. As outlined in the linked post, simply

(when (fboundp 'windmove-default-keybindings)
  (windmove-default-keybindings))

Of course, this is fully configurable. For instance, I bind it to the arrow keys without any modifier (I already use C-p, C-n, etc. for movement) like this:

(when (locate-library "windmove")
  (global-set-key (kbd "<left>") 'windmove-left)
  (global-set-key (kbd "<right>") 'windmove-right)
  (global-set-key (kbd "<up>") 'windmove-up)
  (global-set-key (kbd "<down>") 'windmove-down))

windmove is shipped with Emacs.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • The windmove commands are all autoloaded by default nowadays, so the bindings are all you need. – phils Jan 06 '14 at 03:28
  • 1
    Also, an `if` with no *else* condition can be written as a `when`, and then you don't need the `progn` :) – phils Jan 06 '14 at 03:31
  • @phils, both good points. I have updated my own configuration, and my answer, accordingly. – ChrisGPT was on strike Jan 06 '14 at 12:17
  • The default works terrific! – lsheng Jan 09 '14 at 08:27
  • Well the later one - do I just need to copy and paste? Cuz it didn't work, I guess I need some revisions, but I'm really novice to Emacs languages.. Anyways the default settings works elegantly. Thanks very much! – lsheng Jan 09 '14 at 08:28
  • @Twinkle, you may need to `autoload` the functions, depending on your version of Emacs. I had a [slightly different version](http://stackoverflow.com/revisions/20942342/1) up originally. Try that if you've got something older than, say, 24.3. – ChrisGPT was on strike Jan 09 '14 at 13:34
  • @Chris Thanks very much I think mine has windmove loaded. I just realized that I forgot to add C-x in the front, so now it works as my original idea. Terrific! – lsheng Jan 10 '14 at 08:33
0

This is my final answer and it works elegantly! This is the first time I defined shortcuts myself in Emacs. Terrific! Thanks very much @Chris!

(when (locate-library "windmove")
    (global-set-key (kbd "C-x <left>") 'windmove-left)
    (global-set-key (kbd "C-x <right>") 'windmove-right)
    (global-set-key (kbd "C-x <up>") 'windmove-up)
    (global-set-key (kbd "C-x <down>") 'windmove-down)
  )

PS my windmove package is autoloaded.

lsheng
  • 3,539
  • 10
  • 33
  • 43