0

While writing in latex I often want to copy a math equation's right hand side or left hand side. For example I have something like this

\begin{gather*}
    5 \cdot \left( A A 3 \right) + 50 \cdot \left( A A 3 \right) = 6CAB \\
    500A + 50A + 15 + 5000A + 500A + 150 = 6000 + 100C + 10A + 5
\end{gather*}

and I'd like to copy the right and left hand side of the n-th last equation I've written and store them each in the l and r register respectively. For example if I were to use this on the 2nd last equation the l register would contain

5 \cdot \left( A A 3 \right) + 50 \cdot \left( A A 3 \right)

and the r register would contain

 6CAB \\

So far I have this regex which represents a line that has an = between text

[^=]*\s\+=\s\+[^;]*

But I have no clue on how to somehow input what it's matching into registers and how to find the n-th last equation either... I was wondering if anyone could help me figure this out and was also wondering if this should be a macro?

5

This can be done with either regex+substitute or macros

Substitute. This is the same as your regex except the important parts are surrounded by \( . \) to create capture groups. These are referred to by submatch(1) and submatch(2) respectively. We use the replace expression \= and execute('let') idiom. Finally, use /n to prevent substitution from actually happening.

s/\([^=]*\)\s\+=\s\+\([^;]*\)/\=execute('let [@l,@r]=[submatch(1),submatch(2)]')/n 

Macro. Create a macro as follows:

:let @q = '_"lyt=f=l"ry$'

Then type @q to execute the macro on the line. The parts are:

  1. _ go to start of line.
  2. "lyt= yank until = into register l.
  3. f=l go one past the =.
  4. "ry$ yank until end.

Command. Alternatively, this is straightforward to do using basic vimscript

:let [@l,@r] = split(getline('.'), '=')

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.