On next-error in the grep or compilation window, I can use something like

(setq next-error-recenter 35)

to ensure that window displaying the match scrolls to show the whole match, as described in more detail in this stackoverflow question.

How do I do the same thing in the grep or compilation window itself? I often have grep output or compile output that is off the bottom of the window, and on days when I spend all of my time going through grep output or compile errors, switching back and forth between windows adds a significant overhead.

  • You want to look at compilation-goto-locus and put in a recenter command where appropriate -- at the section that begins with the comment: If the compilation buffer window was selected . . . , You can recenter at different location of the window -- see the doc-string for recenter.
    – lawlist
    Feb 26, 2016 at 7:40
  • Added a command recenter-current-error, bound to l in the *Occur* and *Grep* buffers; the effect of this command is like calling C-l in the window displaying the current error. This will be available in a future release Emacs-28.
    – Tino
    Feb 7, 2021 at 18:40

1 Answer 1


I use such workaround for the exact saving of point position in the compilation/grep buffer:

(defun eab/compile-goto-error ()
  (let ((istc? truncate-lines))
    (toggle-truncate-lines t)
    (let ((buf (current-buffer))
          (line (- (count-lines (window-start) (point))
                   (if (eq (point) (point-at-bol)) 0 1)))
          (point (point)))
      (run-with-timer 0.01 nil `(lambda ()
                                 (let ((cb (current-buffer)))
                                       (pop-to-buffer ,buf)
                                       (recenter ,line)
                                       (goto-char ,point)
                                       (toggle-truncate-lines ,istc?)
                                       (pop-to-buffer cb)))))))

If you want simple centering of the point in the compilation/grep buffer, use:

(defun eab/next-error ()
  (let ((buf (current-buffer)))
    (run-with-timer 0.01 nil `(lambda ()
                             (let ((cb (current-buffer)))
                               (pop-to-buffer ,buf)
                               (pop-to-buffer cb))))))

You can use next-error instead of compile-goto-error in the first example.

Upd. Emacs 25.1 is needed for non-zero time period.

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.