MIMEtypes

A MIMEtype is a universal method of associating a specific “kind” of file to a specific category of application.

For example, if you have an .mp4 file, you can be fairly confident that the file contains video, and so you probably want that file to be opened in a video player rather than, say, a word processor. You might think it logical to associate that file with VLC, a popular video player, but what if you don't have VLC installed? After all, there are several very good video players, and it would be better if the mp4 file would just open in some video player that can handle mp4 files.

The way this is accomplished is that files are assigned, on a systemwide level rather than a per-file basis, MIMEtypes based on their suffix. If your OS sees a file ending in .txt, it assumes it is a plain text file and attempts to open it in any application that has been categorised as a text editor, if it sees a .mp4 then it attempts to open the file in a video player, and so on.

Any OS ships with a pre-defined list of file associations, but users usually have their own favourite applications, so the MIMEtype associations can be changed on a per-user basis.

Easy MIMEtype Definitions

The most immediate way of specifying your preference in what the KDE chooses to open for a file suffix is pretty common across all operating environments, at least conceptually. For instance, if you have an .mp4 file and it opens in VLC but you would rather use Mplayer as your video player:

  • Right-click on any .mp4 file in the Dolphin file manager.
  • Select Open With…Other from the contextual menu.
  • In the Open with dialogue box, select the application you want the file to be opened with. To make this a permanent choice, place a tick mark in the checkbox by Remember application association for this type of file.
  • Click OK to commit.

That's all there is to it.

The association between the file and your chosen application is stored in ~/.local/share/applications/mimeapps.list, which applies to you as a user and no other user on the system. This file association will travel with you in the event that you get a new computer or migrate your home folder for any reason; as long as you copy your entire home directory, the file association will migrate with you.

It takes a while to build up a solid exception-free list of application choices, but once you have defined everything to work just the way you want it to work, you don't have to do it again. For bonus points, backup the ~/.local/share/applications/mimeapps.list file to a Git repository online or a thumbdrive so it's easy to grab whenever you need it.

Custom MIMEtypes

The time that the easy method of setting MIME prefernces falters is when you want to define action for an arbitrary suffix. For instance, if you use Emacs org-mode for your daily agenda, then you might want any file ending in .org to open in Emacs, or if you write your screenplays in .fountain markdown and want all files ending in .fountain to open in Kate. To your computer, these suffixes are meaningless, so it defaults to a quick filetype check and opens whatever application is the most logical.

Try it yourself with a file meant for Screenwriter-mode:

$ echo "INT. HOUSE - NIGHT" >> ~/example.scp
$ file ~/example.scp
/home/klaatu/example.scp: ASCII text

Of course, the computer is right; the file is ASCII text, but that doesn't change the fact that you want it to open in a specific application. You could try the easy method, telling KDE to always open .scp files in Emacs, but .scp is not a MIMEtype, it's just a set of random characters at the end of a filename. Telling KDE to open all files like example.scp is really just telling KDE to open all plain text files with a specific application, and that is not what you want. You want to filter out all .scp files away from other plain text files. and do something unique.

To do this, you must define your own MIMEtype.

MIMEtypes can be defined in 7 lines of XML. Here is a specification for a .scp file, which will open in Emacs:

<?xml version="1.0"?>
   <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
      <mime-type type="application/x-scp">
      <comment>Emacs Screenwriter File</comment>
      <glob pattern="*.scp"/>
   </mime-type>
</mime-info>

Save that file as mime-scp.xml and install it to your system's MIMEtype database:

$ xdg-mime install mime-scp.xml

You may have to log out and log back in (it should be enough just to close any running instance of Dolphin), but now the MIMEtype is active within your user's login. Right-click on a .scp file and set files of its type to open in Emacs. Be sure to place a tick in the Remember checkbox.

From now on, any file ending in .scp will open in Emacs, but all other plain text files will continue to default to their usual settings.

Custom Icons

You can set a custom icon for your MIMEtype, too. Create or choose an icon (either a PNG or SVG is valid) and name your icon the same thing as the mime-type type line in your custom mimetype XML spec, with dashes instead of slashes, and move it to the master icon repository for your system (/usr/share/icons/hicolor/). Continuing with the .scp example:

$ grep mime-type mime-scp.xml | cut -f2 -d'"'
application/x-scp
$ su -c 'mv myFancyIcon.svg /usr/share/icons/hicolor/scalable/application-x-scp.svg'

You may have to log out and log back in for things to kick in, but this will assign all .scp files your chosen icon, and those files will open in whatever application you choose in KDE.

R S Q