For the URLs, I have the pattern \w\+:\/\/[^[:space:]]\+
, but I cannot figure out what to put in containedin
. The best I came up with is containedin=bibQuote,bibBrace
, that works but it breaks syntax highlighting for everything that comes after the URL.
This pattern actually works, except in cases where the URL is at the end of the field value and ends up eating the ending }
or "
of the containedin
group.
You can easily fix this by making your URL pattern more restrictive in what it accepts. For example, simply disallowing these two characters should be enough to solve the problem: \w+://[^[:space:]}"]+
. (Of course, you can decide to be even more restrictive in what you accept in a URL, as long as you don't include those two characters, which should be encoded in valid URLs anyways.)
P.S. I would like to disable spell checking for the author
and journal
fields as well.
I have a suggestion, not exactly for what you asked here, but it might solve your problem with spelling of BibTeX files.
Instead of deciding what not to spell, you could do the opposite and specify only the parts that you actually want to spell.
For example, you might decide to only spell the contents of fields title
(also booktitle
, which ends in the same suffix), type
and note
.
In that case, you can use the following pair of rules:
syntax region bibSpellBrace
\ start=/\(\(title\|type\|note\)\s*=\s*\)\@<={/
\ end=/\ze}/ skip=/\(\\[{}]\)/
\ contained containedin=bibBrace
\ contains=@Spell
syntax region bibSpellQuote
\ start=/\(\(title\|type\|note\)\s*=\s*\)\@<="/
\ end=/\ze"/ skip=/\(\\"\)/
\ contained containedin=bibQuote
\ contains=@Spell
Add these to a file such as ~/.vim/after/syntax/bib.vim
to load the rules automatically (after loading the default syntax rules) when editing BibTeX files.
The rules above mirror the bibBrace
and bibQuote
rules from the default BibTeX syntax, however:
- They use a zero-width look-behind match to check on the field name, so the rules only get enabled on the specified fields. See
:help /\@<=
. Note that using \zs
doesn't work here, since those tokens (with the field name and quote or brace) have already been consumed. The zero-width look-behind still works fine here. (For a more detailed explanation, see question “Syntax highlighting with multiple matches with \zs
”.)
- They use a
\ze
on the ending pattern, to avoid consuming the closing brace or quote and let the container bibBrace
or bibQuote
see those.
- They use
containedin
to insert themselves in the proper location of the syntax, specifically inside bibBrace
or bibQuote
.
Note that since the syntax for BibTeX uses syntax spell default
(which is the default behavior, see :help :syn-spell
for details), that means that as soon as you mention @Spell
in a single rule of your syntax (such as the two above do), Vim will only spell check the contents of the matching groups.
This behavior is the same as the explicit syntax spell notoplevel
, but it turns out the explicit setting is not really needed here, since the default behavior already detects we're explicitly listing the only bits we want to be checked for spelling.
hi link _key bibQuote
to get syntax highlighting back