4

How can I substitute all except the last asterisk in groups of two or more asterisks with two spaces? E.g.

* Micro topic 1
** Microbes are small
** You can't see them!
*** Isn't that something?

Becomes

* Micro topic 1
  * Microbes are small
  * You can't see them!
    * Isn't that something?

Is there a way to modify of /^\*\{2,} to match all but the last asterisk in the group and then perform the substitution?

Or :g/^\*\{2,}/s/\*/ with a range, so that it will subsitute each asterisk with two spaces, while leaving the last asterisk? E.g. *** becomes * (four spaces and an asterisk)

I tried :g/^\*\{2,}/s/\*\+/ to replace the asterisks with spaces, but it doesn't replace the asterisks individually.

0

1 Answer 1

3

You can match "each asterisk that is followed by an asterisk". This is true for all asterisks but the last one. You can then substitute this for two spaces. One way to express that pattern is by ending the match after a single asterisk, but including a trailing asterisk in the pattern:

%s/\*\ze\*/  /g

See :help /\ze for information about the end-of-match pattern atom.

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.