TL;DR: erefactor-rename-symbol-in-buffer
You're looking for a lexically intelligent rename function - one that renames variables by their scope, not just a dumb find and replace. Packages like this exist for a lot of languages but as of writing (April 2019), there aren't actually many options for Emacs Lisp. The erefactor
package is the only one I know of.
erefactor-rename-symbol-in-buffer
is the function you want. It's an intelligent find-and-replace command that will replace symbols in the current scope (including docstrings). The limitation of this command is that it won't search outside the current buffer. It's smart, but not perfect. It will ask you to confirm each occurance to ensure the end result is sanitary.
The other rename function is erefactor-rename-symbol-in-package
. This doesn't work the same way - it only works on global symbols. It relies on your package being set up correctly, with a grouping system and prefix, and it needs the variable to match the package's prefix. It's not very robust - it will fail if you haven't set things up the way this function expects. I think if you're renaming a global symbol, you may as well use a naive find and replace since global symbols in Elisp don't have a namespace. The benefits of lexical understanding are minimal. This command is too fragile for the tradeoff. I would suggest just using projectile-replace
.
Erefactor has a number of other Elisp commands you might find useful. An (incomplete) list is available in the readme.
It's not described on the GitHub page but erefactor
is available on MELPA (M-x package-install RET erefactor
). The MELPA page can be found here.