IT Questions and Answers :)

Thursday, November 23, 2017

Using VIM, what is the find\replace function for replacing the word 'hot' with 'cold' in an entire document?

Using VIM, what is the find\replace function for replacing the word 'hot' with 'cold' in an entire document?

  • :s/hot/cold/g
  • :%s/hot/cold/g
  • :qw! hot\cold
  • :w!/hot/cold%s 
Using VIM, what is the find\replace function for replacing the word 'hot' with 'cold' in an entire document?

 EXPLANATION

Vim provides the :s (substitute) command for search and replace.  The :substitute command searches for a text pattern, and replaces it with a text string. There are many options, but these are what you probably want:
:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
:s/foo/bar/g
Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.
:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first.
:%s/\<foo\>/bar/gc
Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.

SOURCE

http://vim.wikia.com/wiki/Search_and_replace
Share:

Popular Posts

Blog Archive