I want bibtex-mode to align and properly indent fields. I copied a bibtex entry for the following article into Emacs: (Article source here)

But, with a simple paste, the alignment is lost in Emacs and the indentation is messed up:

The TAB key doesn't work to align or indent the entry. How to solve this problem?

  • 1
    Unrelated to your question, consider using bibslurp package, it allows you to retriev BibTeX entries from The SAO/NASA Astrophysics Data System within Emacs itself. – giordano Jul 14 '17 at 13:32
  • 1
    I can't reproduce that behaviour. What happens if you start Emacs with the -Q switch? – Erik Jul 15 '17 at 15:01
  • I get the same behaviour when starting with a -Q switch – Viesturs Jul 15 '17 at 17:57

When the cursor is somewhere in the entry, run the command bibtex-fill-entry (bound to C-c C-q), which will align the fields. You may also want to set variable bibtex-align-at-equal-sign to a non nil value to change the details of alignment.

| improve this answer | |

I had a similar issue, when using the smartparens package in bibtex-mode, where shameful amounts of spaces where inserted. For some reason unknown, bibtex-mode sets the fill-prefix variable to a string containing 18 spaces. (setq fill-prefix nil) in the bibtex-mode-hook fixed the issue in my case.

| improve this answer | |

As mentioned by @JonatanLindén, fill-prefix is set to a string containing 18 spaces. This is because bibtex-clean-entry is using fill-prefix to align continuing text after equal sign. Setting fill-prefix to "" can solve the indentation issue. But to have better alignment when formating entry, you can advice bibtex-clean-entry to temporarily set fill-prefix.

(defun bibtex-mode-setup ()
  (setq-local fill-prefix ""))
(add-hook 'bibtex-mode-hook #'bibtex-mode-setup)

(defun bibtex-reset-fill-prefix (orig-func &rest args)
  (let ((fill-prefix (make-string (1+ bibtex-text-indentation) ? )))
    (apply orig-func args)))
(advice-add 'bibtex-clean-entry :around #'bibtex-reset-fill-prefix)
| improve this answer | |

Usually, you would use the align-regexp command (or the align command if bibtex-mode supported it). But that won't help you with the broken indentation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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