Most IDEs today includes a built-in way of searching and replacing a pattern across multiple files. Vim is no different.
Like many operations in Vim, there are 2 approaches you can take:
The “pure”/“native” way
Using a plugin
Today I needed to rename a
ReactJS component from “NewRoleModal” to “RoleModal”. Let’s use this
The Native Approach
Utilize day-to-day CLI commands for the job, in two steps:
Search all file names that include the term “NewRoleModal” in the relevant path, and set the result into the argslist.
:args `grep -rl 'NewRoleModal' app/react/modules`
If you want to check the content of the argslist just type
Feel free to use your favorite search command here instead of
Perform the replace action on all the search results and save.
:argdo %s/NewRoleModal/RoleModal/g | update
argdoiterates over all files and executes the substitution command.
updatesaves the input from the piped result, meaning the changed files.
The “Easy” approach
Erik recommended me the straightforward vim-easygrep plugin.
After installation just use the
:Replace NewRoleModal RoleModal
A useful keymapping I found is
<Leader>vr - “perform a global search on the word under the cursor and prompt for a pattern with which to replace it.”
.vimrc with your preferred plugin manager.