21

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.

2
  • 1
    I use Openbox. Also, I noticed a /usr/share/applications/mimecache.info file which seems to list the default applications. It is even more confusing...
    – Gradient
    Commented May 26, 2013 at 18:12
  • 2
    I found this config file in ~/.config/mimeapps.list
    – Sam Mason
    Commented Aug 16, 2019 at 17:51

2 Answers 2

36

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

2
  • 7
    Years later, but Thanks! Some developer has put a really bad joke into wine, he must have thought "if you really want back some of that Windows pain, you should have it all to never get nostalgic!" And since that all X11-programs start a notepad clone when opening Text-Files. Opening files from an application is rare for me, but notepad really hurts. With your help I got back a gvim. Commented Jun 6, 2016 at 12:16
  • Similar jokes are being played on me right now, Krita, a digital painting application, is opening CSV files rather than a spreadsheet application. Commented Dec 17, 2021 at 14:18
1
#!/bin/sh

FILETYPE=$(xdg-mime query filetype $1)
APP=$( find /usr/share -type f -name "*.desktop" -printf "%p\n" | rofi -threads 0 -dmenu -i -p "select default app")
APP=$(basename -- $APP)
xdg-mime default "$APP" "$FILETYPE"
echo "$APP set as default application to open $FILETYPE"

Use this script on a file, then it will ask you to choose a default app to use for this filetype.

I use rofi, but you can adjust it for your fuzzy finder of choice. After that, you can just open your files with xdg-open.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .