I want to rewrite parts of an opensource project. This includes renaming methods. How can I do that efficiently in emacs across the whole project?
I would rather not use hacks such as search and replace in every buffer.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityI now use helm-ag
to find all instances of the function name (searches in all files, incl. subdirs, not just in open buffers), and then I use C-c C-e
to enter a buffer that lists all the matches and there I change the function name. When I am done I press C-c C-c (helm-ag--edit-commit)
to store the changes to all the opened files.
This might sound confusing but please see https://github.com/ShingoFukuyama/helm-swoop
When you grok it you will cry tears of joy that such wonderful functionality exists.
iedit
before this solution made sense to me. I use C-c C-q
to quickly edit the variables inside the buffer of matches.
Mar 25, 2020 at 10:51
Since you didn't specify the language, it's hard to give better answers than “Search and Replace”.
If you just want to (interactively) replace all instances of a symbol with another, wgrep is your best bet. It let's you interactively edit the grep results buffer.
M-x package-install RET wgrep
.M-x rgrep
. It will ask you a few easy questions and then do a recursive search for the search-term inside the directory you speficied.M-x wgrep-change-to-wgrep-mode
. This buffer is now editable. Any changes you make here will be reflected in the files themselves.replace-regexp
or a query-replace-regexp
in this buffer, to do the refactoring.M-x wgrep-save-all-buffers
and M-x wgrep-finish-edit
.You can use counsel-rg
followed by C-c C-o to get a list of all candidates in a buffer. Press w to edit them with wgrep
. I recommend iedit
for symbol renames. Press C-c C-c to commit.
Dependencies:
For some generic approaches, look under the "Replace Across Files" heading on the Emacs Wiki search & replace page: http://www.emacswiki.org/emacs/CategorySearchAndReplace
Also see the answers to: Using Emacs to recursively find and replace in text files not already open.
You can use swiper
M-x swiper-all
type your regex, it will list all results, you can move with up/down to see a preview for each result.M-q
and it will ask you what you want to replace the matches with, type it and press enter.counsel-projectile-rg
or spacemacs/search-project-rg
(configuring spacemacs to use ivy) and this will work very well.
M-x project-find-regexp
could help, followed by typing r
in the xref buffer.
It's not a "real" refactoring, but well along the lines of other answers here.
Or use and LSP client, such as eglot
or lsp-mode
, along with a suitable LSP server such as pyls
. Those offer smarter rename operations.
Inspired by Dmitry's anwer, but searching through the entire project (not only the open buffers) and saving all the modified files in the end:
M-x +ivy/project-search
type your regex, it will list all results (I tend to use this function a lot so I bound it to C-x f
).M-q
and it will ask you what you want to replace the matches with, type it and press enter.y
/n
/!
on each, y
to replace, n
to skip, and !
to replace all without asking.C-x-s
and then !
to save all of them.Requires: Ivy.
ropemacs
.