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.
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.
I 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.
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 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.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.
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.