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: https://stackoverflow.com/questions/44919530/find-text-in-a-project-but-within-a-specific-folder/44922948#44922948 – Picaud Vincent Dec 04 '17 at 09:45
-
1Define 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 04 '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 04 '17 at 15:11
3 Answers
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
).
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.Press your
ivy-occur
key to have the found lines in an new buffer.Launch write grep (
wgrep
) to do editing in the occur buffer.Save and exit with
C-c C-c
.

- 2,961
- 11
- 18
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 04 '17 at 15:14
-
ok. and how can I just search for a text in files of a project without replacing it? – Rakori Dec 04 '17 at 23:33
-
@Rakori you can mark files that contain a regexp in dired with `% m`, or you can use `M-x grep`. – Dec 05 '17 at 15:25
Since I am new to emacs, I needed to know every little step of the process. This is what ultimately worked for me:
C-x C-f
opens'find-file
(or alternatively useM-x
'project-find-file
RET
).- Hit
RET
or type a subdirectory and hitRET
. At this point a list of files in the directory appears and your cursor gets focus there. - Use arrow keys to go up/down, and hit
m
to mark each file/folder you want to run find & replace on. M-x
dired-do-find-regexp-and-replace
RET
prompts you for a regex find string. Type it then hitRET
.- Now it prompts you for a replace string. Type it then hit
RET
. - Now it brings you to found strings one-by-one. For each one, type
y
to perform the replacement or typen
to leave it unchanged.

- 101
- 2