My Problem

I am editing a markdown file with some links in a [link text](url) format.

Some of these links lack a space after the closing parens, e.g.:

...fit the [Single Responsibility Principle](https://link...)and can ...

Which renders to a missing space after the link:

...fit the Single Responsibility Principleand can ...

I am trying to build a regex search-replace to add a missing space after the closing parens where necessary.

What have I (naively) tried

Tried search-replacing with a regex:

%s#\)(\S)#\) (\1)#gc

%s#\)                 - Closing parens
%s#\)(\S)             - Followed by a non-space
%s#\)(\S)#            - Replaced with...
%s#\)(\S)#\)          - The closing parens followed by a space
%s#\)(\S)#\) (\1)#gc  - And the original character

And, of course, Googled the problem.

My question

How can I add a space after every closing parens that has a non-space character just after it?

  • Repeats (:help q) might offer an alternative approach which work well if you are just looking for a one-shot/"not going to need this fix again" solution. In this case I would record the commands that fix the space and go to the next ). Then at each occurence of ) I would either use the recorded commands or skip to the next ) character (with a simple search for )). Nov 6, 2018 at 13:02

3 Answers 3


What about

%s#)\zs\ze\S# #g

By default unless you change the 'magic' option, a closing parenthesis is matched by just ). BTW, here I've cheated with :h /\zs and :h /\ze to mark the in between characters that we want to expand with a single space.


You can use the following command:

:g/)\([^ ]\)/s//) \1/g

Where the [^ ] means any character except space, the \(\) surrounding it is to group it and reference it later in the substitution as \1. Thus, it effectively adds a space after a ) followed by any character except a space.

But the above command does not add a space if the ) is at the end of a line. To also add spaces to those too:

:g/)\([^ ]\|$\)/s//) \1/g

Where $ represents end of line and the \| adds it to the match-any-character-except-space as an "or"


Try these:

# match ) followed by zero or more spaces and replace with ) and space
s/) */) /

# match ) followed by optional space and replace with ) and space
s/) \?/) /

) is not escaped here, because by default ( and ) are not special

Add g flag if you need to replace multiple occurrences in a line

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.