Difference between revisions of "Default applications"

From ArchWiki
Jump to: navigation, search
m (Using MIME types and desktop entries)
(Example: .xml and related .desktop configuration: add crosslink to Desktop entry)
 
(212 intermediate revisions by 17 users not shown)
Line 1: Line 1:
 
[[Category:Desktop environments]]
 
[[Category:Desktop environments]]
[[zh-cn:Default Applications]]
+
[[ja:デフォルトアプリケーション]]
There are numerous places to configure default applications on Linux. This page will explain the most used ones: using MIME types and using environment variables.
+
[[ru:Changing filetype association via MIME]]
 +
[[zh-cn:Default applications]]
 +
{{Related articles start}}
 +
{{Related|Desktop entries}}
 +
{{Related|Desktop environment}}
 +
{{Related|Window manager}}
 +
{{Related articles end}}
  
== Using MIME types and desktop entries ==
+
The type of a data file is usually, but not necessarily, denoted by the extension of the file name, for example ''.html'' or ''.jpeg''. A second way to determine the data type is via the so-called "magic bytes" at the start of the file. Both ways determine the MIME-type of the data.  
The modern method to start applications is using [[Desktop Entries]]. This way, programs can advertise which kind of files (to be exact: what MIME types) they can open. For instance, {{ic|gimp.desktop}} states {{ic|<nowiki>MimeType=image/bmp;image/gif;...</nowiki>}}.
+
  
