After running M-x occur, the commands next-error and previous-error let me navigate between hits.

I would like to be able to M-x compile my program and then keep browsing through the same *Occur* results. But if I compile then next-error and previous-error navigate the compilation errors, which makes perfect sense.

I don't want to run M-x occur again because I'd lose my place. What I want is to navigate occur results independently of compilation results.

How can I define next-occur and previous-occur commands that do this? (Note that this is not occur-next and occur-prev, which operate in the *Occur* buffer. I want to bind these to keys in the buffer that's being searched.)

Bonus: I'd prefer grep to be classified like occur, so I can also navigate grep results independently from compilation errors.

1 Answer 1


Looking at the source of occur-mode, I found this:

  (setq next-error-function 'occur-next-error))

This suggests occur-next-error is the function next-error calls in a "occured" buffer. And indeed, it seems that calling occur-next-error (with a negative arg to go backward) does work even after M-x compile. I have not tested this extensively, though.

So it would seem you just need to define a occur-prev-error command calling occur-next-error with arg -1 and use it along with occur-next-error.

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.