Linux – xdg-open default applications behavior

arch linuxdefaultsfreedesktop

When I have no defaults.list and mimeapps.list, xdg-open opens text/plain with gvim. But when I create defaults.list in /usr/share/applications/ and add the following lines :

[Default Applications]
text/plain=emacs.desktop

it opens text files with firefox.

However, if I move /usr/share/applications/defaults.list to ~/.local/share/applications/mimeapps.list, it opens text files with emacs as expected.

My questions:

  1. How does xdg-open determine which application to launch when there is no defaults.list and mimeapps.list?
  2. Why is xdg-open using firefox when I simply write /usr/share/applications/defaults.list?

I am using Arch linux.

Best Answer

If using xdg-open to open applications, then use xdg-mime to set the default application for a given mime type (typically, installing xdg-utils gives you the xdg-mime and related programs).

For example, to see the "filetype" (mime-type, if you will) of given file:

$ xdg-mime query filetype tmp.txt
text/plain

$ xdg-mime query filetype foo.pdf 
application/pdf

$ xdg-mime query filetype $PWD
inode/directory

Example changing the default file manager for opening directories (could choose caja.desktop (default for mint), nautilus.desktop (ubuntu), etc:

$ xdg-mime default Thunar.desktop inode/directory

And also do locate -i foo.desktop to verify that foo.desktop does in fact exist.

More to the point, in order to see what the default text editor is,

$ xdg-mime  query default text/plain 
gedit.desktop

To use a different default text editor (again, verifying that the ".desktop" file exists):

$ locate -i vim.desktop
/usr/share/applications/gvim.desktop

$ xdg-mime default gvim.desktop text/plain

Now, either double-clicking "foo.txt" (in your GUI file manager) or running xdg-open foo.txt will use gvim instead of gedit.

Troubleshooting: be sure that the ".desktop" file for a given application can be found, since that has to be used with the xdg-mime utility; e.g.,

$ xdg-mime default emacs text/plain 
xdg-mime: malformed argument 'emacs', expected *.desktop

Further, this silently fails to work as expected if the ".desktop" file does not exist:

$ locate -i emacs.desktop
(none)

$ xdg-mime default emacs.desktop text/plain  # won't work

The actual emacs ".desktop" file (in my env):

$ locate -r 'emacs.*\.desktop'
/usr/share/applications/emacs24.desktop

$ xdg-mime default emacs24.desktop text/plain  # does work

See also: https://wiki.archlinux.org/index.php/xdg-open

Related Question