I don't see the point in displaying xref with one match. Hope there is a simple solution.


2 Answers 2



Show the definitions list in a regular window.

When only one definition found, jump to it right away instead.

You can also keep using xref-show-definitions-buffer (to keep seeing the Xref buffer when there are multiple matches), just extend it to the "other" case as well:

(setq xref-show-xrefs-function #'xref-show-definitions-buffer)
  • I'm not sure I understand what you are suggesting: are you suggesting that the OP rewrite the function xref-show-definitions-buffer along the lines suggested in the other answer?
    – NickD
    Commented Feb 22, 2022 at 23:27
  • No, just use the above one-liner. xref-show-definitions-buffer already does "go to file when there is only one match". It is its main distinction.
    – Dmitry
    Commented Feb 23, 2022 at 0:17
  • OK - you might want to clarify the answer (I found it a bit mystifying), but that's interesting: I'll have to try it out at some point.
    – NickD
    Commented Feb 23, 2022 at 1:15
  • I hope the above is easier to understand, thanks.
    – Dmitry
    Commented Feb 23, 2022 at 17:45

You can use xref-show-definitions-completing-read:

(setq xref-show-definitions-function #'xref-show-definitions-completing-read
      xref-show-xrefs-function       #'xref-show-definitions-completing-read)

Update: If you want to keep using the xref buffer when there are multiple matches, you can write your own function, based on, but slightly modified from, xref--show-xref-buffer and xref-show-definitions-completing-read:

(defun my-xref--show-xref-buffer (fetcher alist)
  (cl-assert (functionp fetcher))
  (let* ((xrefs
           (assoc-default 'fetched-xrefs alist)
           (funcall fetcher)))
         (xref-alist (xref--analyze xrefs))
         (dd default-directory)
    (if (not (cdr xrefs))
        (xref-pop-to-location (car xrefs) (assoc-default 'display-action alist))
        (with-current-buffer (get-buffer-create xref-buffer-name)
          (setq default-directory dd)
          (xref--show-common-initialize xref-alist fetcher alist)
          (pop-to-buffer (current-buffer))
          (setq buf (current-buffer)))
        (xref--auto-jump-first buf (assoc-default 'auto-jump alist))

(setq xref-show-xrefs-function 'my-xref--show-xref-buffer)

I just added a check copied from xref-show-definitions-completing-read:

(if (not (cdr xrefs))
    (xref-pop-to-location (car xrefs) (assoc-default 'display-action alist))
    ;; original logic
  • Actually I wanted to keep xref interface when there is multiple matches Commented Feb 22, 2022 at 11:58
  • @albert200000 Updated Commented Feb 22, 2022 at 15:39

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.