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:
- :s/foo/bar/g => replaces foo with bar in the current line.
- :%s/foo/bar/g => replaces foo with bar in the entire document.
Some necessart flags:
- g => is used to replaces the occurances.
- c => is used to confirm the replacement.
- i => is used to ignore case sentivity.