The context

In the gif shown below, I demostrate that the way next-error show the current error in a new window depends on the size of the font.

  • When using the font Fira Code 20, a single window is used to show the selected error.
  • When using the font Fira Code 10, 3 windows are used to show the selected error.
  • When using the font Misc Fixed:pixelsize=7, 7 windows are used to show the selected error.

enter image description here

The question

How can I change the behavior of next-error and previous-error so that a single window is opened on the right of the compilation buffer (i.e. the *grep* buffer) and make this behavior not to be affected by the size of the font being used?

  • See the relevant section of compilation-goto-locus; i.e., (if from-compilation-buffer (let ((pop-up-windows t)) (pop-to-buffer (marker-buffer mk) 'other-window)) (switch-to-buffer (marker-buffer mk))) [comments from code omitted]. I have other projects on my plate at the moment, so perhaps another forum participant can write up an answer ...
    – lawlist
    Nov 15, 2020 at 20:57
  • Now that I have a little more free time this evening, I am at a complete loss regarding how a user could achieve the results depicted in the GIF animation by using a default installation of Emacs -- i.e., without any user-configuration and/or additional third-party libraries. Are you able to reproduce the issue with a barbones installation with no user-configuration; aka emacs -Q?
    – lawlist
    Nov 16, 2020 at 1:52
  • The Emacs I show in the gif was opened by executing emacs -Q . As seen in the gif, I only use commands which are bundled with Emacs, namely, grep, set-frame-font, next-error. To achieve the results, execute grep on a set of files so that next-error have multiple files to open and make sure you have a big and a small font. You can create ten files containing only numbers (as seen in the gif) by executing the following one-liner for i in $(seq 1 10); do seq 1 100 > $i.txt; done just make sure to execute that command in an empty folder so that you could remove those files easily.
    – doltes
    Nov 16, 2020 at 2:01

1 Answer 1


You should know that the function responsible of telling whether a window should be splitted or not is window-splittable-p (in v28.2). You can see how this function is called by instrumenting window-splittable-p with Edebug (press C-u M-x in the function definition) and then calling edebug-pop-to-backtrace (by default, bound to d) while in debug mode.

Here's how you can do it:

  1. Make Emacs use the small font
  2. Split the windows as you want *grep* to split when showing results. In the GIF you showed, when using the big font, there are two windows of the same size that are vertically arranged
  3. Use (window-height (selected-window)) and (window-width (selected-window)) to get the height and width of any of the windows (both have the same size, so you can choose either of them)
  4. Define the following mappings in grep-mode-map. Here, we set split-height-threshold and split-width-threshold to values that are greater than the values reported by the previous sexp. Thus, window-splittable-p will always evaluate to false which will result in no splits.
(defun my/next-error-no-select ()
  (let ((split-height-threshold 140)
        (split-width-threshold 260))

(defun my/previous-error-no-select ()
  (let ((split-height-threshold 140)
        (split-width-threshold 260))

(define-key grep-mode-map (kbd "n") 'my/next-error-no-select)
(define-key grep-mode-map (kbd "p") 'my/previous-error-no-select)

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.