1

I thought that if no global specifier 'g' was used, then it was only supposed to do one replacement. For example, I thought

:%s/xyz//g           ' replace every instance of "xyz"
:%s/xyz//            ' replace only first instance of "xyz"

However, when I do the second, every instance is deleted. So, why is there even a 'g' specifier at all? And how do only delete one instance?

1
  • Do you have still something open in your question? Otherwise you could accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Oct 4 at 5:43

2 Answers 2

2

replace every instance of "xyz"

is really:

replace every instance of "xyz" on each line in the given range

and:

replace only first instance of "xyz"

is really:

replace only first instance of "xyz" on each line in the given range

Suppose this is the current line:

Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
  • :s/dolor/XXX will substitute only the first dolor on the line with XXX:

    Lorem ipsum XXX sit amet. Lorem ipsum dolor sit amet.
                ^^^ 1st                   ^^^^^ 2nd, no substitution
    
  • :s/dolor/XXX/g will substitute every dolor on the line with XXX:

    Lorem ipsum XXX sit amet. Lorem ipsum XXX sit amet.
                ^^^ 1st                   ^^^ 2nd
    

Adding a range, so that the substitution is done on several lines, doesn't change the meaning of /g.

  • :%s/dolor/XXX will substitute only the first dolor on each line of the given range with XXX:

    Lorem ipsum XXX sit amet. Lorem ipsum dolor sit amet.
    Lorem ipsum XXX sit amet. Lorem ipsum dolor sit amet.
    Lorem ipsum XXX sit amet. Lorem ipsum dolor sit amet.
    Lorem ipsum XXX sit amet. Lorem ipsum dolor sit amet.
                ^^^ 1st                   ^^^^^ 2nd, no substitution
    
  • :%s/dolor/XXX/g will substitute every dolor on each line of the given range with XXX:

    Lorem ipsum XXX sit amet. Lorem ipsum XXX sit amet.
    Lorem ipsum XXX sit amet. Lorem ipsum XXX sit amet.
    Lorem ipsum XXX sit amet. Lorem ipsum XXX sit amet.
    Lorem ipsum XXX sit amet. Lorem ipsum XXX sit amet.
                ^^^ 1st                   ^^^ 2nd
    

    Which is probably what you imagine when you think "global". /g is not "/g[lobal]", it is just /g.

But why do I go through all that hassle when :help :s_g unambiguously says:

[g] Replace all occurrences in the line.  Without this argument,
    replacement occurs only for the first occurrence in each line.  If
    the 'edcompatible' option is on, Vim remembers this flag and toggles
    it each time you use it, but resets it when you give a new search
    pattern.  If the 'gdefault' option is on, this flag is on by default
    and the [g] argument switches it off.
0

There are two kinds of 'specifiers':

  • The lines specifier aka range specifier (in your case % for all lines)
  • The line specifier (in your case g for all the occurences within the line)

If you want to only replace the next occurrence maybe you could be interested in the c flag to confirm:

:%s/xyz//c
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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