Skip to content

Search and Replaceλ︎

Neovim has several built-in commands to search for patterns in the current buffer and quickfix list can be used to run commands across all the files in a project.

Additional tools that work across all the files in a project:

multiple cursors for in-place editing within a buffer

/ searches through the current buffer, prompting for a pattern to search.

Symbol highlight and dot repeat

# on a word highlights each occurrence in the buffer, n and N to jump backward and forward for each instance.

Change the current occurrence (e.g. cw followed by new word)

Use . to repeat the change after jumping to the next instance.

Substitute commandλ︎

Use the :substitute (:s) to replace all occurrences of the current-pattern with the new pattern within the buffer.

Replace using substitute command

:%s/current-pattern/new-pattern/g

Add the c option to confirm each replacement

:substitue neovim command examples

Project search and replaceλ︎

Use search to populate the Neovim quickfix list and change occurrences across all the files in the current project, e.g.:

Search word and replace across project using quickfix list

Space f w to search for the supplied word or pattern

Ctrl q opens the search results in quickfix list

Use :cdo command to search and replace in the quickfix list

:cdo %s/current-pattern/new-pattern/g

Including the c option to confirm each replacement

Spectreλ︎

Spectre shows occurrences of a pattern throughout all project files, replace individual or all occurrences.

Search and replace with Spectre

Space s is the search and replace menu

Enter a Search pattern and all occurrences across the project are shown

Enter a Replace pattern to see occurrences with their replacement

R to replace all occurrences with the replace pattern

C to replace current occurrence

q adds the Spectre search results to quickfix list to edit occurrences with other Neovim tools

nvim-spectr

🌐 ripgrep and 🌐 sed are required.

🌐 Spectre