16

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.

  • 1
    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: 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
  • 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. – jcaw 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. – DoMiNeLa10 Jan 29 '19 at 17:24
15

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.

|improve this answer|||||
  • 1
    It did run a tear down my cheek. – mihai Sep 30 '16 at 11:54
  • 1
    literally had tears in my eyes when executing this workflow for the first time, no kidding – Paul Dec 7 '18 at 23:41
  • Great solution! – fraxture Dec 5 '19 at 17:32
  • I had to learn to master 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 at 10:51
6

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.
|improve this answer|||||
3

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.

|improve this answer|||||
3

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:

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.