How can I do a "find and replace a string" in the files of a project? Is there a standard, built-in way?

  • Maybe you can have a look here: stackoverflow.com/questions/44919530/… Dec 4 '17 at 9:45
  • 1
    Define what you mean by “project” in terms of how emacs will know what’s in it or not. Example: “everything in directory X and it’s subdirectories.”
    – Dan
    Dec 4 '17 at 11:40
  • You ask for a "standard, built-in" way. Be aware that that is limiting, because Emacs itself has no "standard, built-in" definition of a "project". If you define that term more clearly for your question, it might help direct "standard, built-in" answers toward that definition. (Otherwise, a more useful question might drop the "standard, built-in" restriction. But even then it would help to say what you mean by "project".)
    – Drew
    Dec 4 '17 at 15:11

dired has a command dired-do-find-regexp-and-replace, which is bound to Q by default. Keep in mind it uses regular expressions.

  • And in Emacs before Emacs 25, the same key, Q, does the same thing, but differently - it is bound in Emacs 24 and prior to command dired-do-query-replace-regexp.
    – Drew
    Dec 4 '17 at 15:14
  • ok. and how can I just search for a text in files of a project without replacing it?
    – Rakori
    Dec 4 '17 at 23:33
  • @Rakori you can mark files that contain a regexp in dired with % m, or you can use M-x grep.
    – user12563
    Dec 5 '17 at 15:25

Like said, there are no standard ways of doing complicated things in emacs. Assuming project is defined as a git repository, you can do the following:

Install abo-abo's swiper/ivy/councel, https://github.com/abo-abo/swiper, and wgrep from ELPA. Bind the necessary commands to keys of your choice, especially ivy-occur that is usually C-c C-o and wgrep (C-x C-q).

  1. With a project file open in your active buffer, give command councel-git-grep and type in the string you want to find in all project files.

  2. Press your ivy-occur key to have the found lines in an new buffer.

  3. Launch write grep (wgrep) to do editing in the occur buffer.

  4. Save and exit with C-c C-c.

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.