regular expression - remove space in front of pattern

Viewed 63

I have text as

Bla ~ \cite{} and Bla1 ~ \ref{} and Equation in ~ \eqref{}.

and I would like to remove spaces between ~ and \. Thus above line, should look like:

Bla ~\cite{} and Bla1 ~\ref{} and Equation in ~\eqref{}.

I tried following reg. expression %s:\(\|~\):(~\):gce

but it replaces replaces empty space with ~. Any suggestions for fix?

4 Answers

I just verified in vim and you only have to make sure to disable the special meaning of ~ in both the pattern and the replacement:

:%s/\~ \\/\~\\/g

Will replace ~ \ everywhere with ~\ without space.

You can use

:%s/\v(\~)\s+(\\)/\1\2/g

Here,

  • \v enables very magic mode
  • (\~) - Group 1: ~ char
  • \s+ - one or more whitespaces
  • (\\) - Group 2: a \ char
  • \1\2 - the replacement is Group 1 + Group 2 values
  • g - multiple occurrences.

Using vim start match \zs

:%s/\~\zs //g

This means that vim will consider everything after ~ as it patter for substitution, in our case the space. As we are using the g flag, vim will change all occurrences in the line.

Using vim start match \zs

:%s/\~\zs //g

This means that vim will consider everything after ~ as it patter for substitution, in our case the space.

Related