8

Is it possible to make the occur mode grab the cursor (point)?, right now when I do M-x occur and search anything in a buffer, it will open the new buffer with the findings but the cursor stays in the buffer instead going to the findings.

I have to do C-x 0 to go to the other buffer every time I do a search.

I tried doing it with a blank init.el just in case is helm or some other package, but I haven't been able to make it focus the cursor on the new buffer.

(I know that there's the helm-occur that does that, but is it possible with any command or function to make the cursor change to the active buffer when that command opens a new buffer? )

1
  • 4
    The correct way to navigate is to use M-g M-n and M-g M-p bindings to jump to each occurrence of your search term without jumping to the *Occur* buffer. I learnt that trick from here. Commented Jun 16, 2015 at 19:00

1 Answer 1

11

Occur has a hook, occur-hook, that contains a list of functions to run after a match is found. So we can add a hook to jump to the *Occur* window there:

(add-hook 'occur-hook
          '(lambda ()
             (switch-to-buffer-other-window "*Occur*")))
2
  • Awesome!. It works perfectly. So, I can use switch-to-buffer-other-window with anything that exposes a hook. Can I?.
    – Fabman
    Commented Jun 16, 2015 at 17:22
  • 2
    Sure, as long as you know the name of the buffer you want to switch to. You can call any function from any hook. That isn't guaranteed to be a good idea in all cases, but it's up to you to decide! You have to pay special attention to when the hook is called - some are called in advance of a function, some after it runs. The name and/or the documentation string usually explain this for a particular hook.
    – Tyler
    Commented Jun 16, 2015 at 17:25

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.