11

I edit quite a few BibTeX files, and I regularly forget the trailing commas:

For example:

@Book{cooke2015british,
 author = {Cooke, Lez},
 title = {British Television Drama},
 publisher = {Palgrave on behalf of the British Film Institute},
 year = {2015},
 keywords = {about}
 address = {London},
 isbn = {978-1844576234}
 }

You'll see that I forgot to add the comma after keywords = {about}, which will mean that I have to spend time debugging. Is there a way Vim can make me remember? By the way, I have the bib_autocomp.vim plugin ...

2 Answers2

6

Quick and dirty solution:

:match Error /^\s\+.*,\@<!\ze\n\s\+\w/

You could also create a special syntax for BibTeX files using the same pattern.

Some explanations on the used regular expression:

  1. ^\s\+ searches for indented keywords.
  2. \ze stops the pattern matching such that the next line is not marked.
  3. ,\@<!\n searches for lines not ending with a comma.
  4. \n\s\+\w guarantees that there is a new entry after the currently being scanned.

The same pattern can be used to jump between erroneous lines using vimgrep as following:

:vimgrep /^\s\+.*,\@<!\ze\n\s\+\w/ %

If the quickfix window does not open automatically, it can be opened using :copen. And :cnext and :cprevious can be used to jump between errors.

Vitor
  • 1,742
  • 10
  • 15
0

One possibility is defining a .vimrc shortcut to add the missing ,

Example:

:map \q  :%s/\v([}"])(\s*\n\s*\S+\s*\=)/\1,\2/<CR>

and use \q before leaving or when debugging.

JJoao
  • 561
  • 4
  • 9