A list of MIME type and corresponding default application is stored in {{ic|$XDG_DATA_HOME/applications/mimeapps.list}} (for a single user; {{ic|$XDG_DATA_HOME}} defaults to {{ic|~/.local/share}}) and {{ic|/usr/share/applications/defaults.list}} (system wide). This list can be edited by using [[xdg-open#Configuration|xdg-mime]].
+
Each application installed to a system provides information about the MIME-types it can handle via the [[Desktop entries]], but frequently there is more than one application able to handle data of a certain MIME-type. This article describes how the ''default applications'' to handle a MIME-type can be configured.  
  
This file looks like this:
+
Note that while Arch Linux does not provide custom presets for default applications, the [[desktop environment]] you install may do so. Some desktop environments also provide a GUI or a file-manager which enables to interactively configure default applications for certain file extensions. If this suffices for your system usage, you may not need to configure anything further.
  
[Default Applications]
+
The scope of this article is the freedesktop [https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html Association between MIME types and applications] specification.  
mimetype=desktopfile1;desktopfile2;...;desktopfileN
+
  
E.g.
+
== MIME types and desktop entries ==
 +
 
 +
MIME-types are specified by two slash ({{ic|/}}) parts: ''Type/Extension'', for example {{ic|video/x-ms-wmv}}. The second part of the MIME-type is expanding faster, for example with new applications or data encoding standards.
 +
 
 +
The default applications to open a MIME-type are usually stored in [[#Default mimeapps.list paths|mimeapps.list]] files, but some programs store MIME-type associations in their own custom configuration files. If a MIME-type association is not found in the default configuration, the program should look for the first match of the MIME-type in ''.desktop'' files.
 +
 
 +
Description of some MIME-type first part and examples of second parts:
 +
 
 +
{| class="wikitable"
 +
! Type of MIME (1st part) !! Description !! Examples of extension (2nd part)
 +
|-
 +
|application || Files with binary content such, e.g.: documents,archives,... || epub+zip, ereader, excel, gbr, gzip
 +
|-
 +
|audio || Audio files that that can be played by a music player or audio editor ||  flac, m4a, midi
 +
|-
 +
|chemical || Chemical information, molecular and other chemical data  || x-cif, x-cml, x-daylight-smiles, x-gamess-input, x-gamess-output, x-gaussian-checkpoint, x-gaussian-cube, x-gaussian-log, x-mopac-out, x-pdb, x-qchem-output, x-xyz
 +
|-
 +
|image || Image files that can be opened by image editor or image viewer || bmp, crw, g3fax, gif, jp2, jpeg, jpeg2000, jpg
 +
|-
 +
|inode || Can be opened by a file manager || blockdevice, chardevice, directory, fifo, mount-point, socket, symlink
 +
|-
 +
|message || Message protocols || delivery-status, disposition-notification, external-body, news, partial, rfc822, x-gnu-rmail
 +
|-
 +
|misc || Streaming meta data || ultravox
 +
|-
 +
|text || Text documents that can be viewed (e.g. with command ''less'') or opened with a text editor || html, javascript, mathml, mml, plain
 +
|-
 +
|video || Video files that can be played or edited with a video editor || flv, mp2t, mp4
 +
|-
 +
|x-content || Content on disks such as e.g. Audio,Video,Image or blank disk || audio-cdda, audio-player, blank-bd, blank-cd, blank-dvd, blank-hddvd, image-picturecd, video-dvd, video-svcd, video-vcd
 +
|-
 +
|x-scheme-handler || Internet protocol || ftp, geo, ghelp, help, http, https, hwplay, icy, icyx, info, irc, magnet, mailto, man, mms, mmsh, net, pnm, rtmp, rtp, rtsp, skype, uvox, vnc, xmpp
 +
|-
 +
|x-epoc || SISX package ||x-sisx-app
 +
|-
 +
|multipart || Multi-part mime messages ||alternative, appledouble, digest, encrypted, mixed, related, report, signed, x-mixed-replace
 +
|-
 +
|model || such as 3D model || x-kpovmodeler, vrml, x-modelica
 +
|}
 +
 
 +
For the description of MIME-types you can search in XDG database:
 +
$ grep -e 'mime-type type=' -e '<comment>'  /usr/share/mime/packages/freedesktop.org.xml
 +
 
 +
{{Tip|To see all MIME extensions in the system's ''.desktop'' files that belongs to a MIME-type you can use {{AUR|lsdesktopf}}, for example {{ic|lsdesktopf --gm -gx video}}.}}
 +
 
 +
If you ever require to create a custom association of a new file extension to a MIME-type, see the
 +
[http://standards.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html Association between MIME types and applications] standard.
 +
 
 +
=== Default mimeapps.list paths ===
 +
 
 +
The {{ic|mimeapps.list}} file stores the configuration for the default application to open a MIME-type.
 +
There are different locations for it:
 +
* system-wide,
 +
* per-user,
 +
* custom locations used by some programs.
 +
 
 +
The {{ic|''$desktop''}} in the following list denotes the name of the related desktop environment or window manager. The search order of paths is:
 +
 
 +
{| class="wikitable"
 +
! Path !! Usage
 +
|-
 +
| {{ic|$HOME/.config/''$desktop''-mimeapps.list}} || user overrides, desktop-specific
 +
|-
 +
| {{ic|$HOME/.config/mimeapps.list}} || user overrides
 +
|-
 +
| {{ic|/etc/xdg/''$desktop''-mimeapps.list}} || sysadmin and vendor overrides, desktop-specific
 +
|-
 +
| {{ic|/etc/xdg/mimeapps.list}} || sysadmin and vendor overrides
 +
|-
 +
| {{ic|$HOME/.local/share/applications/''$desktop''-mimeapps.list}} || for compatibility but now deprecated, desktop-specific
 +
|-
 +
| {{ic|$HOME/.local/share/applications/mimeapps.list}} || for compatibility but now deprecated
 +
|-
 +
| {{ic|/usr/local/share/applications/''$desktop''-mimeapps.list}}<br>{{ic|/usr/share/applications/$desktop-mimeapps.list}} || distribution-provided defaults, desktop-specific
 +
|-
 +
| {{ic|/usr/local/share/applications/mimeapps.list}}<br>{{ic|/usr/share/applications/mimeapps.list}} || distribution-provided defaults
 +
|}
 +
 
 +
=== Description of the mimeapps.list file ===
 +
 
 +
The file contains two main sections for default and additional alternatives to open files of a MIME-type. The second and the third section ({{ic|[Removed Associations]}}) are optional.
 +
 
 +
Example:
  
 
  [Default Applications]
 
  [Default Applications]
  text/html=firefox.desktop;chromium.desktop
+
  mimetype1=default1.desktop
  
A distribution could also ship a {{ic|/usr/share/applications/mimeapps.list}} to provide system-wide defaults, but Arch Linux does not do this. To override these system-wide defaults, one can add {{ic|[Added Associations]}} section is used to specify preferred (default) applications in decreasing order of preference for the specified MIME type. The {{ic|[Removed Associations]}} section is used to explicitly remove any previously inherited associations.
+
Defined additional associations of applications to MIME-types:
 +
[Added Associations]
 +
mimetype1=foo1.desktop;foo2.desktop;foo3.desktop;
 +
mimetype2=foo4.desktop;
  
=== xdg-open ===
+
Removed associations of applications with MIME-types (blacklisting a MIME-type association of an application in its {{ic|.desktop}} file):
[[xdg-open]] is a desktop-independent tool for starting default applications. Many applications invoke the {{ic|xdg-open}} command internally. xdg-open uses xdg-mime to query {{ic|~/.local/share/applications/mimeapps.list}} (among other things; if you use a mainstream DE like GNOME, KDE or LXDE, xdg-open might try using their specific tools before xdg-mime) to find the MIME type of the file that is to be opened and the default application associated with that MIME type.  
+
[Removed Associations]
 +
mimetype1=foo5.desktop
  
See [[xdg-open]] for more information.  
+
Multiple ''.desktop'' files for a single MIME-type must be semicolon-separated. Not supported entries are ignored in {{ic|mimeapps.list}}. The DE/WM then searches for the first match to the needed MIME-type them in the default path for ''.desktop'' files.  
  
=== Custom file associations ===
+
{{Tip|To get a quick overview of how many and which ''.desktop'' files can be associated with a certain MIME-type, you can use the {{AUR|lsdesktopf}} utility, e.g. {{ic|lsdesktopf --gen-mimeapps}}.}}
The following method creates a custom mime type and file association manually. This is useful if your desktop does not have a mime type/file association editor installed.  In this example, a fictional multimedia application 'foobar' will be associated with all {{ic|*.foo}} files. This will only affect the current user.
+
  
* First, create the file {{ic|~/.local/share/mime/packages/application-x-foobar.xml}}:
+
{{Note|Arch Linux itself does not provide system-wide presets for associations, but other distributions and specific desktop environments may do so via packaged {{ic|mimeapps.list}} files, or the older and deprecated {{ic|defaults.list}} files.}}
  
$ mkdir -p ~/.local/share/mime/packages
+
Your choice of desktop environment, or none, will affect how your default applications are associated. Further, note that some packages use additional work-around presets, for example Mozilla's packages install a {{ic|/etc/mime.types}} file.
$ cd ~/.local/share/mime/packages
+
$ touch application-x-foobar.xml
+
  
* Then edit {{ic|~/.local/share/mime/packages/application-x-foobar.xml}} and add this text:
+
== Shared MIME-info database ==
{{bc|<nowiki>
+
 
 +
In background to the {{ic|mimeapps.list}} files, the system holds a database of MIME-type information registered via the installed applications' ''.desktop'' files, the [https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.11.html#idm139839923550176 Shared MIME-info Database]. It is created automatically as soon as an application depending on it is installed, via the following [[pacman#Hooks]]:
 +
* {{ic|/usr/share/libalpm/hooks/update-mime-database.hook}} from {{Pkg|shared-mime-info}}
 +
:updates the MIME-info database in {{ic|/usr/share/mime}}, in particular also the ''.xml'' specification in {{ic|/usr/share/mime/packages/freedesktop.org.xml}} for the MIME-types standards
 +
* {{ic|/usr/share/libalpm/hooks/update-desktop-database.hook}} from {{Pkg|desktop-file-utils}}
 +
:updates the {{ic|mimeinfo.cache}} located (per default) in {{ic|/usr/share/applications}}
 +
 
 +
These files keep track of which MIME-types are associated with which ''.desktop'' files overall. When an application is installed, updated or removed, the pacman hooks keep the database updated accordingly.
 +
 
 +
{{Warning|The database files are not meant to be edited directly.}}
 +
 
 +
Application specific configuration is stored in ''.xml'' files and further files of the database (see [https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.11.html#idm139839923550176 .xml source files]) are stored in ''.keys'' and ''.mime'' files that are located in {{ic|/usr/share/mime-info/}}.
 +
 
 +
Global directories for the ''.xml'' files are:
 +
 
 +
/usr/share/mimelnk/application/
 +
/usr/share/mime/packages/
 +
 
 +
{{Warning|Above should not be modified, for user-specific configuration see below.}}
 +
 
 +
Any user-specific ''.xml'' configuration may be stored in:
 +
~/.local/share/mime/packages/
 +
 
 +
=== Example: .xml and related .desktop configuration ===
 +
 
 +
{{Accuracy|What's the point of this example? The standard way of user customizations is described [https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html#associations here] (that is, assuming that the involved MIME type is already registered by a package).}}
 +
 
 +
The following is a short example to create related files, user-specific paths are used to override system defaults, while keeping the integrity of the system-wide configuration.
 +
 
 +
Create and edit {{ic|~/.local/share/mime/packages/application-x-foobar.xml}}:
 +
 
 +
{{bc|1=
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
+
<nowiki><mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"></nowiki>
<mime-type type="application/x-foobar">
+
    '''<mime-type type="application/x-foobar">'''
<comment>foo file</comment>
+
        <comment>foo file</comment>
<icon name="application-x-foobar"/>
+
        <icon name="application-x-foobar"/>
<glob-deleteall/>
+
        <glob-deleteall/>
<glob pattern="*.foo"/>
+
        '''<glob pattern="*.foo"/>'''
</mime-type>
+
    </mime-type>
 
</mime-info>
 
</mime-info>
</nowiki>}}
+
}}
Note that you can use any icon, including one for another application.
+
  
* Next, edit or create the file {{ic|~/.local/share/applications/foobar.desktop}} to contain something like:
+
Create a related [[desktop entry]] file:
{{bc|<nowiki>
+
 
 +
{{hc|~/.local/share/applications/foobar.desktop|2=
 
[Desktop Entry]
 
[Desktop Entry]
 
Name=Foobar
 
Name=Foobar
 
Exec=/usr/bin/foobar
 
Exec=/usr/bin/foobar
MimeType=application/x-foobar
+
'''MimeType=application/x-foobar'''
 
Icon=foobar
 
Icon=foobar
 
Terminal=false
 
Terminal=false
Line 59: Line 177:
 
Categories=AudioVideo;Player;Video;
 
Categories=AudioVideo;Player;Video;
 
Comment=
 
Comment=
</nowiki>}}
+
}}
Note that Categories should be set appropriately for the application type (in this example, a multimedia app).
+
 
 +
Now update the application and mime database with:
 +
 
 +
$ update-desktop-database ~/.local/share/applications
 +
$ update-mime-database    ~/.local/share/mime
 +
 
 +
Programs that use MIME-types, such as file managers, should now open {{ic|*.foo}} files with foobar  (you may need to restart your file manager to see the change.)
 +
 
 +
See also [[Environment variables#Examples]] about global variables that can be used in start-up scripts to set default applications for specific actions.
 +
 
 +
== Utilities to manage MIME types ==
 +
 
 +
Many of the file managers has options to set up and configure associations of mime types with programs.
 +
 
 +
It can be done by using:
 +
 
 +
* Preferences for selected file
 +
* File manager configuration menu
 +
* External program for a specific file manager
 +
 
 +
Other utilities to manage MIME-types are:
 +
 
 +
{| class="wikitable"
 +
! Name/Package !! Method !! Based on !! xdg-utils replacement !! Configuration file
 +
|-
 +
| {{Pkg|mime-editor}} || MIME-type || Utility for [http://rox.sourceforge.net/desktop/home.html ROX] applications || ||
 +
|-
 +
| {{AUR|ayr}} || MIME-type <br> name <br> regex || {{Pkg|file}} <br> {{Pkg|perl-file-mimeinfo}}, etc|| || {{ic|mimeapps.list}} <br> {{ic|defaults.list}}
 +
|-
 +
| {{AUR|busking-git}} || regex || {{Pkg|perl-file-mimeinfo}} || yes || custom
 +
|-
 +
| {{AUR|linopen}} || || {{Pkg|file}} || || custom
 +
|-
 +
| {{AUR|mimeo}} || MIME-type <br> regex || {{Pkg|file}} || {{AUR|xdg-utils-mimeo}} ||{{ic|mimeapps.list}} <br> {{ic|defaults.list}} <br> custom is optional
 +
|-
 +
| {{AUR|mimi-git}} || || {{Pkg|file}} || yes || custom
 +
|-
 +
| {{ic|rifle}} <br> part of {{Pkg|ranger}} || MIME-type <br> name <br> regex ||  || || custom
 +
|-
 +
| {{AUR|sx-open}} || regex || {{Pkg|file}} <br> bash regex || yes || custom
 +
|-
 +
| {{AUR|whippet}} || MIME-type <br> name <br> regex || SQLite database <br> {{Pkg|file}} <br> {{Pkg|perl-file-mimeinfo}}, etc || || custom SQLite database <br> {{ic|mimeapps.list}}
 +
|-
 +
| {{Pkg|xdg-utils}} ||  || {{Pkg|file}} <br> {{Pkg|perl-file-mimeinfo}} || ||
 +
|}
 +
 
 +
=== Examples of usage ===
 +
 
 +
Here is a short description about how to use command line tools to show MIME-type of a file or set preferred program as default to open MIME-type.
 +
 
 +
==== Detect MIME-type ====
 +
 
 +
Tools detecting MIME-type by reading meta-data from header of the file or detecting by magic number that is in two bytes identifier in the begin of the file. See [[Wikipedia:File format#File header|File header]].
 +
 
 +
Many programs are using command ''file'' to detect correct MIME-type. For detection it uses compiled database that is stored in the {{ic|/usr/share/misc/magic/}} directory. It has many options to determinate correct MIME-type and for showing output.
 +
 
 +
In Linux it is two standards to detect MIME-type that can affect which program will start and open the file, e.g. extension is associated with one program but content is associated with another MIME-type. The simplest way to see what is your system prioritizes is by renaming file extension and check again with tools that can use custom [[#Associate file extensions with applications MIME-type|*.xml]] configuration files.
 +
 
 +
Here is examples of utility ''xdg-mime'' that first checking association of extension in [[#Associate file extensions with applications MIME-type|*.xml]] configuration files.
 +
{| class="wikitable"
 +
! Without  extension !! With extension
 +
|-
 +
|xdg-mime query filetype ''foo-file''  || xdg-mime query filetype ''foo-file''.''jpg''
 +
|-
 +
! application/vnd.oasis.opendocument.text ||  image/jpeg
 +
|-
 +
|}
 +
 
 +
Comparison functionality of the tools
 +
 
 +
Here is shown options only for simple or multiple checks of a file, for more options read their own documentation.
 +
{| class="wikitable"
 +
! Tool !! Option !! Detection order !! Functionality !! Type !! Interface
 +
|-
 +
|file || ''(has many)'' || Magic(Byte/Pattern) || Show only || binary || terminal
 +
|-
 +
|xdg-mime || query filetype || *.xml -> Magic || Show / Set || script || terminal
 +
|-
 +
|mimetype || -i ''(*.xml)'' -M ''(Magic)'' || *.xml -> Magic || Show only || script || terminal
 +
|-
 +
|mimeo || -m || *.xml -> Magic  || Show / Set / Launch ||  script || terminal
 +
|}
 +
 
 +
==== Set use of MIME-type by default ====
 +
 
 +
Comparison functionality of the tools
 +
 
 +
Here is shown options only for single file associations, for more options read their own documentation.
 +
 
 +
{| class="wikitable"
 +
! Tool !! Option !!  Functionality !! Type !! Interface
 +
|-
 +
|xdg-mime || default ''*.desktop'' Type1/Extension1 Type2/Extension2 ||  Show / Set || script || terminal
 +
|-
 +
|mimeo || --prefer 'regex:^Type/(Extension'''1'''<nowiki> |</nowiki>Extension?'''2''')$'  ''*.desktop'' || Show / Set / Launch ||  script || terminal
 +
|-
 +
|mimeopen || --ask-default ''(interactive)'' || Set / Launch  || script || terminal
 +
|}
 +
 
 +
=== Application launchers ===
 +
 
 +
{{App|xdg-open|Starting applications from command line, has basic functionality.|https://www.freedesktop.org/wiki/Software/xdg-utils/|{{Pkg|xdg-utils}}}}
 +
 
 +
''xdg-open'' uses {{Pkg|perl-file-mimeinfo}} as a fallback ("generic") method if no [[desktop environment]] is detected. It is a desktop-independent tool. Many applications invoke the {{ic|xdg-open}} command internally. Inside a [[desktop environment]] it passes the arguments to desktop supported environment's file-opener applications (e.g. ''gvfs-open'', ''kde-open'', or ''exo-open''). When no desktop environment is detected the ''xdg-open'' will use its own configuration files.
 +
 
 +
{{App|mimeopen|Starting applications from command line. It can use custom database and prompt user to chose default application from a list of detected relevant and offers to chose own alternative.|https://metacpan.org/release/File-MimeInfo|{{Pkg|perl-file-mimeinfo}}}}
 +
 
 +
Example of the prompt:
 +
 
 +
{{hc| mimeopen -d /path/to/foo-file|
 +
Please choose a default application for files of type ''Type''/''Extension''
 +
        1) notepad  (wine-extension-txt)
 +
        2) Leafpad  (leafpad)
 +
        3) OpenOffice.org Writer  (writer)
 +
        4) gVim  (gvim)
 +
        5) Other...
 +
}}
 +
Your answer becomes the default handler for that type of file. Mimeopen is installed as {{ic|/usr/bin/vendor_perl/mimeopen}}.
 +
 
 +
If you run ''xdg-open'' without a desktop environment, you should also install {{Pkg|perl-file-mimeinfo}}, or {{AUR|xdg-utils-mimeo}} and {{AUR|mimeo}} from the [[AUR]] for a faster alternative.
 +
 
 +
==== Miscellaneous ====
 +
 
 +
The [http://linux.die.net/man/4/mailcap mailcap] file.
 +
 
 +
{{Expansion|When using {{AUR|run-mailcap}}, ''xdg-open'' may refer to it.[http://cgit.freedesktop.org/xdg/xdg-utils/tree/scripts/xdg-open.in#n266] It should then clearly not be combined with the below file to prevent endless loops.}}
 +
 
 +
The ''.mailcap'' file format is used by mail programs such as {{Pkg|mutt}} and {{Pkg|sylpheed}}. To have those programs use ''xdg-open'', edit {{ic|~/.mailcap}}:
 +
 
 +
{{hc|~/.mailcap|
 +
*/*; xdg-open "%s"
 +
}}
 +
 
 +
=== Extended practical examples ===
 +
 
 +
==== xdg-open ====
 +
 
 +
''xdg-mime'' modifies the local file {{ic|~/.config/mimeapps.list}} and {{ic|~/.local/share/applications/mimeapps.list}} (deprecated).
 +
 
 +
To '''query''' the mime type used by an existing file, use
 +
$ xdg-mime query ''filetype'' ''file.ext''
 +
 
 +
To change an associated desktop entry by setting [[Thunar]] as the default file browser:
 +
 
 +
$ xdg-mime default Thunar.desktop inode/directory
 +
Note that you should not specify the complete path, but only the name of the ''.desktop'' file.
 +
 
 +
This command can take multiple mime-types, allowing related files to be handled by the same program. The example below associates [[Emacs]] to all known source files:
 +
$ xdg-mime default emacs.desktop $(grep '^text/x-*' /usr/share/mime/types)
 +
 
 +
===== Set the default browser =====
 +
 
 +
To set the default application for {{ic|http(s)://}} internet protocols:
 +
 
 +
$ xdg-mime default midori.desktop x-scheme-handler/http
 +
$ xdg-mime default midori.desktop x-scheme-handler/https
 +
 
 +
As an alternative try:
 +
 
 +
$ xdg-settings set default-web-browser netsurf.desktop
 +
 
 +
To verify if the URLs opens correctly:
 +
 
 +
$ xdg-open <nowiki>https://archlinux.org</nowiki>
 +
 
 +
To associate ''.html'' files with the {{Pkg|netsurf}} web-browser:
 +
 
 +
$ xdg-mime default netsurf.desktop text/html
  
* Now update the mime database with:
+
== Troubleshooting ==
$ update-mime-database ~/.local/share/mime
+
  
Programs that use mime types, such as file managers, should now open {{ic|*.foo}} files with foobar.  (You may need to restart your file manager to see the change.)
+
=== Variables in .desktop files that affect application launch ===
  
=== Maintaining settings for multiple desktop environments ===
+
Desktop environments and file managers supporting the specifications launch programs according to definition in the ''.desktop'' files. See [[Desktop entries#Application entry]].  
The {{ic|OnlyShowIn}} field of a .desktop file may be useful; see [http://standards.freedesktop.org/menu-spec/latest/ this page]. I haven't tried setting this field yet; please update this wiki page if you have any info about using {{ic|OnlyShowIn}}.
+
  
== Using environment variables ==
+
Usually, configuration of the packaged ''.desktop'' files is not required, but it may not be bug-free. Even if an application containing necessary MIME-type description in the ''.desktop'' file {{ic|MimeType}} variable that is used for association, it can fail to start correctly, not start at all or start without opening a file.
Most non-graphical programs use [[Environment Variables]], such as {{ic|EDITOR}} or {{ic|BROWSER}}. These can be set in your terminal's autostart file (e.g. {{ic|~/.bashrc}}):
+
  
{{hc|~/.bashrc|<nowiki>
+
This may happen, for example, if the {{ic|Exec}} variable is missing internal options needed for how to open a file, or how the application is shown in the menu. The {{ic|Exec}} variable usually begins with {{ic|%}}; for its currently supported options, see [https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables exec-variables].
export EDITOR="nano"
+
export BROWSER="firefox"
+
</nowiki>}}
+
  
== Troubleshooting ==
+
The following table lists the main variable entries of ''.desktop'' files that affect how an application starts, if it has a MIME-type associated with it.  
=== Applications don't appear in the ''Open With...'' context menu (of a file manager) ===
+
Sometimes, a certain application will not appear in the right-click ''Open With...'' dialog. To fix this problem, locate the {{Ic|.desktop}} file in {{Ic|/usr/share/applications}}, edit it as root, and add {{Ic|%U}} to the end of the {{Ic|1=<nowiki>Exec=</nowiki>}} line. For example, Kile currently has this problem; you need to edit {{Ic|/usr/share/applications/kde4/kile.desktop}} and change the line reading {{Ic|1=<nowiki>Exec=kile</nowiki>}} to read {{Ic|1=<nowiki>Exec=kile %U</nowiki>}}. Also, please file a bug against the upstream project if you notice this problem.
+
  
You may also have to edit the {{Ic|MimeType}} list in the {{Ic|.desktop}} file if you install extensions that allow an application to handle additional MIME types.
+
{| class="wikitable"
 +
! Variable names !! Example 1 content !! Example 2 content !! Description
 +
|-
 +
| DBusActivatable || DBusActivatable=true || DBusActivatable=false || Application interact with [https://www.freedesktop.org/wiki/Software/dbus/ D-Bus]. <br> See also configuration: [https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#dbus D-Bus].
 +
|-
 +
| MimeType || MimeType=application/vnd.oasis.opendocument.text || MimeType=application/vnd.sun.xml.math || List of MIME types supported by application
 +
|-
 +
| StartupWMClass || StartupWMClass=google-chrome || StartupWMClass=xpad || Associate windows with the owning application
 +
|-
 +
| Terminal || Terminal=true || Terminal=false || Start in default terminal
 +
|}

Latest revision as of 18:33, 24 May 2016

The type of a data file is usually, but not necessarily, denoted by the extension of the file name, for example .html or .jpeg. A second way to determine the data type is via the so-called "magic bytes" at the start of the file. Both ways determine the MIME-type of the data.

Each application installed to a system provides information about the MIME-types it can handle via the Desktop entries, but frequently there is more than one application able to handle data of a certain MIME-type. This article describes how the default applications to handle a MIME-type can be configured.

Note that while Arch Linux does not provide custom presets for default applications, the desktop environment you install may do so. Some desktop environments also provide a GUI or a file-manager which enables to interactively configure default applications for certain file extensions. If this suffices for your system usage, you may not need to configure anything further.

The scope of this article is the freedesktop Association between MIME types and applications specification.

MIME types and desktop entries

MIME-types are specified by two slash (/) parts: Type/Extension, for example video/x-ms-wmv. The second part of the MIME-type is expanding faster, for example with new applications or data encoding standards.

The default applications to open a MIME-type are usually stored in mimeapps.list files, but some programs store MIME-type associations in their own custom configuration files. If a MIME-type association is not found in the default configuration, the program should look for the first match of the MIME-type in .desktop files.

Description of some MIME-type first part and examples of second parts:

Type of MIME (1st part) Description Examples of extension (2nd part)
application Files with binary content such, e.g.: documents,archives,... epub+zip, ereader, excel, gbr, gzip
audio Audio files that that can be played by a music player or audio editor flac, m4a, midi
chemical Chemical information, molecular and other chemical data x-cif, x-cml, x-daylight-smiles, x-gamess-input, x-gamess-output, x-gaussian-checkpoint, x-gaussian-cube, x-gaussian-log, x-mopac-out, x-pdb, x-qchem-output, x-xyz
image Image files that can be opened by image editor or image viewer bmp, crw, g3fax, gif, jp2, jpeg, jpeg2000, jpg
inode Can be opened by a file manager blockdevice, chardevice, directory, fifo, mount-point, socket, symlink
message Message protocols delivery-status, disposition-notification, external-body, news, partial, rfc822, x-gnu-rmail
misc Streaming meta data ultravox
text Text documents that can be viewed (e.g. with command less) or opened with a text editor html, javascript, mathml, mml, plain
video Video files that can be played or edited with a video editor flv, mp2t, mp4
x-content Content on disks such as e.g. Audio,Video,Image or blank disk audio-cdda, audio-player, blank-bd, blank-cd, blank-dvd, blank-hddvd, image-picturecd, video-dvd, video-svcd, video-vcd
x-scheme-handler Internet protocol ftp, geo, ghelp, help, http, https, hwplay, icy, icyx, info, irc, magnet, mailto, man, mms, mmsh, net, pnm, rtmp, rtp, rtsp, skype, uvox, vnc, xmpp
x-epoc SISX package x-sisx-app
multipart Multi-part mime messages alternative, appledouble, digest, encrypted, mixed, related, report, signed, x-mixed-replace
model such as 3D model x-kpovmodeler, vrml, x-modelica

For the description of MIME-types you can search in XDG database:

$ grep -e 'mime-type type=' -e '<comment>'  /usr/share/mime/packages/freedesktop.org.xml
Tip: To see all MIME extensions in the system's .desktop files that belongs to a MIME-type you can use lsdesktopfAUR, for example lsdesktopf --gm -gx video.

If you ever require to create a custom association of a new file extension to a MIME-type, see the Association between MIME types and applications standard.

Default mimeapps.list paths

The mimeapps.list file stores the configuration for the default application to open a MIME-type. There are different locations for it:

  • system-wide,
  • per-user,
  • custom locations used by some programs.

The $desktop in the following list denotes the name of the related desktop environment or window manager. The search order of paths is:

Path Usage
$HOME/.config/$desktop-mimeapps.list user overrides, desktop-specific
$HOME/.config/mimeapps.list user overrides
/etc/xdg/$desktop-mimeapps.list sysadmin and vendor overrides, desktop-specific
/etc/xdg/mimeapps.list sysadmin and vendor overrides
$HOME/.local/share/applications/$desktop-mimeapps.list for compatibility but now deprecated, desktop-specific
$HOME/.local/share/applications/mimeapps.list for compatibility but now deprecated
/usr/local/share/applications/$desktop-mimeapps.list
/usr/share/applications/$desktop-mimeapps.list
distribution-provided defaults, desktop-specific
/usr/local/share/applications/mimeapps.list
/usr/share/applications/mimeapps.list
distribution-provided defaults

Description of the mimeapps.list file

The file contains two main sections for default and additional alternatives to open files of a MIME-type. The second and the third section ([Removed Associations]) are optional.

Example:

[Default Applications]
mimetype1=default1.desktop

Defined additional associations of applications to MIME-types:

[Added Associations]
mimetype1=foo1.desktop;foo2.desktop;foo3.desktop;
mimetype2=foo4.desktop;

Removed associations of applications with MIME-types (blacklisting a MIME-type association of an application in its .desktop file):

[Removed Associations]
mimetype1=foo5.desktop

Multiple .desktop files for a single MIME-type must be semicolon-separated. Not supported entries are ignored in mimeapps.list. The DE/WM then searches for the first match to the needed MIME-type them in the default path for .desktop files.

Tip: To get a quick overview of how many and which .desktop files can be associated with a certain MIME-type, you can use the lsdesktopfAUR utility, e.g. lsdesktopf --gen-mimeapps.
Note: Arch Linux itself does not provide system-wide presets for associations, but other distributions and specific desktop environments may do so via packaged mimeapps.list files, or the older and deprecated defaults.list files.

Your choice of desktop environment, or none, will affect how your default applications are associated. Further, note that some packages use additional work-around presets, for example Mozilla's packages install a /etc/mime.types file.

Shared MIME-info database

In background to the mimeapps.list files, the system holds a database of MIME-type information registered via the installed applications' .desktop files, the Shared MIME-info Database. It is created automatically as soon as an application depending on it is installed, via the following pacman#Hooks:

updates the MIME-info database in /usr/share/mime, in particular also the .xml specification in /usr/share/mime/packages/freedesktop.org.xml for the MIME-types standards
updates the mimeinfo.cache located (per default) in /usr/share/applications

These files keep track of which MIME-types are associated with which .desktop files overall. When an application is installed, updated or removed, the pacman hooks keep the database updated accordingly.

Warning: The database files are not meant to be edited directly.

Application specific configuration is stored in .xml files and further files of the database (see .xml source files) are stored in .keys and .mime files that are located in /usr/share/mime-info/.

Global directories for the .xml files are:

/usr/share/mimelnk/application/
/usr/share/mime/packages/ 
Warning: Above should not be modified, for user-specific configuration see below.

Any user-specific .xml configuration may be stored in:

~/.local/share/mime/packages/

Example: .xml and related .desktop configuration

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: What's the point of this example? The standard way of user customizations is described here (that is, assuming that the involved MIME type is already registered by a package). (Discuss in Talk:Default applications#)

The following is a short example to create related files, user-specific paths are used to override system defaults, while keeping the integrity of the system-wide configuration.

Create and edit ~/.local/share/mime/packages/application-x-foobar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
    <mime-type type="application/x-foobar">
        <comment>foo file</comment>
        <icon name="application-x-foobar"/>
        <glob-deleteall/>
        <glob pattern="*.foo"/>
    </mime-type>
</mime-info>

Create a related desktop entry file:

~/.local/share/applications/foobar.desktop
[Desktop Entry]
Name=Foobar
Exec=/usr/bin/foobar
MimeType=application/x-foobar
Icon=foobar
Terminal=false
Type=Application
Categories=AudioVideo;Player;Video;
Comment=

Now update the application and mime database with:

$ update-desktop-database ~/.local/share/applications
$ update-mime-database    ~/.local/share/mime

Programs that use MIME-types, such as file managers, should now open *.foo files with foobar (you may need to restart your file manager to see the change.)

See also Environment variables#Examples about global variables that can be used in start-up scripts to set default applications for specific actions.

Utilities to manage MIME types

Many of the file managers has options to set up and configure associations of mime types with programs.

It can be done by using:

  • Preferences for selected file
  • File manager configuration menu
  • External program for a specific file manager

Other utilities to manage MIME-types are:

Name/Package Method Based on xdg-utils replacement Configuration file
mime-editor MIME-type Utility for ROX applications
ayrAUR MIME-type
name
regex
file
perl-file-mimeinfo, etc
mimeapps.list
defaults.list
busking-gitAUR regex perl-file-mimeinfo yes custom
linopenAUR file custom
mimeoAUR MIME-type
regex
file xdg-utils-mimeoAUR mimeapps.list
defaults.list
custom is optional
mimi-gitAUR file yes custom
rifle
part of ranger
MIME-type
name
regex
custom
sx-openAUR regex file
bash regex
yes custom
whippetAUR MIME-type
name
regex
SQLite database
file
perl-file-mimeinfo, etc
custom SQLite database
mimeapps.list
xdg-utils file
perl-file-mimeinfo

Examples of usage

Here is a short description about how to use command line tools to show MIME-type of a file or set preferred program as default to open MIME-type.

Detect MIME-type

Tools detecting MIME-type by reading meta-data from header of the file or detecting by magic number that is in two bytes identifier in the begin of the file. See File header.

Many programs are using command file to detect correct MIME-type. For detection it uses compiled database that is stored in the /usr/share/misc/magic/ directory. It has many options to determinate correct MIME-type and for showing output.

In Linux it is two standards to detect MIME-type that can affect which program will start and open the file, e.g. extension is associated with one program but content is associated with another MIME-type. The simplest way to see what is your system prioritizes is by renaming file extension and check again with tools that can use custom *.xml configuration files.

Here is examples of utility xdg-mime that first checking association of extension in *.xml configuration files.

Without extension With extension
xdg-mime query filetype foo-file xdg-mime query filetype foo-file.jpg
application/vnd.oasis.opendocument.text image/jpeg

Comparison functionality of the tools

Here is shown options only for simple or multiple checks of a file, for more options read their own documentation.

Tool Option Detection order Functionality Type Interface
file (has many) Magic(Byte/Pattern) Show only binary terminal
xdg-mime query filetype *.xml -> Magic Show / Set script terminal
mimetype -i (*.xml) -M (Magic) *.xml -> Magic Show only script terminal
mimeo -m *.xml -> Magic Show / Set / Launch script terminal

Set use of MIME-type by default

Comparison functionality of the tools

Here is shown options only for single file associations, for more options read their own documentation.

Tool Option Functionality Type Interface
xdg-mime default *.desktop Type1/Extension1 Type2/Extension2 Show / Set script terminal
mimeo --prefer 'regex:^Type/(Extension1 |Extension?2)$' *.desktop Show / Set / Launch script terminal
mimeopen --ask-default (interactive) Set / Launch script terminal

Application launchers

xdg-open — Starting applications from command line, has basic functionality.

https://www.freedesktop.org/wiki/Software/xdg-utils/ || xdg-utils

xdg-open uses perl-file-mimeinfo as a fallback ("generic") method if no desktop environment is detected. It is a desktop-independent tool. Many applications invoke the xdg-open command internally. Inside a desktop environment it passes the arguments to desktop supported environment's file-opener applications (e.g. gvfs-open, kde-open, or exo-open). When no desktop environment is detected the xdg-open will use its own configuration files.

mimeopen — Starting applications from command line. It can use custom database and prompt user to chose default application from a list of detected relevant and offers to chose own alternative.

https://metacpan.org/release/File-MimeInfo || perl-file-mimeinfo

Example of the prompt:

 mimeopen -d /path/to/foo-file
 Please choose a default application for files of type Type/Extension
        1) notepad  (wine-extension-txt)
        2) Leafpad  (leafpad)
        3) OpenOffice.org Writer  (writer)
        4) gVim  (gvim)
        5) Other...

Your answer becomes the default handler for that type of file. Mimeopen is installed as /usr/bin/vendor_perl/mimeopen.

If you run xdg-open without a desktop environment, you should also install perl-file-mimeinfo, or xdg-utils-mimeoAUR and mimeoAUR from the AUR for a faster alternative.

Miscellaneous

The mailcap file.

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: When using run-mailcapAUR, xdg-open may refer to it.[1] It should then clearly not be combined with the below file to prevent endless loops. (Discuss in Talk:Default applications#)

The .mailcap file format is used by mail programs such as mutt and sylpheed. To have those programs use xdg-open, edit ~/.mailcap:

~/.mailcap
*/*; xdg-open "%s"

Extended practical examples

xdg-open

xdg-mime modifies the local file ~/.config/mimeapps.list and ~/.local/share/applications/mimeapps.list (deprecated).

To query the mime type used by an existing file, use

$ xdg-mime query filetype file.ext

To change an associated desktop entry by setting Thunar as the default file browser:

$ xdg-mime default Thunar.desktop inode/directory

Note that you should not specify the complete path, but only the name of the .desktop file.

This command can take multiple mime-types, allowing related files to be handled by the same program. The example below associates Emacs to all known source files:

$ xdg-mime default emacs.desktop $(grep '^text/x-*' /usr/share/mime/types)
Set the default browser

To set the default application for http(s):// internet protocols:

$ xdg-mime default midori.desktop x-scheme-handler/http
$ xdg-mime default midori.desktop x-scheme-handler/https

As an alternative try:

$ xdg-settings set default-web-browser netsurf.desktop

To verify if the URLs opens correctly:

$ xdg-open https://archlinux.org

To associate .html files with the netsurf web-browser:

$ xdg-mime default netsurf.desktop text/html

Troubleshooting

Variables in .desktop files that affect application launch

Desktop environments and file managers supporting the specifications launch programs according to definition in the .desktop files. See Desktop entries#Application entry.

Usually, configuration of the packaged .desktop files is not required, but it may not be bug-free. Even if an application containing necessary MIME-type description in the .desktop file MimeType variable that is used for association, it can fail to start correctly, not start at all or start without opening a file.

This may happen, for example, if the Exec variable is missing internal options needed for how to open a file, or how the application is shown in the menu. The Exec variable usually begins with %; for its currently supported options, see exec-variables.

The following table lists the main variable entries of .desktop files that affect how an application starts, if it has a MIME-type associated with it.

Variable names Example 1 content Example 2 content Description
DBusActivatable DBusActivatable=true DBusActivatable=false Application interact with D-Bus.
See also configuration: D-Bus.
MimeType MimeType=application/vnd.oasis.opendocument.text MimeType=application/vnd.sun.xml.math List of MIME types supported by application
StartupWMClass StartupWMClass=google-chrome StartupWMClass=xpad Associate windows with the owning application
Terminal Terminal=true Terminal=false Start in default terminal