Romário
Romário

Reputation: 1966

Is there something similar to `delete-other-windows` (`C-x 1`), but that allows me to restore my window arrangement

Suppose I have a buffer that's a bit too cramped in its current window, so I decide I want to briefly give it more space, then go back to what I was doing. Is it possible for me to just temporarily hide other windows instead of deleting them like C-x 1 does?

Upvotes: 5

Views: 600

Answers (3)

phils
phils

Reputation: 73314

Add this to your init file:

(winner-mode 1)

Then you can use C-x1 as normal, and afterwards use C-c<left> to call winner-undo to restore the previous window configuration.

You can use C-c<left> repeatedly to step back through previous configurations, and C-c<right> to jump back to the latest one again.

n.b. This is a general-purpose feature that is incredibly useful in all kinds of situations besides this specific example. I highly recommend getting familiar with it.

Upvotes: 4

NickD
NickD

Reputation: 6422

You can save the window configuration to a register, go to a single window and when you are done, restore the window configuration from the register:

  • Save the window configuration to register z: C-xrwz
  • Go to a single window: C-x1 and do what you need to do (but do not disturb register z!)
  • Restore the window configuration from register z: C-xrjz

Do (info "(elisp)Registers") for information on registers. Do C-hfwindow-configuration-to-register RET for information on the function that is bound to C-xrw.

Upvotes: 1

Drew
Drew

Reputation: 30708

Library frame-cmds.el has commands for this. They are essentially repeatable versions of standard Emacs commands, such as enlarge-window.

By "repeatable" I mean that you can just repeat the last keystroke of a key sequence.

For example, by default C-x } is bound to the standard Emacs command enlarge-window-horizontally. To repeat it you need to repeat the whole key sequence each time: C-x } C-x } C-x } .... (Or else you need to provide a numeric prefix arg for the number of columns you want to enlarge the window.)

But the repeatable version, command enlarge-window-horizontally-repeat lets you do just C-x } } } ....

The definitions are simple. They use this helper function:

(defun repeat-command (command)
  "Repeat COMMAND."
  (require 'repeat)         ; Define its vars before we let-bind them.
  (let ((repeat-previous-repeated-command  command)
        (repeat-message-function           #'ignore)
        (last-repeatable-command           'repeat))
    (repeat nil)))

This is all there is to the definition of enlarge-window-horizontally-repeat:

(defun enlarge-window-horizontally-repeat ()
  "Enlarge selected window horizontally by one column.
You can repeat this by hitting the last key again..."
  (interactive)
  (require 'repeat)
  (frcmds-repeat-command 'enlarge-window-horizontally))

Here are some key bindings you might use:

(global-set-key [remap enlarge-window-horizontally] 'enlarge-window-horizontally-repeat)
(global-set-key [remap shrink-window-horizontally]  'shrink-window-horizontally-repeat)
(global-set-key [remap enlarge-window]              'enlarge/shrink-window-repeat)

Besides this, there is also standard Emacs command shrink-window-if-larger-than-buffer, bound by default to C-x -. It shrinks the window to fit your buffer content.

Upvotes: 0

Related Questions