& repeats the last :s

But it doesn't repeat with flags, e.g. a global sub:


won't replace in the same way on consecutive lines with &, running :s/"/'/g on the first line here and then & on the second

["first", "second"]
["third", "fourth"]


['first', 'second']
['third", "fourth"]

the global option has been forgotten about when using & on the second line

How can I quickly repeat last substitute command with flags?

(not @: because I may have other commands run after :s)

2 Answers 2


This is exactly what :&& does:

[&] Must be the first one: Keep the flags from the previous substitute
    command.  Examples: >
<   Note that `:s` and `:&` don't keep the flags.

There are also the & and g& commands:

&           Synonym for `:s` (repeat last substitute).  Note
            that the flags are not remembered, thus it might
            actually work differently.  You can use `:&&` to keep
            the flags.

g&          Synonym for `:%s//~/&` (repeat last substitute with
            last search pattern on all lines with the same flags).
            For example, when you first do a substitution with
            `:s/pattern/repl/flags` and then `/search` for
            something else, `g&` will do `:%s/search/repl/flags`.
            Mnemonic: global substitute.

And you could map & to be more like :&&:

nnoremap & :&&<CR>
xnoremap & :&&<CR>
  • 4
    Remember that if you already accidentally pressed &, it becomes the last substitution and the flags are gone
    – doraemon
    Commented Nov 29, 2018 at 4:09
  • 5
    This is correct, but if you add more info the answer will be better. I propose to guide the reader to the relevant help sections, where this is explained immediately. E.g. :help :&. This may also guide readers to learn about things like the mappings & and g&. Commented Nov 29, 2018 at 8:16
  • 5
    I also think that the answers should be full sentences. Commented Nov 29, 2018 at 8:16

I usually use a range operation for substitutions. From the example:

["first", "second"]
["third", "fourth"]

Replacing " with ', while cursor is on first line:


Or from start of file to end of file:


. = current line of file, $ = last line of file, +x = delta line (positive offset), -x = delta line (negative offset), g = globally (all occurrences on line)

  • Welcome to Vi and Vim! I think by "context" you mean a range (:help [range])?
    – D. Ben Knoble
    Commented May 12, 2021 at 14:53
  • Thanks for the suggested. I changed the post
    – RexBarker
    Commented May 21, 2021 at 9:53
  • 1
    Pro-tip: % is the same range as 1,$ :)
    – D. Ben Knoble
    Commented May 21, 2021 at 12:55

Your Answer

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

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