Skip to content

Latest commit

 

History

History
162 lines (107 loc) · 6.68 KB

Emacs-Tips-05.org

File metadata and controls

162 lines (107 loc) · 6.68 KB

How to Create and Manage Multiple Windows

Windows and Frames

  • A “window” is a region within an Emacs frame that shows a particular buffer
  • A “frame” is an Emacs program window at the level of your OS or desktop environment which can hold multiple windows
  • Multiple windows can show the same buffer, but with different scroll, selection, etc

Check out the Emacs manual entry for Multiple Windows

Basic Window Operations

Each item lists the default Emacs binding followed by the evil-mode binding. Note that many of the evil-mode bindings also allow you to use Ctrl with the second key in the sequence!

CommandKeyDescription
delete-windowC-x 0Close the current window
delete-other-windowsC-x 1Close all other windows
split-window-belowC-x 2Split the current window horizonally
split-window-rightC-x 3Split the current window vertically
shrink-window-horizontallyC-x {Make the window smaller horizontally
enlarge-window-horizontallyC-x }Make the window bigger horizontally
shrink-windowNone!Shrink the window vertically
shrink-window-if-larger-than-bufferC-x -Shrink the window vertically to buffer
balance-windowsC-x +Balance the sizes of all windows

TIP: You can use C-u (universal-argument) and a numeric prefix before running the shrink and enlarge commands to dictate the mount by which the window is resized.

evil-mode alternatives

CommandKeyDescription
evil-window-deleteC-w C-cClose the current window
delete-other-windowsC-w C-oClose all other windows
evil-window-splitC-w C-sSplit the current window horizontally
evil-window-vsplitC-w C-vSplit the current window vertically
evil-window-set-widthC-w (pipe)Use numeric prefix to set window width
evil-window-set-heightC-w _Use numeric prefix to set window height
balance-windowsC-w =Balance the sizes of all windows

TIP: You can use a numeric argument before running evil-window-set-width and evil-window-set-height to specify the desired size of the window.

“Other window” operations

CommandKeysDescription
other-windowC-x oSelect the next visible window
find-file-other-windowC-x 4 fOpen a file in another window
dired-other-windowC-x 4 dOpen Dired in another window
dired-jump-other-windowC-x 4 jOpen Dired in another window at location of file
scroll-other-windowM-pgdnScroll the other window down without focusing it
scroll-other-window-downM-pgupScroll the other window up without focusing it

evil-mode alternatives

CommandKeysDescription
evil-window-nextC-w C-wSelect the next visible window
evil-window-prevC-w WSelect the previous visible window
ffap-other-windowC-w C-fOpen a file in another window

More other-window commands

Learn about more other-window commands:

  • Check out the C-x 4 prefix with which-key!
  • Also, use counsel-M-x and search for any commands with other-window in the name!

Defaulting to vertical splits

You can default to vertical splits for “other windows” with the following config:

(setq split-height-threshold nil)
(setq split-width-threshold 0)

More information about controlling how buffers are displayed in the Emacs manual.

Windmove for moving between windows

Windmove comes with Emacs, but is missing some features in Emacs 26.

  • windmove-up/down/left/right - Focus the window next to the current in the specified direction
  • windmove-swap-states-up/down/left/right - “Move” the current buffer to the window in the specified direction

evil-mode equivalents

evil-mode provides its own functions for moving between windows:

  • evil-window-left - C-w h
  • evil-window-right - C-w l
  • evil-window-up - C-w k
  • evil-window-down - C-w j

buffer-move or moving buffers between windows

Use buffer-move for a more general solution:

  • buf-move: Turn on a mode where you can move the current buffer around with arrow keys, any other key finishes it
  • buf-move-left
  • buf-move-right
  • buf-move-up
  • buf-move-down
(use-package buffer-move)

winner-mode

winner-mode provides useful functions for undoing and redoing window configurations:

  • winner-undo (C-c left or C-w u bound below)
  • winner-redo (C-c right or C-w U bound below)
(use-package winner-mode
  :ensure nil
  :bind (:map evil-window-map
         ("u" . winner-undo)
         ("U" . winner-redo))
  :config
  (winner-mode))

Packages for moving between windows

ace-window

ace-window makes it easy to jump between visible windows in your Emacs frame, just run the ace-window command and press the number displayed in the upper left corner of the window you want to switch to. It also enables you to swap, delete, and move windows using similar functionality.

(use-package ace-window)

Tip from Cedrif Daf: Set aw-keys to home-row keys for more convenience:

(setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))

winum-mode

This mode shows numbers in your windows’ mode lines to tell you what keys you can press after using the key binding C-x w. Check out the winum-mode page for more useful tips!

(use-package winum
  :config
  (winum-mode))