3

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?

muru
  • 24,838
  • 8
  • 82
  • 143
Three Diag
  • 205
  • 1
  • 5
  • 1
    http://vi.stackexchange.com/questions/4050/how-to-search-for-pattern-in-certain-syntax-regions 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 01: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 05:41

1 Answers1

2

:g/\\begin{equation}/,/\\end{equation}/s/x/\\x/g

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.

Sat Yam
  • 144
  • 5