I work with LaTeX and do versioning with Git. For bibliography management I use Mendeley.

The problem is that each time Mendeley synchronizes it's .bib exports, they are in different order, what makes bibliography versioning much harder.

My idea is to sort BibTex entries in .bib file, each time before commit.

Could you help me, how to do this in smart (short&sweet) way ? :)

P.S. I can run this routine manually. I do not need git integration. I just want program/script to sort .bib file.

  • 4
    This kind of question might have better audience at TeX - LaTeX StackExchange site. – Stéphane Gimenez Feb 9 '12 at 12:21
  • Not sure where this belongs (StackOverflow for the programming question of how to do the sorting, or (La)TeX for the problem in general), but I don't see anything that relates this question to *nix. – me_and Feb 9 '12 at 12:37
  • 1
    @me_and He seems to be looking for a Linux tool or simple shell script way to sort BibTeX entries, so it's probably fine here – Michael Mrozek Feb 9 '12 at 14:42

How about bibsort?

     bibsort - sort a BibTeX bibliography file

     bibsort [optional sort(1) switches] < infile >outfile

     bibsort filters a BibTeX bibliography, or bibliography frag-
     ment,  on  its standard input, printing on standard output a
     sorted bibliography.

It's a shell script wrapping nawk (and tr, sort and grep) and includes two warnings you might have to pay attention to (see the source).

(Edit There're also a lot of bibtex-related Perl modules...)

Edit2 I just recognized you'd like to sort for any key, while bibsort apparently sorts by the bibtex tags only -- but maybe its source (it's not too long) is still helpful...?

| improve this answer | |

You can use bibtool (-s option - sort according to the reference key) with the git post-commit hook.

| improve this answer | |
  • Didn't find a way to sort by year :-( – fccoelho Mar 16 '18 at 13:50

The reference manager jabref seems to have such an option in the preferences (see Sort order section in File page). It also has many interesting features:

enter image description here

| improve this answer | |
  • This option has been removed in more recent versions of jabref – fccoelho Mar 16 '18 at 13:46
  • No, it hasn't. It was put from "File" to "Export sorting" menu. And more sorting criteria are available now. – Zoltán Csáti Mar 21 '19 at 10:22

I had the exact same problem, right down to my choice of tools: using git and Mendeley on linux.

My 'solution' is sub-par compared to a command-line script, but nonetheless it made me happy, so here it is:

(i was also already using emacs, so) what i do is open a copy of the bibtex file in emacs after i do any work in Mendeley, then i run

M-x bibtex-sort-buffer

in emacs, save the results, and commit those sorted results to git.

Apparently bibtex-mode is standard in emacs, because I didn't have to install anything, and emacs automatically recognized my '.bib' as a bibtex file.

| improve this answer | |

I too have been having this problem, which is still not fixed in Mendeley. Based on que que's great suggestion, I solved my issue using emacs, but in batch mode, nicely allowing me to wrap the solution in a shell script.

First off, I created the following emacs lisp script (let's call it bibsort.el):

( defun bibsort()
    ( bibtex-sort-buffer )
    ( save-buffer )

This sorts any bibtex file by key name and then saves it. You can call the script in batch mode from the command line. I went further in writing a shell script to call it for each bibtex file which Mendeley had reordered (with or without fresh references), by checking the return code from "git diff filename" (this saves time by not running the lisp code unnecessarily on files which have not changed).

Here's the shell script (let's imaginitively call it bibsort.sh), which assumes that both the bibtex files and the lisp script are in the current directory:


bibliobasher() {

    IFS=$( echo -en "\n" )

    for bibfile in $BIBFILES
        git diff --quiet "$bibfile" 2> /dev/null
        if [ $? = 1 ]; then
            emacs --batch -l bibsort.el "$bibfile" -f bibsort


Just run it (after setting file permissions if necessary) before each git commit:

chmod a+x bibsort.sh
git commit ...

It won't touch the extra non-bibtex lines added by Mendeley, and should work on any Unix/Linux installation with emacs. It also works on the GIT console in Windows (which I'm using), but you would have to install the MINGW build of emacs separately.

For me, git commits now work elegantly and economically. Hope this helps ...

| improve this answer | |
  • No need to create the separate bibsort function, emacs Bibliography.bib --batch -f bibtex-sort-buffer -f save-buffer will do it too. – Dean Serenevy Mar 2 '14 at 22:44

emacs (and xemacs) have an add-on called AUCTeX, which (among other (La)TeX related keybord shortcuts and helpers) has a special mode for BibTeX, including sorting .bib files by the key.

| improve this answer | |

If you want to run a command each time a commit is done, perhaps the git post-commit hook can help (see for example the description in the git book.

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