25

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.

The Unfun Cat
  • 2,393
  • 16
  • 32
  • 2
    Which language? – phils Jan 21 '15 at 10:56
  • Have you looked at ag/grep + wgrep + search/replace? You don't open buffers individually in that. Related: http://emacs.stackexchange.com/a/243/115 – Kaushal Modi Jan 21 '15 at 13:27
  • Python. I realize my question was not the best, sorry. – The Unfun Cat Jan 21 '15 at 13:54
  • 1
    As this question has been bumped to the top (in case anyone stumbles upon this from Google), there is a dedicated python library for refactoring called Rope. It's more intelligent than the other solutions offered as it takes into account context. It is implemented in Emacs in a package called `ropemacs`. – JCC Sep 30 '16 at 17:10
  • @TheUnfunCat can you add more context to your question? As of now it's pretty vague, and adding that you want to do this in a python project would clear up a lot of things. –  Jan 29 '19 at 17:24

7 Answers7

21

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.

mihai
  • 321
  • 2
  • 12
The Unfun Cat
  • 2,393
  • 16
  • 32
8

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:

abo-abo
  • 13,943
  • 1
  • 29
  • 43
8

Since you didn't specify the language, it's hard to give better answers than “Search and Replace”.

Using wgrep

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.

  1. Do M-x package-install RET wgrep.
  2. Run 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.
  3. Switch to the results buffer and do M-x wgrep-change-to-wgrep-mode. This buffer is now editable. Any changes you make here will be reflected in the files themselves.
  4. Run a replace-regexp or a query-replace-regexp in this buffer, to do the refactoring.
  5. Finish your edits with M-x wgrep-save-all-buffers and M-x wgrep-finish-edit.
Malabarba
  • 22,878
  • 6
  • 78
  • 163
4

You can use swiper

  1. Run 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.
  2. Now press M-q and it will ask you what you want to replace the matches with, type it and press enter.
  3. It will now walk you through an interactive replace, you can use y/n/! on each, y to replace, n to skip, and ! to replace all without asking.
Dmitry
  • 3,508
  • 15
  • 20
Didier A.
  • 258
  • 2
  • 8
  • It is called swiper though :) Thanks! – The Unfun Cat May 28 '20 at 11:02
  • This works for any of the counsel functions - so you can also run `counsel-projectile-rg` or `spacemacs/search-project-rg` (configuring spacemacs to use ivy) and this will work very well. – guibor Mar 29 '21 at 09:50
4

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.

phils
  • 48,657
  • 3
  • 76
  • 115
1

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.

Dmitry
  • 3,508
  • 15
  • 20
0

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:

  1. Run 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).
  2. Now press M-q and it will ask you what you want to replace the matches with, type it and press enter.
  3. It will now walk you through an interactive replace, you can use y/n/! on each, y to replace, n to skip, and ! to replace all without asking.
  4. To save all the modified buffers, run C-x-s and then ! to save all of them.

Requires: Ivy.

kir0ul
  • 47
  • 4