128

I'm using OpenBox window manager without any desktop environment.

xdg-open behaves strangely. It opens everything with firefox.

$ xdg-settings --list
Known properties:
  default-web-browser           Default web browser

I'm looking for a simple program; something like reading every *.desktop file in /usr/share/applications/ folder and automatically setting xdg settings.

2
  • 2
    using xdg-mime (part of xdg-utils) is the best answer for working with (querying, configuring) xdg-open. See also unix.stackexchange.com/questions/8793/…
    – michael
    Jan 2, 2014 at 9:01
  • 1
    In case someone else has this issue with xdg-open opening everything in Firefox, simply deleting ~/.config/mimeapps.list and restarting fxed it for me (in Ubuntu 20.04).
    – IanS
    Dec 11, 2020 at 11:12

6 Answers 6

145

Why not to use utilities from xdg itself?

To make Thunar the default file-browser, i.e. the default application for opening folders.

$ xdg-mime default Thunar.desktop inode/directory

to use xpdf as the default PDF viewer:

$ xdg-mime default xpdf.desktop application/pdf

This should create an entry

[Default Applications]
application/pdf=xpdf.desktop

in your local MIME database ~/.config/mimeapps.list.

Your PDF files should be opened with xpdf now.

10
  • 1
    +1 and see also my related answer to the related question, unix.stackexchange.com/questions/77136/…
    – michael
    Jan 2, 2014 at 8:57
  • 3
    Most useful answer, thanks. Easiest to know about defaults.list so it can simply be edited by hand if need be.
    – mVChr
    May 24, 2016 at 21:08
  • 1
    For some reason xdg-mime did not modify defaults.list, but editing it manually (thanks to hints in this answer) did the trick. Jun 28, 2016 at 12:45
  • 10
    It seems defaults.list is now called mimeapps.list. See the Arch Wiki for a list of valid locations for mimeapps.list. Sep 24, 2017 at 20:51
  • 2
    to figure out mimetype of a certain file run xdg-mime query filetype pathTofileYourInterestedIn
    – daGo
    Apr 2, 2020 at 19:11
34

You can install and use perl-file-mimeinfo in the extra repository to manage mimetypes.

Example to open all .pdf files in apvlv:

/usr/bin/vendor_perl/mimeopen -d $file.pdf

or on other Linux distributions where mimeopen is NOT in /usr/bin/vendor_perl/ but is in one of the $PATH directories :

mimeopen -d $file.pdf

and then, at the prompt, enter the application: apvlv.

6
  • 3
    It work's. But it only opens file. So my xdg-open left unconfigured and applications using xdg-open won't open files right.
    – kravemir
    Apr 15, 2012 at 8:53
  • I don't quite follow your comment. What is not working? The -d switch sets the default. So if you use it to open fileA.pdf - you should then be able to open fileB.pdf with your preferred application.
    – jasonwryan
    Apr 15, 2012 at 9:34
  • 1
    Yes. mimeopen -d a.pdf sets default for *.pdf, but it sets default for itself - mimeopen b.pdf works. But xdg-open configuration left unchanged and it still opens everything with firefox :/
    – kravemir
    Apr 19, 2012 at 12:40
  • 3
    Once I have set the default with mimeopen, xdg-open behaves as expected--in the case of .pdfs, it opens them with apvlv.
    – jasonwryan
    Apr 20, 2012 at 7:50
  • 2
    This doesn't work for me in Lubuntu 16.04. sudo xdg-mime default pluma.desktop text/plain gives touch: cannot touch "/home/user/.local/share/applications/mimeapps.list": No such file or directory /usr/bin/xdg-mime: 807: /usr/bin/xdg-mime: cannot create /home/user/.local/share/applications/mimeapps.list.new: Directory nonexistent; /usr/bin/vendor_perl/mimeopen -d $file.txt gives bash: /usr/bin/vendor_perl/mimeopen: No such file or directory. Feb 21, 2017 at 18:50
12

You could use something like this to generate the appropriate xdg-mime commands for each of your .desktop files and the mimetypes they contain, then execute the ones you like. Note /usr/share/applications might have subdirectories.

find /usr/share/applications ~/.local/share/applications -iname '*.desktop' -print0 | while IFS= read -r -d $'\0' d; do
  for m in $(grep MimeType "$d" | cut -d= -f2 | tr ";" " "); do
    echo xdg-mime default "'$d'" "'$m'"
  done
done
2
  • See user60561's answer if any .desktop file name contains spaces.
    – Scz
    Mar 10, 2020 at 15:23
  • @Scz I copy/pasted the enhancement, I assume it works since it has been voted by 5 people. Thanks for pointing it.
    – aleb
    Mar 12, 2020 at 11:23
10

On Debian you could use the command, as root

update-mime-database /usr/share/mime

having the shared-mime-info package installed.

2
  • 2
    I've tried it. It doesn't work. I'm running Arch.
    – kravemir
    Apr 14, 2012 at 12:44
  • 1
    -1 This is specific to desktop environments that depend on shared-mime-info, such as GNOME Jun 16, 2019 at 22:17
4

For those like me that did not know how to get the proper mimetype to use with xdg-mime:

1- Initial situation:

$ xdg-open . opens vscodium (who hardcoded this stupid file association?) :[

2- Problem: what is the mime type of "."?

$ mimetype . .: inode/directory

3- Solution:

$ xdg-mime default thunar.desktop inode/directory

NOTE: Following line doesnot work but no error is reported :{ $ xdg-mime default Thunar.desktop inode/directory

1

Adding to what everyone said, Make sure you have no overlapping applications being used.

For example let us assume you want to change the default magnet link to use qbittorrent so you type

xdg-mime default org.qbittorrent.qBittorrent.desktop x-scheme-handler/magnet

But when you type this and check your current default magnet

xdg-mime query default x-scheme-handler/magnet

It shows some other application for example popcorntime.desktop which isn't what you want.

So to fix this:

Using a File manager, go to /usr/share/applications and find popcorntime.desktop

Right click it and then click properties.

Go to the Applications tab.

In the Supported file types: area, remove the Mimetype application/x-bittorrent

Then go ahead and click ok

Notice how when you open properties again and check, you find that nothing has changed and it went back to how it was.

But it actually got deleted for a second and got reconfigured, which is why now the command

xdg-mime default org.qbittorrent.qBittorrent.desktop x-scheme-handler/magnet

which was already set in mimeapps.list will get applied.

1
  • Your result indicates that the xdg-mime default command isn't succeeding, or something is overwriting the mime database - maybe a nightly cron job? Editing distribution files in /usr/share/applications may solve the problem short term, but is a hack.
    – Greg Bell
    Jun 9, 2020 at 4:58

You must log in to answer this question.

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