Adding syntax highlighting for latex plugin in vim - vim

I am using a package called outlines for LaTeX. It adds commands such as \1 \2 \3 etc.
They are not highlighted by default in vim. So, I created a file called tex.vim in my .vimrc/syntax folder, and put this in the file:
:syn match outline /\\[1-9]/
hi link outline Label
This works only at the top level, not within a block. In other words, it works before my \begin{document}, but not between \begin{document} and \end{document}.
This is pretty much useless. How can I get vim to recognize the syntax, regardless of where it appears in the document?

You need to find the syntax group or cluster defined by the Tex syntax, and use contained containedin=..., but in your case, there is already a syntax group for statements, it's just that it doesn't include numbers. Therefore, you can just piggyback on the existing group and only add matching for numbers:
:syn match texStatement /\\\d/

Related

Vim recipe for a minor syntax highlighting extension

I want to leave my system's Python syntax highlighting mostly intact, but I have a specific pattern I'd like to highlight for an idiom I use a lot. How can I add additional highlighting instructions on top of the existing highlighting done by vim?
(Apologies if this has already been asked. All the vim syntax highlighting questions I found seemed to involve writing a new syntax highlighting from scratch.)
Put your additional :syntax commands into ~/.vim/after/syntax/python.vim, and they will be automatically executed after the original syntax script.
It's easy to highlight stuff that so far isn't parsed at all.
For elements already parsed / hightlighted, you need to find out by which syntax group (e.g. pythonFunction), and add a containedin=pythonFunction clause to your :syntax commands. Without that, the original matching will obscure yours. To find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.
Introducing highlighting across (larger) elements that have multiple existing syntax groups is difficult, as your match will obscure the original ones, and that may break the entire parsing. You need to carefully examine the existing nested element structure, and try to fit in yours, again via contains= and containedin= clauses. Depending on the actual situation, that can be difficult.
For the actual syntax definitions, see the help starting at :h :syn-keyword. Basically, there are simple keyword definitions, regular expression matches, and regions defined by start and end patterns.

error when defining a new vim syntax

Why can't this line correctly highlight all things between (* and *) as comments in vim?
syn region datsComment start="(\*" end="\*)" contains=datsComment,datsTodo
hi def link datsComment Comment
It does for me (in a fresh buffer without other syntax definitions). You probably have other syntax elements there that prevent a match.
You need to find out which syntax group causes that. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin. If you find other syntax groups obscuring the match, you probably should include them in the contains=datsComment,datsTodo,... part.

Vim: use different color for words with a pattern

Am a hardware engineer and I use Embedded Ruby Language to simplify writing my hardware verilog/system verilog code. In my *.sv and *.v files, i have a lot of ERB variables starting with "__" (double underscore). E.g. <% __MEM_DEPTH = 64 %>. Is there any way by which I can make vim display the words starting with the double underscore in a different color?
You can extend the built-in syntax highlighting. For example, put the following into ~/.vim/after/syntax/verilog.vim:
syntax match verilogErbVar "\<__\w\+\>"
hi link verilogErbVar Identifier
This assumes that the corresponding text fragments aren't yet matched by the original syntax (in my short test, they weren't). Else, you need to find the syntax groups that contain them and add a containedin=... to the :syntax command.
To find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.

vim syntax scripts "sourcing" another, but only for matching lines

I'm writing a vim syntax script and I want to be able to make lines matching a certain pattern, say, '^>', "source" or imitate the markdown syntax highlighting.
Is there a way to do this at the syntax script level? Do I need to just copy and paste it in manually and make the proper adjustments? Does this require a modeline on the actual file?
Thanks!
Have a look at :help :syn-include. It allows you to import an existing syntax (like e.g. markdown) into a syntax cluster in your own syntax, and then you can assign syntax regions (if I understand you correctly, that would be a region starting with /^>/ and ending at the end of the line /$/) to it.
Note that success isn't guaranteed; you need some collaboration from the included syntax. (For example, if the markdown syntax anchors its patterns at ^, but now it's included behind the > prefix, it won't match any more.) In the worst case, you have to modify the included syntax or copy it completely into your own syntax.

highlight sub-match in vim

I'm trying to figure out how to highlight a specific portion of a match in vim.
Given the following example rule (taken from the coffeescript syntax file source):
syn match coffeeExtendedOp /\%(\S\s*\)\#<=[+\-*/%&|\^=!<>?.]\+\|[-=]>\|--\|++\|:/ display
This regular expression matches various coffeescript operators. The operators are highlighted (in my vimrc) like this:
hi Operator guifg=#ff0000
For example, since coffeeExtendedOp is linked to coffeeOperator which is linked to Operator, in the above source file. This all works, but I'm wondering how to specifically highlight the ++ operator matched in the above syn match with a different color, say blue, within my vimrc (that is, without altering the original source file above). I'm simply wondering if this is possible.
EDIT: I think the rules are placed under a cluster, so perhaps that's why it's not affecting anything. Is there a way to access the rule within the cluster?
EDIT: Question was clarified.
Solution:
syn match plusplus /++/ contained containedin=coffeeExtendedOp display
hi plusplus guifg=#0000ff
The problem now is that this only works when I run them as commands in vim, but not when I put it in my vimrc file. Any ideas? Could it be that the stuff is hidden behind the cluster? But then why is it visible in vim through a command? I tried including the syntax file but it didn't seem to have any effect.
Looking at the coffee.vim you linked to it seems like the dot belongs to the coffeeDotAccess syntax item. So you can highlight it just by doing this:
:hi coffeeDotAccess ctermfg=blue
I'm going to guess a bit at what you need. (I don't speak Coffeescript and your sample regex is way too complicated for me to start reading at the moment).
Transparent syntax items
You could have a look at transparent syntax rules: (http://vimdoc.sourceforge.net/htmldoc/usr_44.html)
In a C language file you would like to highlight the () text after a "while"
differently from the () text after a "for". In both of these there can be
nested () items, which should be highlighted in the same way. You must make
sure the () highlighting stops at the matching ). This is one way to do this:
:syntax region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/
\ contains=cCondNest
:syntax region cFor matchgroup=cFor start=/for\s*(/ end=/)/
\ contains=cCondNest
:syntax region cCondNest start=/(/ end=/)/ contained transparent
Partial matches in regex
If you really just meant highlighting submatches, have a look at the the
\zs start match
\ze end match
In short,
:match Error /foo\zsbar\zered/
would highlight only 'bar' in 'foobarred'

Resources