When I press gd to go to the definition of a class in C++, Emacs splits the screen and shows me two definition options that I need to click on with one of them. I'm used to coc.nvim where I would just go for the first option at once. How do I do this in Emacs?


You may want to use package ivy-xref or helm-xref to replace the "window-ish" selector with popular completion framework like ivy or helm (so it feels better when picking a definition). Sure you also need ivy/helm themselves. It's easy if you already use one of them, otherwise have a try.

If you insist... Digging down into xref-find-definitions, it's this function who does the most work:

(defun xref--show-xrefs (xrefs display-action &optional always-show-list)
   ((and (not (cdr xrefs)) (not always-show-list))
    (xref--pop-to-location (car xrefs) display-action))
    (funcall xref-show-xrefs-function xrefs
             `((window . ,(selected-window))
               (display-action . ,display-action))))))

We can see the first branch of cond is when there's only one candidate, just jump to it. The second branch (the t part) is that when there are more than 1, we show them with the value of the variable xref-show-xrefs-function. So you need to customize the value of xref-show-xrefs-function to make the function do the same as the first branch of the above cond. Something like this (not tested):

(setq xref-show-xrefs-function 'my/xref-just-jump-to-first)

(defun my/xref-just-jump-to-first (xrefs alist)
  "However many candidates, just jump to the first."
  (xref--pop-to-location (car xrefs) nil))
| improve this answer | |

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.