Search and Replace

Source: Wikia.com

Vim provides the :s (:substitute) command for search and replace. To find each occurrence of 'foo' (in all lines) and replace it with bar:

  • :%s/foo/bar/g

To find each occurrence of 'foo' (in the current line only) and replace it with 'bar':

  • :s/foo/bar/g

To change each 'foo' to 'bar', but ask for confirmation first:

  • :%s/foo/bar/gc

To change only whole words exactly matching 'foo' to 'bar'; ask for confirmation:

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

To change each 'foo' (case insensitive due to the i flag) to 'bar'; ask for confirmation:

  • :%s/foo/bar/gci

Last updated

Was this helpful?