Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
2  
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

6 Answers 6

up vote 3 down vote accepted

How about bibsort?

NAME
     bibsort - sort a BibTeX bibliography file

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

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

share|improve this answer

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

share|improve this answer

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.

share|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:

#!/bin/bash

bibliobasher() {

    IFS=$( echo -en "\n" )
    BIBFILES=*.bib

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

bibliobasher

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

chmod a+x bibsort.sh
./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 ...

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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