This blog is about the Linux Command Line Interface (CLI), with an occasional foray into GUI territory.
Instead of just giving you information like some man page, I hope to illustrate each command in real-life scenarios.

Search This Blog

Friday, March 7, 2014

How to specify default applications for file types: Part 1 - Command line

Numerous apps vie to open your PDF files by default - evince, xpdf, Adobe Reader, Okular, etc. If you don't like the default as dictated by your desktop environment, you can change it.

The general idea is that default applications for file types, or 'file association', is governed by a set of configuration files. Having said that, I wish an universal method existed to change file association that is independent of the Linux distribution, the desktop environment, the file manager, and the application. In reality, the desktop environment may choose to interpret the same configuration file in different ways, or use different files altogether. Below is the general procedure that has served me well.

xdg-mime

If you are comfortable with command-line, then I'd recommend that you use the xdg-mime tool first.

Before we change the default application for opening a PDF document, let's identify the current default application.

$ xdg-mime query default application/pdf
evince.desktop

The above command requires that you know the MIME type of PDF files(application/pdf). My earlier post explains how to find the MIME type of files.

If evince is not the default program you want, you can change it to say xpdf as follows.

$ xdg-mime default xpdf.desktop application/pdf

The second argument - xpdf.desktop - specifies the default application in the form of its desktop filename.
Every application must have a desktop file containing configuration information in order to be registered into the desktop environment's menu system. The global desktop file must exist in the /usr/share/applications directory. Local desktop files must exist in the ~/.local/share/applications directory for individual users.

Before you run xdg-mime, you must make sure that:

The desktop file preexists in the right directory for your application.

The xdg-mime command inserts one line into the [Default Applications] section of the local configuration file for file association (~/.local/share/applications/mimeapps.list).

application/pdf=xpdf.desktop

To verify that xpdf is now indeed the default application:

Open a PDF file using the command.

$ xdg-open somefile.pdf

Double click a PDF file from inside your favourite file manager.

With my Debian wheezy system running GNOME 3, xpdf now opens PDF files by default from both the command-line and the Nautilus file manager.

If you are not comfortable with the command-line or xpdf does not open the PDF file by default from your file manager, the remedy is to configure the default application using your file manager. That will be the topic of part 2 of this series.