Understanding file associations in LXDE and PCManFM
October 29, 2012 7 Comments
Or “understanding how PCManFM decides which program to launch when you double-click on a file”
Or “understanding the list of suggested programs when you right-click on a file in PCManFM”
Or “making sense of the mimeinfo.cache, defaults.list and mimeapps.list crazyness”
Create a text file (text.txt) file in PCManFM and right click on it: PCManFM displays a list of possible application that can open the file:
Now if I double-click on the text.txt file, abiword (the first item highlighted in the screenshot above) is going to be launched.
- Where does this “AbiWord – gedit- Leafpad” list come from ?
- I want to use gedit instead of AbiWord when double-clicking on the file: how can I change this ?
- I want my text file to be opened by a program that is not listed: how can I do this ?
- Where in the world is Carmen SanDiego ?
Sit back and relax, I’m going to tell you the whole story.
At the beginning, there was the .desktop files
These files are mainly used to specify which elements are going to be displayed in the LXDE start menu and on your desktop. Check my creating LXDE shortcuts post for more information.
Then there was mimeinfo.cache
This file is located in /usr/share/applications/mimeinfo.cache and it is a basically a raw reverse cache for the .desktop information. Check this link for more information.
More precisely, each .desktop file on your system (which can be located either in /usr/share/applications, in /usr/local/share/applications or in ~/.local/share/applications) can contain a “MimeType” attribute which states the mimetype that the program associated to the .desktop file can handle.
On my Fedora installation, I have 3 text editors: Abiword, Leafpad and gedit and each one of them can open files that have a text/plain mimetype:
[laurent@localhost applications]$ cat fedora-abiword.desktop | grep text/plain MimeType=application/x-abiword;text/x-abiword;text/x-xml-abiword;text/plain; [laurent@localhost applications]$ cat gedit.desktop | grep text/plain MimeType=text/plain; [laurent@localhost applications]$ cat fedora-leafpad.desktop | grep text/plain MimeType=text/plain;
Now if I have a look in mimeinfo.cache, I can find the reverse mapping that we just talked about:
[laurent@localhost applications]$ cat mimeinfo.cache | grep text/plain text/plain=fedora-abiword.desktop;gedit.desktop;fedora-leafpad.desktop;
So this translates to “any file with a text/plain mimetype can be opened using either abiword, gedit or leafpad”.
Astute readers will notice that:
- This list (abiword + gedit + leafpad) is exactly the one displayed in PCManFM when I right click on a file (see screenshot above).
- The first item in this list (in my case: abiword) is the program that is launched when I double-click on a text file in PCManFM.
However, editing this mimecache.info file (to change the order of the .desktop files that are associated with the text/plain mimetype) is not what you want to do. The truth is you may lose all your changes after installing/removing a new Linux package.
This is because this file is generated by launching the “update-desktop-database” utility. This tool parses all your .desktop files and generates the mimeinfo.cache file… more or less each time you install a new package. This is where the defaults.list files join the party
Here comes defaults.list
defaults.list is a file where you configure the default program that should be used to open a file. To be very specific, this is where you configure the default “mime type -> .desktop file” association.
Like .desktop files, you can have a defaults.list file in any of the following locations (you can even have 3 files: one at each location).
First, there is a global defaults.list file located in /usr/share/applications and then you can have one for each user, located in ~/.local/share/applications. As you can guess, the “user-level” file has higher priority than the “global one”.
If I only have a /usr/share/applications/defaults.list file that contains a “text/plain -> gedit.desktop” mapping, then double-clicking on a text file in PCManFM will open gedit, no matter what the ordering of mimeinfo.cache is:
[laurent@localhost applications]$ cat /usr/share/applications/defaults.list | grep text/plain text/plain=gedit.desktop
But if I also have a ~/.local/share/applications/default.list file that contains a “text/plain -> fedora-leafpad.desktop” mapping, then double-clicking on a text file in PCManFM will open leafpad (since this file has “higher priority” than the files in the 2 other locations).
[laurent@localhost applications]$ cat ~/.local/share/applications/defaults.list | grep text/plain text/plain=fedora-leafpad.desktop
But wait, there is more !
The mimeinfo.cache, the defaults.list and the mimeapps.list
Yes that’s true, one more file to bother you (did I mention “crazyness” at the beginning of this post ?).
Now if you think that ~/.local/share/applications/defaults.list has higher priority than /usr/share/applications/defaults.list which has higher priority than /usr/share/applications/mimeinfo.cache”, you would still be incorrect because ~/.local/share/applications/mimeapps.list has even higher priority than all these bastards.
mimeapps.list allows you to:
- add even more “right-click” options to PCManFM right click menu
- set the default program for a mimetype (exactly like in defaults.list)
Although I’m not aware of any tool to help you deal with defaults.list file, there are at least 2 options to help you with mimeapps.list
OK, so let’s say that you would like to open your text file with yet another program that is not listed in the PCManFM right-click menu. Just choose the “Open with…” option and select an application. In my case, I’ll choose “Xpad”, and I’ll choose the “set selected application as default” option:
And there you are, xpad is now part of your right-click menu:
Behind the scene, PCManFM created a mimeapps.list file:
laurent@localhost applications]$ cat ~/.local/share/applications/mimeapps.list [Added Associations] text/plain=fedora-xpad.desktop; [Default Applications] text/plain=fedora-xpad.desktop
Another way to generate this file is to use the xdg-mime tool with “xdg-mime default fedora-xpad.desktop text/plain”
So let’s recap
The list of program that is displayed when right-clicking on a file in PCManFM is the combination of:
The default program that is launched when you double-click on a file in PCManFM is (first match wins):
- The one from ~/.local/share/applications/mimeapps.list
- The one from ~/.local/share/applications/defaults.list
- The one from /usr/local/share/applications/defaults.list
- The one from /usr/share/applications/defaults.list
- The first one from /usr/share/applications/mimeinfo.cache