Emacs: Manage Split Windows

, , …,

This page is a tutorial on managing split windows in emacs.

First, know the basics:

PurposeCommand NameKey
split top/bottomsplit-window-belowCtrl+x 2
split left/rightsplit-window-rightCtrl+x 3
unsplit alldelete-other-windowsCtrl+x 1
remove current panedelete-windowCtrl+x 0
cycle cursor to next paneother-windowCtrl+x o
wildly split emacs screen
Wildly split emacs screen. From top to bottom: calendar, Unicode, shell, hex editing mode, HTML mode, file management, calculator.

Note: in emacs, a “window” is technically called a “frame”, while a pane in a split window is called “window”.

Adjusting Split Pane Size

You can adjust the split pane size by dragging on the mode line bar in the middle. For windows split left/right, drag the status bar at the split point.

Or, you can use keys. Here's the commands:

CommandKeyPurpose
enlarge-windowCtrl+x ^increase height
shrink-windowdecrease height
enlarge-window-horizontallyCtrl+x }increase width
shrink-window-horizontallyCtrl+x {decrease width
shrink-window-if-larger-than-bufferCtrl+x -shrink a window to fit its content.
balance-windowsCtrl+x + make all windows same width/height

Most of these commands also take a argument. You can call universal-argumentCtrl+u】 first then type a number. If you use these commands often, you probably want to give them a easier key. 〔☛ Emacs: How to Define Keys

Save Split-window Config for Current Session

winner-mode

winner-mode lets you revert to a previous pane configuration. (in emacs 23.2 () or later)

Example use:

  1. Call winner-mode to turn it on.
  2. Call split-window-vertically.
  3. Call split-window-horizontally.
  4. Now, remove all split panes by calling delete-other-windows.
  5. Now, you can go back to previous split pane config by calling winner-undoCtrl+c 】. There's also winner-redoCtrl+c 】.

If you want this functionality always, put in your emacs init file:

;; ability to revert split pane config. Call winner-undo 【Ctrl+c ←】 and winner-redo 【Ctrl+c →】
(winner-mode 1) ; in GNU emacs 23.2

Save Split-Window Config

You can save split-window config to register, like this:

Note: this doesn't work when you restart emacs, even if you have desktop-save-mode on.

Save Split-window Config Across Sessions

Use revive.el by HIROSE Yuuji. It's in emacs MELPA package repository. 〔☛ A Guide on Emacs 24 Package System

First, install it. Then, call save-current-configuration. It'll generate a file at ~/.revive.el.

To restore, call resume.

Swap Buffers Among Panes

If you want to swap buffers among your current split panes, install buffer-move.el by Lucas Bonnet. It's in emacs package repository MELPA 〔☛ A Guide on Emacs 24 Package System〕.

(thx to Mark Hepburn and John D Cook)

Flow Buffer Side-by-Side

See: Emacs: You Have Widescreen? Have Screen Flow Side by Side

Move Cursor to Different Panes by Arrow

By default, you can move cursor to the next pane by calling other-windowCtrl+x o】. The default key is hard to use. Also, there's no key to move cursor to previous pane.

You can put the following in your emacs init file. It'll let you move cursor by {【⇧ Shift+】, 【⇧ Shift+】, 【⇧ Shift+】, 【⇧ Shift+】}.

However, you'll lose shift select.

;; use Shift+arrow_keys to move cursor around split panes
(windmove-default-keybindings)

;; when cursor is on edge, move to the other side, as in a toroidal space
(setq windmove-wrap-around t )

Move Cursor to Previous Pane

alternatively, you can put the following in your emacs init file:

(defun move-cursor-next-pane ()
  "Move cursor to the next pane."
  (interactive)
  (other-window 1))

(defun move-cursor-previous-pane ()
  "Move cursor to the previous pane."
  (interactive)
  (other-window -1))

You can give them easy keys, such as {【Ctrl+3】, 【Ctrl+4】}. 〔☛ Emacs: How to Define Keys

blog comments powered by Disqus