Skip to main content

Posts

Showing posts from May, 2013

How to search and replace string in VI editor Linux

:%s/old_string/new_string/g
Find each occurrence of 'old_string' (in all lines), and replace it with 'new_string'.

:s/old_string/new_string/g
Find each occurrence of 'old_string' (in the current line only), and replace it with 'new_string'.

:%s/old_string/new_string/gc
Change each 'old_string' to 'new_string', but ask for confirmation first.

:%s/\<old_string\>/new_string/gc
Change only whole words exactly matching 'old_string' to 'new_string'; ask for confirmation.

:%s/old_string/new_string/gci
Change each 'old_string' (case insensitive) to 'new_string'; ask for confirmation.

This may be wanted after using :set noignorecase to make searches case sensitive (the default).
:%s/old_string/new_string/gcI
Change each 'old_string' (case sensitive) to 'new_string'; ask for confirmation.

This may be wanted after using :set ignorecase to make searches case insensitive.
The g flag means global – each…