Question: 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 Principle](https://link...)and 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?**

Answer: 
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