3

:help change.txt mentions 2- or 3- letter :substitute commands, such as :sg, :sgn and so on. I am guessing that these :s[xx] correspond to flags, but am not sure how to use them.

For example, I would think that :%sg/foo/bar would substitute all matches of foo, including multiple occurences on the same line. However, that commands gives me an error --- "trailing characters".

What is the appropriate syntax for :s[x[x]]?

6

Actually the behavior of the substitution command :s if you don't provide any arguments is to repeat the last substitution you did. So if you have a line

foo Foo foF

And use :s/f/X/ you will get

Xoo Foo foF

Now if you use :s (without any argument) you will reapply the same command and get as a result:

Xoo Foo XoF

The 2 and 3 letters substitution commands work the same but adding some flags at the end of the original substitution. So now if you have the previous line and use :sgi you will get the equivalent of :s/f/X/gi which will result in

Xoo Xoo XoX

That explains why you get E488 Trailing characters the short substitutes commands don't take an argument as they only apply the last :s command.

0

statox has given a complete answer. To find this in the documentation, refer to :h :& or the paragraph after :h :s. Quoting from the help (emphasis mine):

:[range]s[ubstitute] [flags] [count]
:[range]&[&][flags] [count]

Repeat last :substitute with same search pattern and substitute string, but without the same flags. You may add [flags], see |:s_flags|.
Note that after :substitute the '&' flag can't be used, it's recognized as a pattern separator.
The space between :substitute and the 'c', 'g', 'i', 'I' and 'r' flags isn't required, but in scripts it's a good idea to keep it to avoid confusion.

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.