Skip to content

Instantly share code, notes, and snippets.

@finalclass
Last active August 6, 2021 09:53
Show Gist options
  • Save finalclass/49ed7e6864260d3cd6a8e643807a3ae3 to your computer and use it in GitHub Desktop.
Save finalclass/49ed7e6864260d3cd6a8e643807a3ae3 to your computer and use it in GitHub Desktop.
associating file types with apps

How to associate a file type with an app on linux?

You need:

  1. to define an app
  2. to define a mime type
  3. set default

Define an app

To define an app create a new file in ~/.local/share/applications. For example: atril.desktop (the .desktop extension is required)

Add this content inside:

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=Atril
Exec=atril
Icon=application-pdf
StartupWMClass=crx_mdpkiolbdkhdjpekfbkbmhigcaggjagi
MimeType=application/pdf

Define a mime type

To do this you have to create a file anywhere on your computer with this structure: mimeType.xml where mimeType should be replaced with the desired mime type but with - instead of /. For example for html it would be text-html.xml. In our example we will associate pdf files with atril app so our file will be named: application-pdf.xml because mime type of the pdf file is application/pdf. If you don't know what's the mime type of a certain file type this:

xdg-mime query filetype path-to-file.pdf

This is an example content of the application-pdf.xml file:

<?xml version="1.0" encoding="utf-8"?>
<mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="application/pdf">
  <comment>a pdf file</comment>
  <glob pattern="*.pdf"/>
</mime-type>

The final step in defining a mime type is to install it. You can do it with this command:

xdg-mime install application-pdf.xml 

Set default

The final step is to associate the mime type with the application (mark it as default app). You can do it with the command:

xdg-mime default atril.desktop application/pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment