Linux – How does Linux decide on a default application

file associationlinuxmime-types

I know that each program that can open files has a desktop entry under /usr/share/applications, and that the defaults for each mimetype are stored in /usr/share/applications/defaults.list and ~/.local/share/applications/mimeapps.list. On my system, the command that updates these lists is update-mime-database. However, I could not find an answer on google about how this program decides which application becomes default if there are multiple installed that can handle that file. I ask because when I installed Geany on my system recently, all source code files started opening in Geany, instead of in my regular text editor, and I was wondering how the choice to replace my regular editor with Geany was made. Also, I know that I can change each file back to my regular editor manually, but is there a way give a certain program default to all the types of files it can open? Essentially like Windows's default applications manager, where you can see all the filetypes any program can open, and select or deselect them all.

I also know of the xdg-mime utility, but I that doesn't seem to be what chooses between multiple possible programs for opening a file, or for re-registering as default all the filetypes for a certain program.

Best Answer

First of all, Linux (the operating system) doesn't choose anything in what you describe (so the title is rather off), it is either the application you're clicking the file on or the desktop environment that makes the choice based on logic and information.

You mentioned update-mime-database, which leads to Gnome with some googling, and this page specifically: (which is just a replica of this:

Each application that wishes to contribute to the MIME database will install a single XML file, named after the application, into one of the three /packages/ directories (depending on where the user requested the application be installed). After installing, uninstalling or modifying this file, the application MUST run the update-mime-database command, which is provided by the shared database.

update-mime-database is passed the mime directory containing the packages subdirectory which was modified as its only argument. It scans all the XML files in the packages subdirectory, combines the information in them, and creates a number of output files.

And here's the gotcha:

Where the information from these files is conflicting, information from directories lower in the list takes precedence. Any file named Override.xml takes precedence over all other files in the same packages directory. This can be used by tools which let the user edit the database to ensure that the user's changes take effect.

So it might be that because Geany just happens to appear "lower in the list" it takes precedence. Lower here might mean anything, I guess, from alphabetical ordering of the respective files to the ordering among some predefined application list.

That page also describes the format of the files, and also mentions that additional tools might be available for manipulating the database (Override.xml specifically). As for if such tools exist might be worth another question.

Edit: This answer on using mimeopenmight also prove helpful to you even though it might not apply to your specific file manager of choice:

Related Question