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.


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 

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:

  1. The desktop file preexists in the right directory for your application.
  2. You provide the correct desktop file name as an argument to xdg-mime.
$ ls -l /usr/share/applications/*xpdf*.desktop
-rw-r--r-- 1 root root 181 Mar  4 10:13 /usr/share/applications/xpdf.desktop

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).


To verify that xpdf is now indeed the default application:

  1. Open a PDF file using the command.
    $ xdg-open somefile.pdf
  2. 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.

1 comment:

Franz Felix said...

user@debian-8-multimedia-dvm:~$ xdg-mime query default application/pdf

no reply so tried to add it:
user@debian-8-multimedia-dvm:~$ xdg-mime default evince.desktop application/pdf

check again now:
user@debian-8-multimedia-dvm:~$ xdg-mime query default application/pdf

trying to open a pdf
user@debian-8-multimedia-dvm:~/QubesIncoming/personal$ xdg-open test.pdf
/usr/bin/xdg-open: 491: /usr/bin/xdg-open: gnome-open: not found

What means "gnome-open: not found" here?