What is an easy way to automatically resize a window (height and or width) according to the length / line length of the buffer?

Possible behavior: A frame split horizontally with two windows with the right-hand buffer set to automatically resize to a minimum width that minimizes wrapped lines. A maximum width might be specified and occasionally force some lines to be wrapped.


The command fit-window-to-buffer resizes a window, so you can type M-x fit-window-to-buffer RET to resize the current buffer's window. By default fit-window-to-buffer only resizes vertically (according to height), but you can use the variable fit-window-to-buffer-horizontally to change that: - nil (the default): only resize vertically - only: only resize horizontally (according to width) - other value: both vertical and horizontal resizing is allowed

To resize a window programatically, you can call fit-window-to-buffer or hook it into appropriate places, depending on what you want to do exactly. Here are some helper functions that might be easier to use:

(defun fit-window-to-buffer-width (&optional window max-width min-width)
  "Fit WINDOW according to its buffer's width.
WINDOW, MAX-WIDTH and MIN-WIDTH have the same meaning as in
  (let ((fit-window-to-buffer-horizontally 'only))
    (fit-window-to-buffer window nil nil max-width min-width)))

(defun fit-window-to-buffer-height (&optional window max-height min-height)
  "Fit WINDOW according to its buffer's height.
WINDOW, MAX-HEIGHT and MIN-HEIGHT have the same meaning as in
  (let ((fit-window-to-buffer-horizontally nil))
    (fit-window-to-buffer window max-height min-height nil nil)))

(defun fit-window-to-buffer-height-or-width
    (&optional window max-height min-height max-width min-width)
  "Fit WINDOW according to its buffer's height and width.
as in `fit-window-to-buffer'."
  (let ((fit-window-to-buffer-horizontally t))
    (fit-window-to-buffer window max-height min-height max-width min-width)))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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