Set Mailcap Package Precedence with mailcap.order

mailcap.order decides which Debian package wins when two apps both claim the same MIME type, without touching a single executable name. You will set a system-wide preference, regenerate /etc/mailcap, and verify that the generated file contains the expected package entries. This guide uses the mailcap package version 3.70+nmu1ubuntu1.24.04.1 installed on the reference system. Allow about ten minutes. You need a shell, an editor, and sudo access for the system-wide change.

The ordering file chooses among entries supplied by Debian packages. It does not directly choose an executable. That distinction is the main source of mistakes.

1. Inspect the available package rules

Before editing anything, find the package files that provide mailcap entries:

$ find /usr/lib/mime/packages -maxdepth 1 -type f -printf '%f\n' | sort | less

Inspect a candidate package file and confirm the MIME type it supplies:

$ sed -n '1,12p' /usr/lib/mime/packages/man-db
text/plain; /usr/bin/man -l %s; copiousoutput; description=Manual page; priority=6

The filename, man-db, is the package name used by mailcap.order. The command inside the entry is a separate matter. Repeat this check for the package you actually want to rank.

Checkpoint: You should have a real filename under /usr/lib/mime/packages and a MIME pattern from that file. If you cannot identify both, stop before editing the ordering file.

2. Record the current generated file

Changing the order regenerates /etc/mailcap, so make a root-owned backup first:

$ sudo cp -p /etc/mailcap /etc/mailcap.before-mailcap-order
$ sudo cp -p /etc/mailcap.order /etc/mailcap.order.before-mailcap-order

These commands require elevated privileges because both files are system configuration. The backup names are deliberately explicit. Do not overwrite an existing backup without checking it first:

$ sudo test ! -e /etc/mailcap.before-mailcap-order && echo 'mailcap backup is new'
$ sudo test ! -e /etc/mailcap.order.before-mailcap-order && echo 'order backup is new'

If either test prints nothing, choose a different backup name or inspect the existing backup. Do not treat that as permission to discard it.

3. Add package and MIME precedence rules

Edit the ordering file as root, preserving its comments:

$ sudoedit /etc/mailcap.order

For example, these lines place the man-db package before other matching packages for plain text, then place vim-common before other packages for remaining text types:

man-db:text/plain
vim-common:text/*

Each line has a package name, a colon, and an optional MIME type. A package-only rule such as man-db: matches all entries supplied by that package. A rule ending in */* is a broad fallback and can move many entries, so use it only when that is genuinely intended.

Rules that match are placed in the order of this file. Entries that do not match are placed later. A wildcard is not an exclusion rule: there is no way in this format to say that one subtype should escape an earlier wildcard. If two packages both provide image/*, you cannot use this file to select one for GIF and the other for JPEG.

Do not write an executable name in the first field. For example, vim:text/* is not a rule for the vim executable unless a package file literally has that package name. To define a rule for a specific program, use a user ~/.mailcap entry or the user section of /etc/mailcap, as described by the local mailcap.order(5) documentation.

4. Regenerate the system mailcap file

Run the generator as root:

$ sudo /usr/sbin/update-mime

On success it normally produces no output and returns status 0. Check the status immediately:

$ printf '%s\n' "$?"
0

update-mime reads package definitions from /usr/lib/mime/packages and updates /etc/mailcap. Debian package installation and removal can also trigger it, so keep the source ordering file under review rather than editing the generated mailcap file directly.

For a per-user configuration, the same command has a safer local mode:

$ update-mime --local

This writes a complete ~/.mailcap and looks for ~/.mailcap.order. It does not alter /etc/mailcap, and it avoids the root requirement. Use this when the preference belongs to one account rather than every user.

5. Verify the generated order

Search the generated file for the MIME type and the commands you expect:

$ grep -nE '^(text/plain|text/\*);' /etc/mailcap | head -n 12

The matching entries should appear in the precedence you selected, subject to the package entries and their priorities. The generated file may contain more than one entry for a type. Do not assume the first line is the only available handler.

To check which packages supplied the matching lines, compare their command paths with the package files:

$ grep -nE '^(text/plain|text/\*);' /usr/lib/mime/packages/man-db /usr/lib/mime/packages/vim-common

If your rule had no visible effect, check spelling and punctuation first. The package field must match a filename under /usr/lib/mime/packages; the MIME field must match the entry's type pattern. Then rerun update-mime and inspect /etc/mailcap again.

6. Undo a bad ordering change

If the regenerated result is wrong, restore both files from the backups and regenerate:

$ sudo cp -p /etc/mailcap.order.before-mailcap-order /etc/mailcap.order
$ sudo cp -p /etc/mailcap.before-mailcap-order /etc/mailcap
$ sudo /usr/sbin/update-mime

Recovery: The last command is needed because /etc/mailcap is generated output. If you only restore the ordering file, a later package trigger or manual regeneration can recreate the unwanted order. If the change affects an application, close and reopen that application after verification; this guide does not restart services.

Done means