Thursday, July 15, 2010

Searching and replacing in VIM

Vim has a command ":s(substitute)" for search and replace text. It searches for text and replace the text.

:%s/abc/xyz/g 
Find each occurrence of 'abc', and replace it with 'xyz'.


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

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

:%s/abc/xyz/gcI 
Change each 'abc' (case sensitive) to 'xyz'; ask for confirmation.

:s/abc/xyz/g
Replaces in current line only
 :%s/abc/xyz/g
Replaces in all lines
:4,8s/abc/xyz/g
Replaces all abc by xyz in lines between 4 & 8


So ":%s" is equivalent to ":1,$s"

Modifiers:
g: GLobal
c: confirmation
i: case insensitive
I: Case sensitive

Reference: VIM Wiki

1 comment: