Substitute in Vim doesn't seem to work globally?

macvim, vim

Solution

The `g` flag is to substitute the pattern multiple times per line.

What you're looking for is running the `s///` command for the entire buffer:

:%s/pattern/replacement/g

Notice the `%` in front of `s///`, to select the entire buffer. It could also be a range of lines like this:

:10,21s/pattern/replacement/g

to perform the replacement between lines 10 and 21.

Without the `g` flag there, only the first occurrence of pattern will be replaced per line.

Problem

The `global` option in my `substitute` doesn't seem to work. Even when I do substitute with `g` set it substitutes the text only in the current line. If I run it again while staying on the current line which doesn't have any more matches it gives me E486: Pattern not found: {patt} If I go to the next match and run it again it works fine for that line. But there is literally no difference in using `g` and not using it. Any ideas on what's wrong? ``` printf("abc"); printf("def"); ``` Doing `:s/printf/print/g` highlights both the `printf's` but only the replaces the first one. I need to do `n` to go the next match and rerun substitute.

Original source