2

Is it possible to find/jump/point to where an Emacs LISP variable is used/defined within the current file? I know about find-variable, describe-variable and find-symbol, but those all seem to work only on globally defined symbols. I would like something that I can use to quickly examine the current file.

I recently started learning Racket, and in using the DrRacket IDE, I found it has this feature, which I find extremely useful:

DrRacket-link-variables

It would be great to have this feature in Emacs for emacs-lisp (and of course, for other languages as well).

5
  • 1
    Take a look at xref. And for other languages you have to use eglot or lsp-mode.
    – bertfred
    Jan 17, 2019 at 14:07
  • 2
    By default, M-.will jump to the definition, and M-? will find uses. In case of Elisp the former requires the code to be evaluated.
    – user12563
    Jan 17, 2019 at 14:45
  • @bertfred and @DoMiNeLa10 Thank you for the pointers! I think your comments should probably be answers. I took a look at xref - unfortunately it seems to only find top-level identifiers, but not (for example) variables declared in let or dolist, even with etags mode. Still, very useful to know. Jan 17, 2019 at 15:08
  • Take a look at Elsa - github.com/emacs-elsa/Elsa I haven't used it but believe it's capable of what you're looking for regarding let variables and such
    – kfoley
    Jan 17, 2019 at 15:22
  • @zzamboni new releases of Emacs can complete local variable names in Elisp, so I assume the ground work for this is already there, but I don't know whether anyone has worked on it because I have no use for such functionality myself.
    – user12563
    Jan 17, 2019 at 15:25

1 Answer 1

1

There are tools that find the definition of a variable, but if you want a list of all the places a variable is used in a file (including the definition), the swiper package is great. It's not specific to variables, or programming languages. It will prompt you for a string, then display a list of all the occurrences of that string in the buffer. You can move back and forth through the list to navigate quickly through the different locations.

Here's an example. Note that the list includes the line numbers for each occurrence.

enter image description here

enter image description here

2
  • Thanks! I didn't know about swiper. It will be useful not only for this, but for many other uses :) Jan 17, 2019 at 15:10
  • A tip for using the symbol at point as input, e.g. as in the screenshot, when the point is over swiper--width, type M-n to "swiper" for swiper--width in current buffer.
    – whatacold
    Jan 19, 2019 at 7:13

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.