Let's try to associate a file extension to a specific application. For the sake of this example, I would like to associate .somefile to be opened with sublime /usr/bin/subl
Before beginning, let's set a baseline. If you use the xdg-mime utility to query a file's type, you most probably will get something like text/plain. To try this out, let's create a sample file:
touch x.somefile
And then see what the system thinks about this filetype:
xdg-mime query filetype x.somefile
Most probably it will print out something, like text/plain. The first exercise is to change it.
Let's create a new mime type. For this, I'll need to create an xml file, with the name of lakat-somefile.xml (lakat is a vendor string, you might want to replace it with something else):
<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
<mime-type type="application/x-somefile">
<comment>somefile type file</comment>
<glob pattern="*.somefile"/>
</mime-type>
</mime-info>
Now let's install this mimetype:
xdg-mime install lakat-somefile.xml
And see that now the type of our dummy file is recognised:
xdg-mime query filetype x.somefile
We are expecting application/x-somefile there. If you are seeing something like inode/x-empty there, you most likely need to install some mime specific packages. In arch linux, what you need is:
sudo pacman -S perl-file-mimeinfo
Once that's done, the next thing to do is to create an "application" that will be used to open this file. If you already have an application, you don't need to do this.
Create a file, called somefile.desktop with the following contents:
[Desktop Entry]
Name=Somefile opener
Comment=Opens with sublime
Exec=/usr/bin/subl
Version=1.0
Type=Application
MimeType=application/x-somefile
now move this file to your home directory, as a per user application:
cp somefile.desktop ~/.local/share/applications/
Now we need to associate the two. Before we do that, let's make sure that we have the correct files in place. Some old applications refer to this config file as ~/.local/share/applications/mimeapps.list, others refer to it as ~/.config/mimeapps.list. But both should be the same, so let's say that ~/.config/mimeapps.list is a symlink to ~/.local/share/applications/mimeapps.list:
ln -s ~/.local/share/applications/mimeapps.list ~/.config/mimeapps.list
Now before we start to edit this file, let's see what is the situation now, what application is used to open application/x-somefile type files:
xdg-mime query default application/x-somefile
That should yield nothing at the moment. So let's add some associations. Open ~/.config/mimeapps.list, and add something like this:
[Default Applications]
application/x-somefile=somefile.desktop
Now doing the query again, you should see that:
xdg-mime query default application/x-somefile
returns somefile.desktop as expected. Now you should be all set, and you can open your files with xdg-open:
xdg-open x.somefile