So, Dr. Racket has a very interesting feature where you can see all occurrences of a bound variable if you hover it:

Screencap of Dr Racket showing a definition of the quadratic equation with arrows pointing from the definition of b to its occurrences within the scope

Is there a way to do something similar in Emacs? I'm currently editing C++ code. It doesn't need to be super-sophisticated: I'd be super-happy with just the local lexical context.

This2 proved really useful to me, but it will highlight any matching words in the entire text, not just the code (let alone the enclosing lexical context.)

  • Fancy. That feels like the sort of feature that the upcoming tree-sitter integration might facilitate. Maybe semantic can already do it?
    – phils
    Commented Nov 23, 2021 at 0:25

1 Answer 1


It depends on the language. For Javascript, js2-mode has a complete language parser and so understand exactly which words really are variables, and which are the correct ones. js2-refactor has some functionality that relies on that exact information; I believe it can both highlight all occurrences of a variable and rename it, though it doesn’t draw lines over the top like that.

For C++, I have heard people mention the srefactor package. I’ve never used it though, so I don’t know if it does exactly what you want. You might also be interested in a general guide to related Emacs packages for C/C++ development.

There is also lsp-mode, which is all the rage at the moment.

  • srefactor sounds promising. I'll try it and come back with results
    – Romário
    Commented Nov 25, 2021 at 11:56

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.