With the way I work, I often just want one window (what's showing my code). Now several things (like opening help) open another window. That's fine, but when I'm finished I just want all windows closed and to return to the original.

Often Q will get me out of something, But not every time. I want a consistent shortcut to 'close all but the main window'

delete-other-windows is close to what I'm looking for, but I've obnoxiously been focusing the wrong window when utilizing it.

Is there a way to tag a window (say on startup of a frame, tag the first window), and then close all other windows besides that one? I don't want to think about where I'm currently focused.

  • Frames can take properties, but I'm not sure if windows can. Instead, have a look at winner-mode and the manual on window configurations.
    – Dan
    Jan 15, 2015 at 19:12
  • I use winner-mode frequently to undo unintended window splits and popups and then there is also the popwin package that allows you to control how *Help* and other windows open. Jan 15, 2015 at 19:29
  • 1
    How about assigning something like F11 to delete-window and F12 to delete-other-windows? If you do it for a couple of days, you'll be hooked. Just set up your mode-line to clearly indicate which window has focus, and check the mode-line color before choosing F11 or F12. I use Control+Option+Command+F11 and Control+Option+Command+F12
    – lawlist
    Jan 15, 2015 at 19:46
  • 1
    @Dan windows can have 'properties', they are called window parameters, I messed with them once here: stackoverflow.com/questions/21761971/… Jan 15, 2015 at 20:26
  • 1
    @JordonBiondo That post about window properties was just what I needed, thanks. Made an answer with it that's been working great. Jan 15, 2015 at 23:40

4 Answers 4


First rough draft. set-my-main-buffer sets the current buffer. just-my-main-buffer deletes other windows and switches back to the main buffer if not already selected.

(defvar my-main-buffer (current-buffer))

(defun set-my-main-buffer ()
  (setq my-main-buffer (current-buffer)))

(defun just-my-main-buffer ()
  (when (and
          (buffer-live-p my-main-buffer)
          (not (eq (current-buffer) my-main-buffer)))
    (switch-to-buffer my-main-buffer)))
  • awesome, this works well so far, after adding these hooks: (add-hook 'prog-mode-hook '(lambda() (set-my-main-buffer))) (add-hook 'text-mode-hook '(lambda() (set-my-main-buffer))) Jan 15, 2015 at 20:28
  • (add-hook 'prog-mode-hook 'set-my-main-buffer) and (add-hook 'text-mode-hook 'set-my-main-buffer) is preferred to using lambda because it makes removing elements from a hook easier -- e.g., remove-hook. Alternatively, a function containing multiple items can be added in one fell swoop -- and that function can also be removed in one fell swoop. Inspection of the hook -- e.g., M-x describe-variable RET prog-mode-hook RET -- is also easier on the eyes when lambdas are not included.
    – lawlist
    Jan 15, 2015 at 20:41

Here's the code from a post of mine

(require 'ace-window)

(defun ace-maximize-window ()
  "Ace maximize window."
  (setq aw--current-op
        (lambda (aj)
          (let ((wnd (aj-position-window aj)))
            (select-window wnd)
  (aw--doit " Ace - Maximize Window"))

(global-set-key (kbd "C-M-o") 'ace-maximize-window)

This lets you maximize a window that you select with ace-window.

This way, the "main window" isn't set in stone and you can select it dynamically and then delete all the other ones. This code lets you select the "main window" across all frames. You can extend it to delete all other frames as well, or just the windows on the selected frame.

  • as cool as this is, its more then I need. I always want to 'jump' to the same window, the choice part isn't useful to me. I use multiple frames and a tiling window manager to handle switching. Jan 15, 2015 at 23:43

You can save the initial window configuration for every newly created frame, then have a function that reverts the frames window configuration the initial one on command.

Here is the code for your init file:

;; this will run for each new frame
(defun set-frame-initial-layout (&optional frame)
  "Set the frames 'my-initial-layout parameter to the current window config."
  (let ((frame (or frame (selected-frame))))
    (set-frame-parameter frame 'my-initial-layout (current-window-configuration frame))))

;; add it to the hooks so it runs for each new frame
(add-hook 'after-make-frame-functions 'set-frame-initial-layout)
;; run it once at start up for the first frame

;; use M-x initial-configuration when to revert the current frames window
;; configuration to the initial layout it had upon creation
;; you could also bind this to a key
(defun initial-configuration ()
  "Change back the current frames window configuration to it's initial one."
  (set-window-configuration (frame-parameter nil 'my-initial-layout)))

The common use case would be that you create a new frame that shows buffer A, then after a while when you have a bunch of windows open and just want to see buffer A in that frame, you just do M-x initial-configuration and you're back where you were. I think I'll actually add this to my own configuration.

  • yeah. Sadly it reverts the buffers too. I only want the single window, not the initial buffer in view too. Often for me this is my scratch :P Jan 15, 2015 at 21:12

Ok, the answer from lawlist worked mostly for me, but with multiple frames I had trouble.

Invoking just-my-main-window deletes all windows besides your main window within your current frame. You can manually mark your main window or add hooks to do so when entering modes (shown below).

(defun mark-this-window-as-main ()
  "Mark the current window as the main window."
  (mapc (lambda (win) (set-window-parameter win 'main nil))
  (set-window-parameter nil 'main t))

(defun get-main-window()
  "Find and return the main window or nil if non exists."
  (cl-find-if (lambda (win) (window-parameter win 'main)) (window-list)))

(defun just-my-main-window ()
  "Show only the main window"
  (delete-other-windows (get-main-window)))

(add-hook 'prog-mode-hook 'mark-this-window-as-main)
(add-hook 'text-mode-hook 'mark-this-window-as-main)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.