How to Find and Replace Text using Vim Editor

Published February 06, 2025

You have opened a text file and want to find and replace text in Vi, Vim and Neovim editor while it is open. The text to replace may be hardcoded text or corresponding to certain regular expressions. This blog post will show you how to find and replace text in Vi, Vim and NeoVim editors.

How to find and replace text using Vim How to find and replace text using Vim

Create a file commit_history.txt using a bash one-liner command

For this blog post, we will create a file called commit_history.txt and fill it with lines similar to that of contents when you run git rebase -i.

Run this one-liner bash command to create commit_history.txt with 150 lines:

for i in {1..150}; do echo "pick $(md5sum <<< "${i}" | cut -f 1 -d " ") added file${i}.txt" ; done > commit_history.txt

The file will have 150 lines with random MD5 values like this:

pick b026324c6904b2a9cb4b88d6d61c81d1 added file1.txt
pick 26ab0db90d72e28ad0ba1e22ee510510 added file2.txt
pick 6d7fce9fee471194aa8b5b6e47267f03 added file3.txt
...
pick e6d604c19ab3dad77d96f20161330764 added file148.txt
pick b8344b1405a2d9cf936b0de14220b872 added file149.txt
pick 176ef0dfef8803a9ff66c1fd346824cc added file150.txt

Find and replace all occurrences of text pick with squash

Assuming the file is already open in Vi or Vim editor, and you want ot replace all occurrences of pick with squash.

First, press the Esc key. Then, type this:

:%s/pick/squash/g

This will replace all occurrences of pick with squash. Then, save and exit.

:wq!

NOTE: You will not be asked for confirmation before the replace operation.

Find and replace all occurrences of text with confirmation

To replace all occurrences of pick with squash, but with confirmation each time, use this command from within Vim.

:%s/pick/squash/gc

Find and replace all occurrences regardless of case

To replace all occurrences of pick with squash, and with search case insensitive, use this command from within Vim.

:%s/pick/squash/gi

You can combine case insensitive and confirmation, like this:

:%s/pick/squash/gci

Find and replace from lines 25 to 30

To replace all occurrences of pick with squash from lines 25 through 30, use this command from within Vim.

:25,30s/pick/squash/g

Find and replace from current line for the next 13 lines

To replace all occurrences of pick with squash from the current line for the next 13 lines, use this command from within Vim.

:.,13s/pick/squash/g

How to exit Vi or Vim editor

If you are new to Vi or Vim, or have forgotten, this is the command to exit Vi.

Quit without saving

:q!

Save and quit

:wq!

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: February 06, 2025.     This post was originally written on February 06, 2025.