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 imv.desktop
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
):
image/jpeg=imv.desktop
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] Version=1.0 Name=GNU Emacs (GUI) GenericName=Text Editor Comment=GNU Emacs is an extensible, customizable text editor - and more MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++; Exec=/usr/bin/emacsclient -c %F Icon=emacs25 Type=Application Terminal=false Categories=Utility;Development;TextEditor; StartupWMClass=Emacs Keywords=Text;Editor;
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.