16

Given split windows like this:

|------|-------|
|1     |2      |
|      |       |
|------|-------|
|3     |4      |
|      |       |
|------|-------|

There are two options to collaps the split windows:

  • delete-other-windows (Ctrl-x 1)
  • delete-window (Ctrl-x 0)

Question: is there a way to control how they collapse?


For example, how can I collapse like this:

|------|-------|
|1             |
|              |
|------|-------|
|3     |4      |
|      |       |
|------|-------|

, this:

|------|-------|
|2             |
|              |
|------|-------|
|3     |4      |
|      |       |
|------|-------|

, this:

|------|-------|
|1     |2      |
|      |       |
|      |-------|
|      |4      |
|      |       |
|------|-------|

, or this:

|------|-------|
|3     |2      |
|      |       |
|      |-------|
|      |4      |
|      |       |
|------|-------|

?

Beginner
  • 2,661
  • 3
  • 17
  • 25

3 Answers3

13

Try ace-window.

If you bind it like this:

(global-set-key "M-p" 'ace-window)

You can switch to window 2 by pressing M-p 2. Also, you can delete window 2 by pressing M-p x2: this will bring you into your first example. Other examples follow by just changing the final key.

There are other modifiers present:

  • Use x to delete
  • Use m to swap
  • Use v to split vertically
  • Use b to split horizontally
  • Use i to delete other windows than the selected one
  • Use o to delete other windows than the current one
  • Use n to switch to the last window

The last two modifiers actually require no further input, while the others always prompt you for a window to act upon.

The modifiers are customizable via aw-dispatch-alist. The window keys (which are 1/2/3/4... by default) are customizable via aw-keys.

Here's the setting that I'm using:

(setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
abo-abo
  • 13,943
  • 1
  • 29
  • 43
10

The function delete-window has an optional argument WINDOW.

The function window-in-direction has a mandatory argument of DIRECTION and optional arguments for WINDOW IGNORE SIGN WRAP MIN. To learn more about these arguments by reading the doc-string, a user may type M-x describe-function RET window-in-direction RET.

For example, the following four functions could be used to control the direction a window is deleted. A user may wish to assign keyboard shortcuts.

(defun delete-window-above ()
"Delete window in direction 'above."
(interactive)
  (delete-window (window-in-direction 'above)))

(defun delete-window-below ()
"Delete window in direction 'below."
(interactive)
  (delete-window (window-in-direction 'below)))

(defun delete-window-left ()
"Delete window in direction 'left."
(interactive)
  (delete-window (window-in-direction 'left)))

(defun delete-window-right ()
"Delete window in direction 'right."
(interactive)
  (delete-window (window-in-direction 'right)))
lawlist
  • 18,826
  • 5
  • 37
  • 118
1

Just focus on the window you'd want to delete and C-x 0

I know its a dated post but might be useful to someone.

Yakaas
  • 19
  • 3