search replace with Emacs
Search (and replace) is something I do so much in emacs, I want to share my typical workflow for it.
search in buffer(s)
For this purpose, I like to use
swiper 1 which I bind to
swiper searches inside current buffer and list all matches in minibuffer.
To search in all open buffers, use
Sometimes I just prefer simple search like the classic vim way, then I use
which maps to
evil mode 2.
It behaves exactly like what you do with
/ in vim normal mode.
search in directories
ripgrep(rg) 3 seems to be the fatest search tool around and many
Emacs packages use it as backend.
.gitignore file but if you want it to search
everything, you can use
Sometimes it finds things from the file I don't want, to prevent that,
add a file with name
.ignore at the root of git repo as described here 4:
/TAGS *.min.js* /Build/Output/ /ThirdParty/
My favorite Emacs package for searching is
counsel-rg 5 and I bind it to
rg as backend.
counsel-rg by default looks for a directory with
.git and starts a search from
To limit the scope to current directory and its sub directories, not where
.git is, use
prefix argument before running
counsel-rg then select
which directory you want to run
deadgrep 6 is another package which uses
rg as backend for searching.
It has many features and allow filtering to be done more easier than
One notable feature is search on remote host:
If you're editing a remote file with Tramp, deadgrep will search the remote machine. This requires rg to be installed on that machine.
replace in multiple files
For this purpose, this great blog post 7 describes it in detail.
counsel-ag but same thing works for
which is the one I use.
In short, workflow is something like this:
type search pattern
ivy-occur) which saves result into a buffer
:%s/from/to/gto do the replace
wgrep-finish-edit) save the changes