Monday, August 4, 2014

Using xdg-mime to set default programs to open certain filetypes in Linux

Recently I had a need to set xpdf as the default program for opening pdf files instead of evince. I decided to use the command line to achieve this. Turns out that I can do this using xdg-mime.

To know what is the filetype (in the mime 'language') of pdf files:

 $ xdg-mime query filetype Programming.pdf  
 application/pdf  

Once I know the mime type of pdf files, I can see the default program currently used to open pdf files:

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

So, now I know that evince is being used as the default pdf file opener, and that there is a desktop file by the name of evince.desktop. The location of this file can easily be found using programs like 'locate':

 $ locate /evince.desktop   
 /usr/share/applications/evince.desktop  

Now I know where such desktop files are kept on my system. After installing xpdf, xpdf.desktop also turned up there (if not it is easy to create such a desktop file). So now I can make xpdf as the default program for opening pdf files using the following:

 $ xdg-mime default xpdf.desktop application/pdf   

That's it. Now if I try to click on a pdf file, xpdf is used to open it.