Emacs: Manage Split Windows

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

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's manuals, a “window” is called a “frame”. 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 〔➤ Emacs 23.2 Features (released 2010-05)〕)

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:

;; feature for 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

In Emacs 24.4, if you restart emacs, previous session's windows and split-panes are automatically restored. This is done by desktop-save-mode. 〔➤ Emacs: Save/Restore Opened Files, Windows Configuration: desktop-mode〕 〔➤ Emacs 24.4 New Features (released 2014-10)

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

Emacs Tip: Full-Screen vs Multiple Window

For best efficiency, i recommend not to rely on split-panes as a workflow. Instead of one full-screen window with multiple split panes, i recommend 2 or 3 windows, and split & unsplit frequently as necessary.

The only commands on this page you need are:

See:

Thanks to Mark Hepburn and John D Cook.

Like it? Buy Xah Emacs Tutorial.
blog comments powered by Disqus