1

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.

5

You can use repeat function with replace expression:

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

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

3
  • 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?
    – John Smith
    Sep 12 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
    Sep 12 at 22:38
  • 1
    For sure, will do that going forward
    – John Smith
    Sep 13 at 15:18

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.