Reference

Find and Replace Command

The command to find and replace a word in a document is by the use of :subtitute (:S) command. The basic command is as follows:

:[range]s/{pattern}/{string}/[flags] [count]
  • [range] = the word to be replaced will be searched within this specified lines.
  • {pattern} = the word that has to be replaced
  • {string} = the new replaced word
  • [flags] = is constraint for the command
  • [count] = is a postive number that mutliplies the command

Note: To replace a special charater use “\” before the character, for example, to replace $apple => ^orange use "

:%s/\$apple/\^orange/g".

Examples:

  1. :s/foo/bar/g => replaces foo with bar in the current line.
  2. :%s/foo/bar/g => replaces foo with bar in the entire document.

Some necessart flags:

  1. g => is used to replaces the occurances.
  2. c => is used to confirm the replacement.
  3. i => is used to ignore case sentivity.