3

Suppose I have a file that contains:

Time: 60 minutes
Seconds: 3600
Milliseconds: 36000

I would like to replace any word with seconds.

%s/seconds/time/g

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.

  • The example you give should work, is there something more specific you are having a problem with? – jecxjo May 13 '15 at 19:28
  • Your given command replaces with time (the other way around). Please add exact expected results, and a description of what exactly you're struggling with! – Ingo Karkat May 13 '15 at 19:52
  • Say for example the file had: 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. – Juan Davila May 13 '15 at 19:53
  • Read :help :range; it has all the details on how to be more specific than the global :% range. – Ingo Karkat May 13 '15 at 20:14
5

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.

2

Aside from jecxjo's answer, there are also 2 other ways to replace only seconds and leave milliseconds` alone (without using ranges, which may be unwieldy for large files):

  1. Anchor the pattern to the start of the line with ^: %s/^seconds/time/; works in this specific case, but (obviously) won't work if you have an instance of seconds somewhere that isn't at the start of the line

  2. More flexible is using a "word" match: %s/\<seconds\>/time/. The \< matches the "beginning of a word", and the \> matches the "end of a word"; so adding them both only matches if seconds is a word on it's own (and not part of a word); in this case, you could also use :%s/\<seconds/time/, because in milliseconds, seconds isn't at the start of the word.

  • Weirdly, from the comment: "What am looking for is after the replace, Time: 60 minutes Time: 3600 MilliTime: 36000". – muru May 13 '15 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.