11

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?

Viesturs
  • 805
  • 8
  • 19
  • 1
    Unrelated to your question, consider using [`bibslurp`](https://github.com/mkmcc/bibslurp) package, it allows you to retriev BibTeX entries from [The SAO/NASA Astrophysics Data System](http://adswww.harvard.edu/) 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? – Nova Jul 15 '17 at 15:01
  • I get the same behaviour when starting with a `-Q` switch – Viesturs Jul 15 '17 at 17:57

4 Answers4

16

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.

JeanPierre
  • 7,323
  • 1
  • 18
  • 37
Dan
  • 32,584
  • 6
  • 98
  • 168
  • Can `bibtex-fill-entry` be applied after each save in bibtex-mode? – alper Oct 26 '22 at 10:42
  • @alper: that's an unrelated question, but you can look at `before-save-hook` and `after-save-hook`. – Dan Oct 27 '22 at 16:16
  • I have done it but `bibtex-fill-entry` only work at the block where cursor is at, not on the entire buffer – alper Oct 27 '22 at 18:29
3

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.

3

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)
Saddle Point
  • 481
  • 7
  • 23
1

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.

Nova
  • 1,059
  • 9
  • 21