Set default program to open files in Arch Linux

by krisrowland

I am currently using Arch Linux as my work OS. I have found Google’s Chromium browser to be far better than FireFox. I never thought I’d say that, but Chromium is excellent under Linux now.

I was finding that the default program that Chromium was using to open pdfs was FireFox with the acroread plugin; strange, but each to their own… It turns out that Chromium just uses the xdg-open command to load files with the user’s preferred applications. How to choose these preferred applications? The xdg-mime command must be used, e.g., to set acrorread as the deafault pdf reader:

xdg-mime default acroread.desktop application/pdf

However, I noticed that when I first tried this, I received four error lines telling me that various processes couldn’t access the configuration file:

/usr/bin/xdg-mime: line 625: /home/kris/.local/share/applications/defaults.list.new: No such file or directory
grep: /home/kris/.local/share/applications/defaults.list.new: No such file or directory
/usr/bin/xdg-mime: line 627: /home/kris/.local/share/applications/defaults.list.new: No such file or directory
/usr/bin/xdg-mime: line 629: /home/kris/.local/share/applications/defaults.list.new: No such file or directory
mv: cannot stat `/home/kris/.local/share/applications/defaults.list.new’: No such file or directory

This was easily fixed by manually creating a blank file in that directory which (I had to manually create the applications folder, but the ~/.local/share folder already existed for me). Now it seems xdg-utils works like a champ.

To make opening using default programs in the terminal a little easier, I created an alias in my ~/.bashrc by adding the line:

alias open=’xdg-open’

so that all I have to do is say open [file] and it opens the file in my preferred application set using xdg-mime default [application].[environment] [filetype].

Full worked example:

What about determining the filetype of a given file, determining the application it is opened with by default and then changing the default program? I just did this when I realised I didn’t have a program set to open locally saved/cached html files.

Say the local file is called test.html. To determine the filetype xdg associates with this file, run:

xdg-mime query filetype test.html

The output was:

text/html; charset=utf-8

So that the filetype is text/html. To determine the current program set to open this filetype, run:

xdg-mime query default text/html

I received a blank line after this; there was no default application set. To set a default application (e.g. the Google the Chromium browser), run:

xdg-mime default chromium.desktop text/html

… and that’s it. Now the command xdg-open test.html will open test.html in the Chromium browser. Again, if you bound xdg-open to just open as I showed above, you just need to type open test.html for the same result.

Advertisements