1

Suppose I have \frac{stuff}{matter} somewhere in a file I am editing. And I want to change stuff and matter. So at the trigger of a command, I'd like to see the following take place:

stuff gets highlighted in red (say) and matter gets highlighted in blue.

This will help me keep track of whether I am editing away at stuff or matter.

Then, of course, I'd like to trigger a command which disables the highlights.

How can I make this happen?

  • I don't understand your question. But at the very basic level, it seems that you want to highlight codes inside braces, but with different colors rather than one. That is possible, though I myself don't know how to do it. I say because of rainbow parenthesis in vim for lisp programs. – klaus May 12 '18 at 7:05
  • Yeah I just need to color things inside braces which follow a certain phrase, like frac. Can you please elaborate on rainbow parentheses? – caffeinemachine May 12 '18 at 8:21
2

You're basically talking about syntax highlighting, a relatively deep topic. In simplest terms, you define syntax matches through patterns and link those matches to highlight groups which define text color.

Just about everything you need to know is in :h syntax. You might want to start with a gentler introduction, though. This is one (though perhaps not ideal...I just have it handy): Basic Syntax Highlighting

I'll admit that I'm not that well versed in this stuff (I keep meaning to spend the time needed to learn it!) And, unfortunately, yours is not a trivial case because you have different highlighting on the same line. That being said here's my crack at it...

syntax region rFrac start="\frac" end="$" contains=rStuff,rMatter keepend
syntax region rStuff start="{stuff" end="}" contains=rMatter contained keepend
syntax region rMatter start="{matter" end="}" contained
" arbitrarily chosen highlight groups...Error is usually red. :)
highlight link rStuff Error
highlight link rMatter Keyword

Note: I am taking your example literally. If you actually meant "stuff" to be a substitute for arbitrary text then modify the pattern(s) (i.e. the start value) to fit whatever you have in mind.

Just run these through the command line to test it out. Works for me...mostly. :) It's possible there may be "false positives"...that is, coloring text that should not be colored. (But don't worry...if there's a superior solution someone will post it soon.)

Now once you get this working we can get into some commands to quickly turn it on or off. That's not a problem.

  • Thanks for answering. Yes, I intended for stuff to be arbitrary things. – caffeinemachine May 12 '18 at 8:40
  • You could try start="{[^}]*" in rStuff, for starters. – B Layer May 12 '18 at 8:43
  • Thanks. Let me squander an unconscionable amount of time on it to get this right. :) – caffeinemachine May 12 '18 at 8:44
  • Hehe. Sure. Good luck. I poke my head in a fair amount if you get stuck on something. (Though, to repeat, I'm not an expert on this stuff!) – B Layer May 12 '18 at 8:45
  • This might be a stupid question. I copied this in my vimrc. The highlights are not taking effect. Is there a trigger command hidden in your code? – caffeinemachine May 12 '18 at 8:55

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.