When you open a file in Linux, the question arises: Which of the available applications should be opened? Usually, this question is deferred to xdg-open. But, how does xdg-open know which application is the right one and how do you configure the one you want to use instead?🤓

To get started, try opening your file like so: xdg-open your_path. This is just to confirm what application is opened right now. To configure a different application, first, we have to know about the mimetype of your_path. In this example, I'm asking for the mimetype of an image:

$ mimetype foo.jpg
foo.jpg: image/jpeg

So, the mimetype is image/jpeg. Now we can ask how xdg comes up with the responsible application:

$ XDG_UTILS_DEBUG_LEVEL=3 xdg-mime query default image/jpeg

Checking /home/munen/.config/mimeapps.list
Checking /home/munen/.local/share/applications/mimeapps.list

We are increasing the debug level with XDG_UTILS_DEBUG_LEVEL=3 to list the responsible application, and the paths to the config files.

Finally, we can set a new default application. Suppose you want to open jpegs with imv in the future. You will add the responsible desktop entry file. My distribution (Debian) usually ships with a desktop entry file for every package. The Debian package for imv does include a imv.desktop file. Here's how to configure imv as the default for image/jpeg files, then:

$ xdg-mime default imv.desktop image/jpeg

This will add the following line to one of your mimeapps.list config files (i.e. .config/mimeapps.list):


How to create a custom desktop entry file

If an application doesn't have a desktop entry file, or you want to create a custom configuration for your needs, just create a new file in ~/.local/share/applications.

Here is an example of creating a desktop entry file for Emacs, but instead of running Emacs (which would be the default), it uses emacsclient. I'm using this config for all kinds of mimetypes (directories, zip files, etc).

[Desktop Entry]
Name=GNU Emacs (GUI)
GenericName=Text Editor
Comment=GNU Emacs is an extensible, customizable text editor - and more
Exec=/usr/bin/emacsclient -c %F

If you liked this post, please consider supporting our Free and Open Source software work – you can sponsor us on Github and Patreon or star our FLOSS repositories.