Find and replace strings in vim

Last Updated On Monday 25th Apr 2022

Find and replace in Vim

	:%s/find this/replace with this/g

Vim Replace Character

We can drop the range of the find and replace from all lines to only the current one by removing the %.

	:s/find this/replace with this/g
  • The % is just a shortcut for 1, $, where means the end of the file.
	:10,15s/find this/replace with this/g

We’ll run it on lines 10 through 15(inclusive)

Replace in vim


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

Search and Replace in vim

If you have many range

	:for range in split('6,10 14,18')| exe range 's/<search_string>/<replace_string>/g' | endfor