Vim search and replace strings in multiple lines
Vim provides a handy command [colon s] :s
(substitute) for quick search and replace.
:substitute
search a string pattern and replaces it with a desired given string.
There are many ways to do it, a couple of what I use are below for a
scenarios like
Find each occurrence of text ‘foos’ in current file in all lines and replace it with text ‘ball’. ‘foos’ string is case insensitive
# <---- case sensitive command start ---->
# Find each occurrence of text 'foos' in current file
# in all lines and replace it with text 'ball'
:%s /foos/ball/g
# Find each text 'foos' in current line only and
# replace it with text 'ball'
:s/foos/ball/g
# Find each occurrence of text 'foos' in current file in all lines
# and replace it with text 'ball' but ask confirmation first
:%s/foos/ball/gc
# Find each text 'foos' in current line only and replace it
# with text 'ball' but ask confirmation first
:s/foos/ball/gc
# <---- case sensitive command end ---->
# <---- case-insensitive command start ---->
# Find each occurrence of text 'foos' in current file
# in all lines and replace it with text 'ball'
:%s /foos/ball/gi
# Find each text 'foos' in current line only and
# replace it with text 'ball'
:s/foos/ball/gi
# Find each occurrence of text 'foos' in current file in all lines
# and replace it with text 'ball' but ask confirmation first
:%s/foos/ball/gic
# Find each text 'foos' in current line only and replace it
# with text 'ball' but ask confirmation first
:s/foos/ball/gic
# <---- case-insensitive command end ---->
Find more about the vim :substitute
command using
:help :s