DEV Community

Cover image for Find and replace text in project files with vim

Posted on • Updated on

Find and replace text in project files with vim

I've seen a lot of articles describing how to replace text with the cfdo command and others that describe how to find files with FZF, but I couldn't find one that «puts it all together», so I decided to make a short instruction.


First you need to install fzf via vim-plug:

call plug#begin()

" if you have fzf installed via brew
if isdirectory('/usr/local/opt/fzf')
    Plug '/usr/local/opt/fzf'
    Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim', {
        \ 'on': [
            \ 'Ag',
            \ 'Rg',
            \ 'FZF',
            \ 'Files',
            \ 'Buffers',
            \ 'Commits',
            \ 'BCommits',
            \ 'Tags',
            \ 'BTags',
            \ 'History',
            \ 'Lines',
            \ 'BLines',
            \ 'Marks'
        \ ] }

call plug#end()
Enter fullscreen mode Exit fullscreen mode

and then ripgrep

Configuring Meta key (⌥) in macOS terminal

Some shortcuts use Meta key. To make it work properly in macOS terminal check the «Use option as meta key»:


Or «Esc+» option in iterm2:


Finding and replacing text

  1. To search text within project files simply type :Rg <text>. This will open up a FZF buffer with a list of files.
  2. Then with a Tab key you can select/deselect files that you want to pass to quickfix window. To select all files tap Alt/⌥-a and to deselect Alt/⌥-d.
  3. Once your selection is ready hit Enter to pass selected files to quickfix buffer.
  4. Now you can replace text in all files within quickfix buffer with :cfdo %s/<text to search>/<text to replace with>/g | update.

The cfdo command simply executes command on every file in the list, and the update command after the pipe saves the results to the disk.

I've used here some of the snippets from my vim setup. You can find it here:

GitHub logo gko / vimio

🎩 easy to install/use vim settings

Vim Settings


An article describing key features of this config.


In order to get all features you might want to install following packages:


On unix and windows(with bash which can be installed with git):

curl -L | bash
Enter fullscreen mode Exit fullscreen mode


In macOS don't forget to check the «Use option as meta key»:


And «Esc+» option in iterm2:



Some of shortcuts(Leader key is comma):

  • Ctrl + s saves current file
  • Leader + s in both select and normal mode initiates search and replace
  • Alt + Up/Down moves line or selection above or below current line(see upside-down for more info)
  • Alt + Left/Right moves character or selection to left or to the right
  • Leader + n toggles NERDTree
  • Leader + m shows current file in NERDTree
  • when in select mode ', ", ( wraps selection accordingly
  • y

Top comments (2)

zigius profile image
Daniel Singer

Very clean and easy solution

alissonviegas profile image
Alisson Viegas

Thanks!!! Nice solution.