4

I'm using (mac)vim with tex-suite and would like to have a single regex command (or any other way) to do the following thing:

Change

\textcolor{green}{some random text}

into

some random text

This should be done for all occurrences of \textcolor{green}{} in my tex file...

Any idea?

EDIT: I need it to recognize matching braces. Here an example :

\textcolor{green}{
     with $v_\text{F}\sim10^6$m.s$^{-1}$ the massless Dirac fermions 
     velocity in pristine graphene}.
sehe
  • 374,641
  • 47
  • 450
  • 633
Nigu
  • 2,025
  • 2
  • 22
  • 31
  • It isn't very clear _which_ braces can be parentheses. Care to show examples? – sehe Dec 15 '11 at 10:22
  • @sehe. Sorry, I shouldn't have use the word parentheses at all. In my case, it is all braces. But I wanted it to be general. I'll update the question with an example of nested braces. – Nigu Dec 15 '11 at 12:16
  • I just tested with the edited version and I can confirm it works well. – sehe Dec 15 '11 at 12:21

2 Answers2

5

In my experience, things like this most often crop up during editing, and you might have the search for \textcolor{green}{ already highlighted.

In such a scenario, :global is usually my weapon of choice:

:g//norm d%diBvaBp

diBvaBp: diB (delete inner block), vaB (select block), p (put)

If you have surround.vim installed (recommend it!) you could remove the pair of braces simply doing dsB (delete surrounding {})

:g//norm d%dsB

Of course, you can combine it like

:g/\\textcolor{green}{/norm d%dsB

I just noted a potential issue when the target patterns don't start at the beginning of a line. The simplest way to get around that is

:g//norm nNd%diBvaBp

A more involved way (possibly less efficient) would be using a macro:

/\\textcolor{green}{
gg
qqd%diBvaBpnq

Followed by something like 100@q to repeat the macro

sehe
  • 374,641
  • 47
  • 450
  • 633
  • @Nigu found a caveat and proposed workarounds/fixes. The macro approach is usually slower, but that might not be of interest – sehe Dec 15 '11 at 12:31
  • @sehe. There is still an issue when I have different occurrences of `\textcolor{green}{` on the same line... But I'll accept your answer, there may be even other caveats that will spur up, so the yield may not be worth the trouble. Thanks a lot anyway! – Nigu Dec 15 '11 at 13:08
  • @Nigu: I was aware of that remaining quirk. Just run the command twice, or go with the macro variant. Btw, I have given this real thought and I don't expect anything else to pop up. If this is for one-off edits, I'd recomment it. Otherwise, you're better of using an existing parsing for the syntax you're editing – sehe Dec 15 '11 at 13:36
1
 :%s,\\textcolor{green}{\([^}]\+\)},\1,g

Updated as per your updated question:

:%s,\\textcolor{green},\r-HUUHAA-&,g
:g/\\textcolor{green}/normal 0f\df}lvi{xhP$xx
:%s/\n-HUUHAA-//

Quick explanation of how it works:

  1. Put all \textcolor{green} lines onto a line of their own, with 'special' marker -HUUHAA-
  2. Use visual selection vi{ to select everything in between the {}, paste it outside and delete the now empty {}.
  3. Delete leftover stuff including the marker.
holygeek
  • 15,653
  • 1
  • 40
  • 50
  • Thanks. This works for most cases, but not if I have \textcolor{green}{some random text{some other random text}still some more random text}. That is, if I have nested braces, it doesn't work. I'll update the question to say I need it to recognize matching braces (or parentheses). – Nigu Dec 15 '11 at 10:10
  • @Nigu: my version _does_ correctly handle nested braces/parentheses. Provided, they are balanced, of course – sehe Dec 15 '11 at 10:23