When you have Emacs split into multiple windows, how do you move your cursor between windows? How do you move the windows around?
Update (27 Jan 2015): You may want to skip the post below and use ace-window. I plan to use it rather than my solution below. Also, see Sacha Chua’s video on window management, including using ace-window.
Moving the cursor between windows
You can use C-x o
to move the cursor to the “other” window. That works great when you only have two windows: C-x o
toggles between them.
When you have more windows, C-x o
moves to the next window in top-to-bottom, left-to-right order. So if you have five windows, for example, you have to go to the “other” window up to four times to move to each of the windows.
The cursor is in the A window. Repeatedly executing C-x o
will move the cursor to C, B, and D.
There are more direct commands:
windmove-up
windmove-down
windmove-left
windmove-right
You might map these to Control or Alt plus the arrow keys to have a memorable key sequence to navigate windows. However, such keybindings may conflict with existing keybindings, particularly in org-mode
.
Moving windows
You can move windows around with the buffer-move. (Technically the windows stay fixed and the buffer contents moves around.)
Super and Hyper keys
Since the various Control arrow keys and Alt arrow keys are spoken for in my configuration, I thought I’d create Super and Hyper keys so that Super-up would map to windmove-up
etc.
Several articles recommend mapping the Windows key to Super and the Menu key to Hyper on Windows. The former is a poor choice because so many of the Windows key combinations are already used by the OS. I did get the latter to work by adding the following to the branch of my init.el
that runs on Windows.
(setq w32-pass-apps-to-system nil w32-apps-modifier 'hyper) (global-set-key (kbd "<H-up>") 'windmove-up) (global-set-key (kbd "<H-down>") 'windmove-down) (global-set-key (kbd "<H-left>") 'windmove-left) (global-set-key (kbd "<H-right>") 'windmove-right)
I haven’t figured out yet how to make Super or Hyper keys work on Linux. Suggestions for setting up Super and Hyper on Linux or Windows would be much appreciated.
I’m sure you mean “H-up” to be surrounded by angled brackets. So, “<:” should be “<” instead.
> Super and Hyper keys
Why not just use left/up/right/down arrow keys?
In my opinion they’re too far away off of the “home row” on a keyboard to be useful and convenient for code navigation – so I use Ctrl+N/P/F/B to navigate code, and arrow keys to change windows.
You need
ace-window
(get it from MELPA). Then map it to C-x o and your window manipulation problems are solved. With the universal argument, it will swap window contents. With two universal arguments it will delete a window.I’ve tried all the solutions you talk about and others and believe me,
ace-window
is what you want.C-x o is a bad default, switching windows is just too common an operation.
It’s non-standard but I’ve mapped C-; to previous-multframe-window and C-‘ to next-multiframe-window.
I’d be curious to hear others solutions.
Note that modern emacs also supports indivdual frames, in addition to windows. An emacs window is a subdivision of frame; just as “C-x 2” will create a new window in the current frame, “C-x 5 2” will create a brand new frame.
In my emacs setup, I have assigned other-window to F5 and other-frame to F6. I find that it provides a good conceptual split to be able to decide to move inside or between frames.
Obviously, frames are only relevant in a windowing system.
@jcs: You’re right. ace-window is the way to go. Thanks.
VJ commented that C-x o isn’t the best binding for
ace-window
and I agree. Before 24.4 I had it bound to <f11>, which worked really well but 24.4 grabbed that binding for full screen so I dropped back to C-x 0. Some function key or other easy sequence is probably the way to go.ace-window
solves so many problems that the key sequence hardly matters.> make Super or Hyper keys work on Linux
I was digging this last week. try ‘setxkbmap -option altwin:hyper_win’ or ‘setxkbmap -option altwin:alt_super_win’. The first one would map both LWIN and RWIN to Hyper, and the latter would map LWIN and MENU to Super, RWIN to Alt_R. Refer /usr/share/X11/xkb/symbols/altwin for details.
But I failed to map LWIN to Super, RWIN to Hyper. Tried to modify file /usr/share/X11/xkb/symbols/altwin, but setxkbmap won’t use it. It seems it reads options from some other places. Anyone helps?
BTW: maybe your desktop environment provides some configuration option on top of setxkbmap or xmodmap
Here is what I use under X11 to set up advanced modifier keys. I save it to a file and load it with a command like: “xmodmap “. It uses direct key codes, which I find to be most reliable but also means you need to adjust for your specific keyboard and language (use ‘xev’ to discover precise codes). Also note that some window managers like to reserve certain keybindings for it self (for instance ALT or Menu key combinations), some can be disabled but not necessarily all. Exclamation point starts a comment.
!Modifier keycodes, caps lock as super, menu as hyper
keycode 115 = Alt_L
keycode 64 = Meta_L
keycode 108 = Meta_R
keycode 66 = Super_L
keycode 135 = Hyper_L
!Modifier layout
clear lock
clear mod1
clear mod2
clear mod3
clear mod4
clear mod5
add mod1 = Alt_L Alt_R
add mod2 = Num_Lock
add mod3 = Super_L
add mod4 = Meta_L Meta_R
add mod5 = Hyper_L
Oh my goodness, ace-window is so much better. Thanks for the heads-up, jcs and Dr. Cook.