I am trying to sort a bib file alphabetically using Bibtool, but I am looking for sorting the entries by types then by keys. For instance, first all the articles, then the books, etc... With, all the articles sorted alphabetically by keys, then same with the books.

It looks like it should be a command like:

bibtool -s -sort.format={@type $key} -i BiBin.bib -o BiBout.bib 

Thank you all for your help



Following the example on page 60 of the BibTool manual, the type can be specified with %s($type).


    note = {misc entry 1}
    author = {John Doe},
    title = {On Things},
    note = {misc entry 2},
    author = {John Smith},
    title = {Of Things},

bibtool -s --sort.format='{%s($type) %s($key)}' -i bibsort.bib -o bibsorted.bib

@Article{     uvw,
  author    = {John Smith},
  title     = {Of Things}
@Article{     xyz,
  author    = {John Doe},
  title     = {On Things}
@Misc{        abc,
  note      = {misc entry 2}
@Misc{        def,
  note      = {misc entry 1}

Note that you need single quotes in the terminal command to prevent variable substitution by the terminal.

| improve this answer | |
  • Thank you, it works perfectly on my macOS terminal, however it still does not run with cygwin for some reasons... – RockyRock Apr 13 '18 at 13:30
  • Maybe you can try to store the sort format in a resource file and call that with bibtool -r resource_file (p. 18 in the manual). – Marijn Apr 13 '18 at 13:35

If you are using Biber you can use the following sort.conf

<?xml version="1.0" encoding="UTF-8"?>
  <sortingtemplate name="tool">
    <sort order="1">
      <sortitem order="1">entrytype</sortitem>
    <sort order="2">
      <sortitem order="1">entrykey</sortitem>

Then call Biber in tool mode

biber --tool --configfile=sort.conf <yourbibfile>.bib

this will produced a sorted file called <yourbibfile>_bibertool.bib

The standard Biber --tool mode caveats apply: Prevent `biber --tool` from removing non-standard fields in .bib files. Only fields known in the data model will survive tool mode (so if you use fields that are not recognised by the standard data model, you will have to make them known to Biber in a custom data model, see gusbrs' answer).

| improve this answer | |
  • That last sentence is disturbing. I just imagined the bibtool doing all kinds of brutal things to the unfortunate fields who happened to not be on the guest list. Lots of bloodshed. – thymaro Apr 14 '18 at 6:27
  • That's not a problem - you can also customise the data model with biber. – PLK Apr 15 '18 at 20:00

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.