DEV Community

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

Posted on • Edited 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.

Prerequisites

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'
else
    Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
endif
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»:

terminal

Or «Esc+» option in iterm2:

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

screenshot

An article describing key features of this config.

Prerequisites

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

Installation

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

curl -L https://raw.github.com/gko/vimio/main/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

macOS

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

terminal

And «Esc+» option in iterm2:

iterm2

Shortcuts

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)

Collapse
 
zigius profile image
Daniel Singer

Thanks!
Very clean and easy solution

Collapse
 
alissonviegas profile image
Alisson Viegas

Thanks!!! Nice solution.