How to Find and Replace Project-wide in Vim

April 13, 2018

The problem

Sometimes one finds themselves with a need to search and replace some text throughout many places in a project. This often happens during a refactor, or when you made a mistake with a CLI code generator (like rails scaffold), and you accidentally misspell something.

In most modern IDEs, “find and replace all” is pretty simple - highlight the text, Ctrl-Shift-F, enter the new text, and hit enter. As you know, Vim does things a little differently.

You probably are already aware of how to find and replace all text within a file: : %s/text/newText/g - “within this file, find all instances of text, and replace it with newText.”

But doing so in multiple files at once with just one or two commands has proven difficult until Vim 7.4.8 released the :cdo and :cfdo commands. NeoVim with FZF and Ag makes it even easier.

The solution

Setup

Make sure you’re using Neovim (or Vim 7.4.8+, but really just use Neovim)