Like many features to do with the desktop, default applications are governed by one of many Freedesktop specifications.
Your distribution likely provides xdg-open
, which programs will use to open the default application for a file.
You can use it directly like this:
xdg-open file.html
If you want to know the default application used for opening a particular filetype, first get the filetype with xdg-mime
:
xdg-mime query filetype file.html
You’ll get:
text/html
Now you can query the default application used for that filetype:
xdg-mime query default text/html
The output might be:
chromium.desktop
At this point, you might be wondering what the .desktop
extension means. These are Desktop Entries, defined by the Freedesktop Desktop Entry Specification.desktop
entries are files that describe an application; namely, the name of the application, the name of the icons it uses, and how to launch it.
.desktop
entries are stored in several locations. You’ll find your system .desktop
entries at /usr/share/applications
. You’ll find your local .desktop
entries at ~/.local/share/applications
.
There are more locations your .desktop
entries might be. For example, if you install an application via Flatpak, you can use the $XDG_DATA_DIRS
environment variable to find out where they are:
echo $XDG_DATA_DIRS
To set a new default application for this filetype, you need to know the name of the .desktop
entry for the application you want to use.
So, if we want to use Firefox, we want to do something like this:
ls /usr/share/applications | grep -i firefox
That gives me:
firefox.desktop
If you installed Firefox another way, you might get nothing at all. The desktop entry might be located in ~/.local/share/applications
and named something different, or somewhere else altogether.
Now that we know the name, we can set it as the default application for text/html
files:
xdg-mime default firefox.desktop text/html
And now if you try to open that .html
file with xdg-open
, it will open in Firefox:
xdg-open file.html
Applications using xdg-open
to determine the default filetype for .html
files will also now open them in Firefox for you through xdg-open
.
And that’s all there is to it!