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:

\begin{equation} \end{equation}
\[ \]
$ $

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?

  • 1
    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
  • 2
    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


This will look for x between \begin{equation} and \end{equation} 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.