Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize occur window in Emacs

Tags:

emacs

elisp

When entering occur mode for example (occur "test") the frame splits into two windows as shown below:

enter image description here

As seen the Occur buffer is taking up too much space on the frame, since there is only two matches (for the text "test"). I would like to shrink that window accordingly.

I tried the following code:

(defun test-occur ()
  (interactive)
  (occur "test")
  (save-window-excursion
    (other-window 1)
    (let (( win (selected-window))
      (n (count-lines (point-min) (point-max)))
      (h (window-body-height)))
      (let ((delta (- (- h n) 3)))
    (window-resize win (- 0 delta) nil)))))

But it does not work (nothing happens with the Occur window)..

like image 715
Håkon Hægland Avatar asked Sep 04 '25 16:09

Håkon Hægland


1 Answers

Just do this:

 (add-hook 'occur-hook
       (lambda ()
         (save-selected-window
           (pop-to-buffer "*Occur*")
           (fit-window-to-buffer))))
like image 197
Drew Avatar answered Sep 07 '25 16:09

Drew