Swapping via regular expressions is performed by the ex command
:{range}s/pattern/string/{flags} {count}
If you wish to replace instances of a work on a specific line you can supply the exact line with the absolute location. To swap matches only on line 2:
:2s/Seconds/time
Or you can use a relative location using -/+
. To swap only on a line two above the cursor:
:-2s/Seconds/time
Range can also be defined by using visual mode to select the area to perform the search. Visually select a region and press :
:'<,'>s/Second/time
Ranges can also be absolute or relative
:2,4s/Second/time
:-2,+2s/Second/time
The first does lines 2 through 4 and the second does 2 lines above the cursor to two lines below the cursor.
If you want to swap all cases you can supply the range and the flag g
which finds all matches. Supply the %
range for the entire document.
:%s/Seconds/time/g
Some commonly used flags are:
c
: Confirms each substitution (yes/no/abort/quit)
g
: Replace all occurrences in a line. Without this flag only the first instance on a line is swapped.
i
: Ignore the case of the pattern
I
: DO NOT ignore the case of the pattern
For further information check out the built in help :h :substitute
.
time
(the other way around). Please add exact expected results, and a description of what exactly you're struggling with!Time: 60 minutes Seconds: 3600 Milliseconds: 36000
What am looking for is after the replace,Time: 60 minutes Time: 3600 MilliTime: 36000
Basically, I would like to know how I can be more specific about only replacing lines that have Seconds in the string, and avoiding the others if there were more than just these 3 lines.:help :range
; it has all the details on how to be more specific than the global:%
range.