# Character in math mode replacement

I would like to replace instances of a single character variable, say x, throughout all my equations in a latex file, say the replacement is \x.

The equations are denoted by one of the following:


$$
$$


I could use :%s,x,\\x,gc but then I would have to confirm all instances of x that are not in math mode.

Is there any way to bound the range to be inside math modes only?

• vi.stackexchange.com/questions/4050/… might of use (it's just search, but it should be possible to adapt the solutions for replace as well) – muru Apr 10 '17 at 1:13
• If you are on a posix system I'd suggest to use an external filter like 1GVG!sed '/begin{equation}/,/end{equation}/s/x/\\x/g'. I believe it's okay to different tools for different purposes. – Philippos Apr 10 '17 at 5:41

:g/\$$/,/$/extract_tex]/s/x/\\x/g This will look for x between and and will substitute all instances of x with \x. To look for the pattern x inside \[$ and , replace the matching pattern and do a search replace again. If you want all the search instances in one line, use | to separate two search expressions.