When I grep something inside emacs and click to open one of the matching files. The matching line is always displayed at the bottom of the screen, and I have to scroll it up for a better view.

Is it possible to focus the matching line at the center of the screen automatically when opening one of the grep results?


Displaying the source line in the middle of the window is the default behaviour, so something in your own config has caused your problem.

The variable C-hv next-error-recenter usually determines where the line is positioned. A value of -1 will place it at the bottom of the window (which may be the cause of your problem); a value of 0 at the top; nil means "if the line is already visible somewhere in the window simply move point to there, but otherwise display the line in the centre of the window".

If you want to centre the line even when the line was already visible, you can use a value of '(4) (which mimics passing a prefix argument C-u to recenter), but I think you'll find the nil behaviour less disorientating.

Failing anything else (or if the above is not the cause of your problem), have a look at C-hv next-error-hook in case something is using that.

  • Thank you SO MUCH for this! The '(4) is just what I was looking for! – Rune Kaagaard Sep 14 '17 at 16:18
  • @RuneKaagaard you may also find C-h i g (elisp)Prefix Command Arguments useful to read. – phils Sep 14 '17 at 22:47

I recently faced the same problem. Searching around, I found centered-cursor-mode. It does not solve the problem directly, but by enabling it when looking at search results, I solved the problem well enough for my needs. Perhaps it will work for you as well.

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.