How to search and replace in vim

This is a copy and paste documentation from the vim-wiki

 

A. Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.

:%s/foo/bar/g

 

B. Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.

:s/foo/bar/g

 

C. Change each 'foo' to 'bar', but ask for confirmation first.

:%s/foo/bar/gc

 

D. Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.

:%s/\<foo\>/bar/gc

 

E. Change each 'foo' (case insensitive due to the i flag) to 'bar'; ask for confirmation.

:%s/foo/bar/gci

 

F. This may be wanted after using :set noignorecase to make searches case sensitive (the default).

:%s/foo\c/bar/gc 

is the same because \c makes the search case insensitive.

 

G. Change each 'foo' (case sensitive due to the I flag) to 'bar'; ask for confirmation.

:%s/foo/bar/gcI

 

H. This may be wanted after using :set ignorecase to make searches case insensitive.

:%s/foo\C/bar/gc 

is the same because \C makes the search case sensitive.