6

I would like to disable spell checking for the URLs in my BibTeX (.bib) files. I was able to disable spell checking for the citation keys (author-year or author+year) as follows:

autocmd FileType bib
\ syntax match _key '\w\+[-+]\d\d\d\d'
\ containedin=bibKey
\ contains=@NoSpell

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.

P.S. I would like to disable spell checking for the author and journal fields as well.

4
  • 2
    Welcome to Vi and Vim! I wonder if it would be simpler to only enable spell in title/abstract/notes fields? I don't have spell set in bib files at all, but I also don't know the syntax files for bib very well.
    – D. Ben Knoble
    Apr 16, 2021 at 17:26
  • @D.BenKnoble I agree, that would be great. Apr 16, 2021 at 18:37
  • Oof, the bib syntax file only really defines keys and fields; there's no way to target specific ones, AFAICT, so I'm not sure I can figure this out
    – D. Ben Knoble
    Apr 17, 2021 at 16:38
  • You might be able to hi link _key bibQuote to get syntax highlighting back Apr 18, 2021 at 12:42

1 Answer 1

7
+50

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:

  1. 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.)
  2. 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.
  3. 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.

2
  • 1
    Wow. A single touch of a truly expert mind and everything works as expected. Abracadabra. On top of that, you explained everything with utmost clarity so that I will not struggle with a similar problem again. Thank you so much for taking time to help me! Apr 21, 2021 at 7:39
  • 1
    Happy to help and glad that it seems that this helped you!
    – filbranden
    Apr 21, 2021 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.