Deleting windows vertically or horizontally

Posted: March 28, 2007 in Emacs

Most of the time I use emacs in fullscreen mode. So it’s a good thing to split the window horizontally, so you have two side-by-side windows. Now it happens sometimes that I split one of those two “main windows” vertically or a pup-up-window does so. So how do I get back to my 2 side-by-side window layout then?

There are several possibilities:

  1. I saved the layout and can quickly restore it. See my blog entry Conveniently save and restore frame configurations
  2. I use winner-mode and so I can switch back to older window configurations step by step. This can be quite annoying if I had several layout changes in between.
  3. I switch into all the windows I want to delete and hit C-x 0 in them.
  4. I use this code which provides a pimped version of the standard emacs function delete-other-windows usually bound to C-x 1. My version th-delete-other-windows acts like the standard function when you don’t provide it a prefix arg. With prefix arg 2 or 3 it deletes the windows below/above or left/right of the current window.
    (require 'windmove)
    (defun th-delete-other-windows-vertically ()
      "Delete all windows above or below the current window."
        (while (condition-case nil (windmove-up) (error nil))
        (while (condition-case nil (windmove-down) (error nil))
    (defun  th-delete-other-windows-horizontally ()
      "Delete all windows left or right of the current window."
        (while (condition-case nil (windmove-left) (error nil))
        (while (condition-case nil (windmove-right) (error nil))
    (defun th-delete-other-windows (&optional arg)
      "If ARG is 2 delete all windows which are above or below the
    current window. If ARG is 3 delete all windows which are left or
    right to the current window. If no prefix arg is given, delete
    all other windows."
      (interactive "p")
       ((= arg 2) (th-delete-other-windows-vertically))
       ((= arg 3) (th-delete-other-windows-horizontally))
       (t (delete-other-windows))))
    (global-set-key (kbd "C-x 1") 'th-delete-other-windows)

UPDATE: There was a line (message "Arg = %d" arg) in
which raises an error when the function is called without a prefix arg. Simply remove the line. In addition it has to be (interactive "p") instead of (interactive "P"). Both defects are removed in the code above, now. I really should start testing all possibilities before posting…

UPDATE 2: In some situation the wrong window is selected after invoking one of the functions above. You can find the fixed versions in the file th-common.el my darcs repository. Have a look at my ElispArea.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s