vim highlighting: remove syntax keyword - vim

One of the vim plugins I use does:
syn keyword rustTodo contained TODO FIXME XXX NB NOTE
Resulting in highlighting NB in comments, which I don't like. Is there a way to either redefine the keywords or remove one from them? Looking at :help syn-keyword makes me think that this is not possible.

If that is the only definition for rustTodo (these are cummulative), you can remove and then redefine it:
syn clear rustTodo
syn keyword rustTodo contained TODO FIXME XXX NOTE
Unfortunately, the granularity for removal of syntax items is limited to whole syntax groups (here : rustTodo); you cannot pick individual keywords unless they also have separate groups (which would result in much more linking of highlight groups and therefore inefficient).
To make this permanent, put it into ~/.vim/after/syntax/rust.vim
If you think that the majority of users doesn't like NB here, please suggest to the author to drop it. Adding it back as a personal customization is easier and more maintainable than removing it...

I had a similar question while importing syntax files from within other syntax files, and came up with a dirty hack.
Instead of loading your syntax file with
syn include #othersyntax syntax/othersyntax.vim
you can use
syn include #othersyntax !sed 's/<NB>//' syntax/othersyntax.vim
Eww. This brings serious platform dependency and security issues, but at least relieves you of the burden to keep the replacement keyword list in sync with upstream.

Related

Why is TODO always highlighted in vim?

I was going through a bash file and I noticed that TODO was written twice in the file, and both the instances were highlighted. Why's this? Is it because TODOs are very significant and might avert wastage of time for developers? What's the history behind this?
This is just a syntax element which may be defined (or not) for this particular buffer.
Typically syntax is defined in $VIMRUNTIME/syntax/[filetype].vim. The relevant command could look like this, for example:
syn keyword cTodo contained TODO FIXME XXX
Whether such definition is useful or not is subjective, I admit.
You can see all syntax elements defined for the current buffer with :syntax command.

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.

Allowing syntax highlighting of kramdown footnotes while preventing highlighting in code blocks in vim

vim's default syntax highlighting for markdown is based on vanilla markdown, which considers every tab or four-space indented line to be the beginning of a code block. From markdown.vim:
syn region markdownCodeBlock start=" \|\t" end="$" contained
However, I use kramdown, which allows for footnotes (as do some others, such as Pandoc, Markdown Extra, and MultiMarkdown). I've come up with this rule modification to allow for syntax highlighting and spell checking within footnotes but not code blocks:
syn region markdownCodeBlock start="\(\[\^.*\]:\n\)\#<! \|\t" end="$" contained
It's less than perfect, however, because it doesn't allow footnotes to start on the same line as their instantiation, doesn't allow for code blocks inside footnotes, and doesn't allow for multi-paragraph footnotes. Any suggestions for how to do this better?
You can use vim-pandoc. It doesn't correctly highlight code inside footnotes, but addresses the rest of those issues. Unfortunately, it doesn't work great with some other less common parts of kramdown syntax (such as tables), so it's not a perfect fix.
Its default setting uses vim's conceal feature extensively; if you don't like this, you can turn it off:
:set cole=0

removing spell syntax highlighting in vim

This question is a sequel question of this one. I have the following script that removes capitalized words from vim spell check.
syn match myExCapitalWords +\<\w*[A-Z]\K*\>+ contains=#NoSpell
But it works only if I do syn clear first. But then all other highlighting (e.g. markdown) gets lost. I went through syn list to see what might be causing the conflict, but now I am out of clue.
It looks like you're extending arbitrary syntaxes with your myExCapitalWords group. Whether / in which syntax items that works depends on the underlying syntax. Unfortunately, it is not possible to extend arbitrary syntaxes in a blanket way. That's why you're seeing problems that can only be solved via :syn clear (which gets rid of the underlying syntax).
A syntax contains multiple groups, some of which are usually contained= in others. If you introduce a new syntax, it will only apply to where no other syntax group already matches. You can force your group into others via containedin=TOP or even containedin=ALL, but that overlay may prevent other original groups from matching, and causes strange effects because their own contains= or nextgroup= now don't apply.
So, unfortunately, there's no general solution for this. If you're only interested in a few syntaxes, you can tweak your one-liner to make it cooperate with the underlying syntax (e.g. try containedin={syntaxName}Comment{s}), but there's no generally applicable solution.

syntax highlighting for Assembler

I need to add support for assembler language I'm working with (it is not x86, 68K, or 8051 which are well supported by vim). I looked at the existing syntax files, and here are my questions
1) When does it really make sense to use syn keyword and syn match? My understanding is that the latter supports regex and gives more flexibility. On the other way, looking at /usr/share/vim/vim70/syntax/asmh8300.vim - they define opcodes in both keyword and match, what benefit does it really give?
2) Instructions in my Asm have a common format:
INSTR OP1, OP2 ..; i.e. space delimits the instruction name from operands.
I think for this I'm ok with only defining all Asm commands in 'keyword' since space symbol is by default in 'iskeyword'. Am I right ?
3) The Asm also supports C-style structures, enums and comments. Can I just borrow its syntax definition from c.vim or it won't work and requires some tweaking?
If you have a limited set of identifiers, and if they all consist solely of keyword characters, then :syn keyword is the best choice. You're right in that :syn match provides a superset of functionality. Essentially,
:syn keyword myGroup foobar
is equivalent to
:syn match myGroup "\<foobar\>"
Beware of old versions
The syntax/asmh8300.vim syntax you've referenced is from 2002, it may not be the best example of how to write a syntax file. (For example, it omits the \<...\> around its matches, what looks like a bug to me. And it still has compatibility stuff for Vim 5 / 6 that's not needed any more.)
Also, do you actually use Vim 7.0?! Vim 7.0 is from 2007 and very outdated. It should be possible to install the latest version 7.3; if you can't find a proper package for your distribution (for Windows, check the binaries from the Cream project, it's also not very difficult to compile (e.g. from the Mercurial sources) on Linux.
Borrow other syntax elements
If other syntaxes are embedded in your syntax, and clearly delimited (e.g. like JavaScript inside HTML), you can :syn include it. But if there are just similar constructs, it's best to copy-and-paste them into your syntax (and adapt at least the group names). You need to be careful to catch all contained syntax groups, too; together with syntax clusters, the hierarchy can be quite complex!
More tips
When writing a syntax, you often 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.

Resources