VIM Find And Replace All Text Substitute Command

In Vim, you can use the :%s/old-text/new-text/g command to replace all occurrences of “old-text” with “new-text” in the entire file. Here’s a breakdown of the command:

  • : – begins a Vim command line
  • % – represents all lines in the file
  • s – stands for substitute
  • old-text – the text you want to replace
  • new-text – the text you want to replace with
  • g – stands for global, meaning all occurrences in each line will be replaced (without g, only the first occurrence in each line will be replaced)

Here’s an example:

 
:%s/error/success/g

This command will replace all instances of the word “error” with the word “success” in the entire file.

Note that the changes won’t be saved until you write the file with :w. To replace and save the changes in one step, you can use :%s/old-text/new-text/gw.

Leave a Comment