Vim: use different color for words with a pattern - vim

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.

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.

How to get vim spell check to mark bad words inside of Latex math equations?

Spell check in vim is nice enough to not highlight things inside of a math environment when writing LaTeX. This is convenient, but I make some very common typos like "theat" instead of "theta" that would be nice to catch automatically. Is there a way to get the words in the explicit list of 'bad words' to be highlighted regardless of their context?
Note that I still don't want to check whether everything is good within a math environment, just that it is not explicitly bad.
To enable spell checking for a syntax group, you'd have to add contains=#Spell, e.g. for the texStatement group, that would be:
:syn match texStatement "\\\a\+" contains=#Spell
But with that, you'd still have to add all "good" statements to your spell file. If you just want to highlight certain "bad" words, you can define a contained match:
:syn match texBadStatement "theat" containedin=texStatement
And then link to the error or bad spell highlighting:
:hi link texBadStatement SpellBad
Put those commands into ~/.vim/after/syntax/tex.vim to make them persistent.
I'm not a Latex specialist. If the texStatement group is wrong, you need 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.

Adding syntax highlighting for latex plugin in 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/

Resources