3

I run a command that takes over another window, and maybe resizes it a little, but doesn't focus it. Let's say C-h e or C-x C-b.

I can go to that window and press q there, and everything goes back to the previous state (the window resizes back, its previous content is restored, focus is back to the original window).

I want to be get the same effect without having to go to that window. Instead of C-h e C-x o q I want to be able to do C-h e <some keystroke that will close the most recently opened closeable-by-pressing-q window>.


(Months later) I found a simpler and more straightforward way... sometimes the simpler things elude us... (I'm leaving the "solved" mark where it is anyway):

(defun quit-other-window () (interactive) (other-window 1) (quit-window)) (global-set-key (kbd "C-c q") #'quit-other-window) 

1 Answer 1

2

You have a couple of options. You can use the windmove-delete-up, -down, -left and -right functions. I use them with these bindings:

(global-set-key (kbd "C-c W") #'windmove-delete-up) (global-set-key (kbd "C-c S") #'windmove-delete-down) (global-set-key (kbd "C-c A") #'windmove-delete-left) (global-set-key (kbd "C-c D") #'windmove-delete-right) 

Say I type C-h e and it opens the *Messages* buffer in a window below the current one, which remains selected, then I can type C-c S to delete the *Messages* buffer's window.

Otherwise you can enable winner-mode and use winner-undo, which will undo the latest change to the arrangement of the windows. winner-mode binds some keys by default when it is loaded, so if you want to have winner-undo always available but use your own key bindings, you could add this to your init file

(setq winner-dont-bind-my-keys t) (winner-mode) 

and then, for example

(define-key winner-mode-map (kbd "C-c q") #'winner-undo) (define-key winner-mode-map (kbd "C-c e") #'winner-redo) 
1
  • Thanks, Arch. The first one doesn't work when the frame is already split and the function takes over an existing window. The windmove-delete-* functions just delete it, so they don't restore the original layout. winner-undo works like a charm, though. Commented Jan 28, 2021 at 16:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.