When using :s, how do I multiply the string being written by an integer, to write N copies of it?

For example, :s/pattern/10*\r/ would write 10 carriage returns in each match.

1 Answer 1


You can use repeat function with replace expression:

%s/\n\_s*$/\=repeat("\r", 10)

Check :h sub-replace-expression and :h repeat() for details.

  • Thanks. Is the final enclosing slash omitted because it's always optional in the :s command or because it's not used for the repeat function? Commented Sep 12, 2021 at 19:21
  • 2
    The former—its optional in vim (maybe ex/vi, even ed?? It’s not optional in sed) when you provide no flags for :substitute
    – D. Ben Knoble
    Commented Sep 12, 2021 at 22:38
  • 1
    For sure, will do that going forward Commented Sep 13, 2021 at 15:18

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.