20

& repeats the last :s

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

:s/"/'/g

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"]

produces

['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

22

This is exactly what :&& does:

                            *:&&*
[&] Must be the first one: Keep the flags from the previous substitute
    command.  Examples: >
        :&&
        :s/this/that/&
<   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&*
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>
3
  • 4
    Remember that if you already accidentally pressed &, it becomes the last substitution and the flags are gone
    – doraemon
    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&. Nov 29, 2018 at 8:16
  • 5
    I also think that the answers should be full sentences. Nov 29, 2018 at 8:16
0

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

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

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

:.,+1s/"/'/g

Or from start of file to end of file:

:1,$s/"/'/g

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

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

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.