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
`fit-window-to-buffer'."
(interactive)
(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
`fit-window-to-buffer'."
(interactive)
(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.
WINDOW, MAX-HEIGHT, MIN-HEIGHT, MAX-WIDTH and MIN-WIDTH have the same meaning
as in `fit-window-to-buffer'."
(interactive)
(let ((fit-window-to-buffer-horizontally t))
(fit-window-to-buffer window max-height min-height max-width min-width)))