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 ...

1
  • Not that I know of, but you could try something like this to fix it: :%s/[^ ,]\zs\ze\n\s*[^ }]/,/. Commented Oct 22, 2015 at 14:31

2 Answers 2

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.

2
  • This is good! Can I somehow make it jump to the next error when I press n? Commented Oct 25, 2015 at 14:56
  • I've updated the answer to include what you asked for. See if it works for you.
    – Vitor
    Commented Oct 25, 2015 at 19:38
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.

2
  • Not working for me? I get E10: \ should be followed by /, ? or & 24,1 60%, but perhaps I am doing it wrong? Commented Oct 25, 2015 at 14:55
  • @KristianNordestgaard, in .vimrcwe have to remove initial ":" . Please try map \q :%s/\v([}"])(\s*\n\s*\S+\s*\=)/\1,\2/e<CR><CR>
    – JJoao
    Commented Oct 25, 2015 at 17:31

Your Answer

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

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