Difference between revisions of "Default applications"

From ArchWiki
Jump to: navigation, search
(readded <glob-deleteall/> and added update-desktop-database needed by gnome)
(perl-file-mimeinfo: mention replacement of file)
 
(313 intermediate revisions by 18 users not shown)
Line 1: Line 1:
 
[[Category:Desktop environments]]
 
[[Category:Desktop environments]]
[[zh-cn:Default Applications]]
+
[[ja:デフォルトアプリケーション]]
 +
[[ru:Changing filetype association via MIME]]
 +
[[zh-cn:Default applications]]
 
{{Related articles start}}
 
{{Related articles start}}
{{Related|Desktop Environment}}
+
{{Related|Desktop entries}}
{{Related|Window Manager}}
+
{{Related|Desktop environment}}
 +
{{Related|Window manager}}
 
{{Related articles end}}
 
{{Related articles end}}
Default applications can be set for use with particular file types (e.g. the [[Firefox]] web browser for {{ic|HTML}} files). Where undertaken, files may be opened and/or edited with the desired application much faster and more conveniently. There are also numerous methods to configure default applications in Linux. This page will explain the most common methods: File Managers, MIME types, and environment variables.
 
  
== Using File Managers ==
+
There is frequently more than one application able to handle data of a certain type, so users and even some packages assemble lists of default applications for each [[#MIME types|MIME type]]. While the base install of Arch Linux does not define default applications, [[desktop environments]] you install may do so. Some desktop environments also provide a GUI or a file-manager which can interactively configure default applications. If you do not use a desktop environment, you may need to install additional software in order to conveniently manage default applications.
{{tip|This is the recommended method for less experienced users}}
+
Most file managers will allow for specific applications to be set as the defaults for various file types. New defaults may also be set at any time. For example, to set a default application using  {{pkg|thunar}}, the native file manager for [[XFCE]]:
+
  
* {{ic|right-click}} the file-type desired
+
== MIME types ==
* Select {{ic|Open with another application}}
+
* Select the desired application
+
* Ensure that the {{ic|Use as default for this kind of file}} check-box is ticked
+
* Click the {{ic|Open}} button.
+
  
The general process will be very similar for most other popular file managers, including [[PCManFM]] and {{pkg|spacefm}}.
+
Before setting the default application per file type, the file type must be detected. There are two common ways that this detection is done:
  
== Using MIME types and desktop entries ==
+
* using the file name extension e.g. ''.html'' or ''.jpeg''
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>}}.
+
* using a [[w:List of file signatures|magic number]] in the first few bytes of the file
  
The recommended way of specifying the preferred (default) applications for MIME types is via the files called {{ic|mimeapps.list}} which may be stored in the directories {{ic|$XDG_DATA_HOME/applications (usually ~/.local/share/applications)}} for high user-level preferences and {{ic|$XDG_DATA_DIRS/applications (usually /usr/local/share/applications and /usr/share/applications)}} for lower system-level preferences. A {{ic|mimeapps.list}} file may have either or both of the following sections, for example:
+
However it is possible that a single file type is identified by several different magic numbers and file name extensions, therefore [[w:MIME type|MIME types]] are used to represent distinct file types. MIME types are specified by two parts separated by a slash: {{ic|''type''/''subtype''}}. The type describes the general category of the content, while the subtype identifies the specific data type. For example, {{ic|image/jpeg}} is the MIME type for [[w:JPEG|JPEG]] images, while {{ic|video/H264}} is the MIME type for [[w:H.264/MPEG-4 AVC|H.264]] video.
  
[Added Associations]
+
Technically, every MIME type should be registered with the [[w:Internet Assigned Numbers Authority|IANA]][http://www.iana.org/assignments/media-types/media-types.xhtml], however many applications use unofficial MIME types; these often have a type starting with {{ic|x-}}, for example {{ic|x-scheme-handler/https}} for a HTTPS URL. For local use, the [[#MIME database]] can be used by other packages to register new MIME types.
text/html=firefox.desktop;chromium.desktop;opera-browser.desktop;
+
image/jpeg=viewnior.desktop;gpicview.desktop;
+
  
[Removed Associations]
+
=== MIME database ===
text/html=midori.desktop;google-chrome.desktop;
+
image/jpeg=feh.desktop;
+
  
where the {{ic|[Added Associations]}} section specifies preferred (default) applications in decreasing order of preference for the specified MIME type. The {{ic|[Removed Associations]}} section removes any associations made in lower level applications directories.
+
The system maintains a database of recognized MIME types: the [https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.11.html#idm139839923550176 Shared MIME-info Database]. The database is built from the XML files installed by packages in {{ic|/usr/share/mime/packages}} using the tools from the {{Pkg|shared-mime-info}} package.
  
An older way of specifying preferred (default) applications is via files called {{ic|defaults.list}} may be stored in the same applications directories. These files are now generally only used for read-only special purposes, and mimeapps.list files are now favoured instead. A {{ic|defaults.list}} file has the following section, for example:
+
The files in {{ic|/usr/share/mime/}} should not be directly edited, however it is possible to maintain a separate database on a per-user basis in the {{ic|~/.local/share/mime/}} tree.
  
  [Default Applications]
+
==== New MIME types ====
  text/html=firefox.desktop;chromium.desktop;opera-browser.desktop;
+
 
  image/jpeg=viewnior.desktop;gpicview.desktop;
+
{{expansion|Is the process different for assigning an extension to an existing MIME type?}}
 +
 
 +
This example defines a new MIME type {{ic|application/x-foobar}} and assigns it to any file with a name ending in ''.foo''. Simply create the following file:
 +
 
 +
{{hc|~/.local/share/mime/packages/application-x-foobar.xml|2=
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<nowiki><mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"></nowiki>
 +
    '''<mime-type type="application/x-foobar">'''
 +
        <comment>foo file</comment>
 +
        <icon name="application-x-foobar"/>
 +
        <glob-deleteall/>
 +
        '''<glob pattern="*.foo"/>'''
 +
    </mime-type>
 +
</mime-info>
 +
}}
 +
 
 +
And then update the MIME database
 +
 
 +
  $ update-mime-database ~/.local/share/mime
 +
 
 +
Of course this will not have any affect if no desktop entries are associated with the MIME type. You may need to create new [[desktop entries]] or modify [[#XDG standard|mimeapps.list]].
 +
 
 +
=== Desktop entries ===
 +
 
 +
Each package can use [[desktop entries]] to provide information about the MIME types that can be handled by the packaged software. In order to provide fast search in the reverse direction, the system uses the tools from the {{Pkg|desktop-file-utils}} package to analyze the desktop files and to create an inverse mapping stored in the {{ic|/usr/share/applications/mimeinfo.cache}} file. This is the only file that programs need to read to find all desktop files that might be used to handle given MIME type. Using the database is easier and faster than reading hundreds of ''.desktop'' files directly.
 +
 
 +
The files in {{ic|/usr/share/applications/}} should not be edited directly, it is possible to maintain a separate database on a per-user basis in the {{ic|~/.local/share/applications/}} tree. See [[Desktop entries]] for details.
 +
 
 +
== Set default applications ==
 +
 
 +
The configuration of default applications depends on which launcher is used. Unfortunately there are multiple incompatible standards and many programs even have their own custom formats.
 +
 
 +
The most common standards are explained below for manual editing. You can also use some [[#Utilities|utility]] to do the job, which may be using some of these standards internally or its own configuration format.
 +
 
 +
=== Environment variables ===
 +
 
 +
Console programs in particular are configured by setting an appropriate [[environment variable]], e.g. {{ic|BROWSER}} or {{ic|EDITOR}}. See [[Environment variables#Examples]].
 +
 
 +
=== XDG standard ===
 +
 
 +
The [https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html XDG standard] is the most common for configuring desktop environments. Default applications for each MIME type are stored in {{ic|mimeapps.list}} files, which can be stored in several locations. They are searched in the following order, with earlier associations taking precedence over later ones:
 +
 
 +
{| class="wikitable"
 +
! Path !! Usage
 +
|-
 +
| {{ic|~/.config/mimeapps.list}} || user overrides
 +
|-
 +
| {{ic|/etc/xdg/mimeapps.list}} || system-wide overrides
 +
|-
 +
| {{ic|~/.local/share/applications/mimeapps.list}} || ('''deprecated''') user overrides
 +
|-
 +
| {{ic|/usr/local/share/applications/mimeapps.list}}<br>{{ic|/usr/share/applications/mimeapps.list}} || distribution-provided defaults
 +
|}
 +
 
 +
Additionally, it is possible to define [[Desktop environment|desktop-environment]]-specific default applications in a file named {{ic|''desktop''-mimeapps.list}} where {{ic|''desktop''}} is the name of the desktop environment (from the {{ic|XDG_CURRENT_DESKTOP}} environment variable). For example, {{ic|/etc/xdg/xfce-mimeapps.list}} defines system-wide default application overrides for [[XFCE]]. These desktop-specific overrides take precedence over the corresponding non-desktop-specific file. For example, {{ic|/etc/xdg/xfce-mimeapps.list}} takes precedence over {{ic|/etc/xdg/mimeapps.list}} but is still overridden by {{ic|~/.config/mimeapps.list}}.
 +
 
 +
{{Tip|Although deprecated, several applications still read/write to {{ic|~/.local/share/applications/mimeapps.list}}. To simplify maintenance, simply symlink it
 +
  $ ln -s ~/.local/share/applications/mimeapps.list ~/.config/mimeapps.list
 +
the reverse symlink unfortunately does not work because of how {{ic|xdg-mime}} writes to this file.
 +
}}
 +
 
 +
{{Note|You might also find files in these locations named {{ic|defaults.list}}. This file is similar to {{ic|mimeapps.list}} except it only lists default applications (not added/removed associations). It is now deprecated and should be manually merged with {{ic|mimeapps.list}}.}}
 +
 
 +
==== Format ====
 +
 
 +
Consider the following example:
 +
 
 +
{{hc|mimeapps.list|2=
 +
[Added Associations]
 +
image/jpeg=bar.desktop;baz.desktop
 +
video/H264=bar.desktop
 +
[Removed Associations]
 +
video/H264=baz.desktop
 +
[Default Applications]
 +
image/jpeg=foo.desktop}}
 +
 
 +
Each section assigns one or more desktop entries to MIME types.
 +
* '''Added Associations''' indicates that the applications support opening that MIME type. For example, {{ic|bar.desktop}} and {{ic|baz.desktop}} can open JPEG images. This might affect the application list you see when right-clicking a file in a file browser.
 +
* '''Removed Associations''' indicates that the applications ''do not'' support that MIME type. For example, {{ic|baz.desktop}} cannot open H.264 video.
 +
* '''Default Applications''' indicates that the applications should be the default choice for opening that MIME type. For example, JPEG images should be opened with {{ic|foo.desktop}}. This implicitly adds an association between the application and the MIME type. If there are multiple applications, they are tried in order.
 +
 
 +
Each section is optional and can be omitted if unneeded.
 +
 
 +
=== mailcap ===
 +
 
 +
The {{man|4|mailcap|url=http://linux.die.net/man/4/mailcap}} file format is used by mail programs such as {{Pkg|mutt}} and {{Pkg|sylpheed}} to open non-text files. To have those programs use [[#xdg-utils|xdg-open]], edit {{ic|~/.mailcap}}:
 +
 
 +
{{hc|~/.mailcap|
 +
*/*; xdg-open "%s"
 +
}}
 +
 
 +
{{Warning|If you use {{AUR|run-mailcap}}, it is possible for {{ic|xdg-open}} to delegate to it. This will cause an infinite loop if you configured your {{ic|.mailcap}} as described above.}}
 +
 
 +
== Utilities ==
 +
 
 +
While it is possible to configure default applications and MIME types by directly editing [[#XDG standard|mimeapps.list]] and the [[#MIME database]], there are many tools that can simplify the process. These tools are also important because applications may delegate opening of files to these tools rather than trying to implement the MIME type standard themselves.
 +
 
 +
If you use a [[desktop environment]] you should first check if it provides its own utility. That should be preferred over these alternatives.
 +
 
 +
=== xdg-utils ===
 +
 
 +
{{pkg|xdg-utils}} provides the official utilities for managing MIME types and default applications according to the [[#XDG standard]]. Most importantly, it provides {{ic|/usr/bin/xdg-open}} which many applications use to open a file with its default application. It is desktop-environment-independent in the sense that it attempts to use each environment's native default application tool and provides its own mechanism if no known environment is detected. Examples:
 +
 
 +
# determine a file's MIME type
 +
$ xdg-mime query filetype photo.jpeg
 +
  image/jpeg
 +
 +
# determine the default application for a MIME type
 +
$ xdg-mime query default image/jpeg
 +
gimp.desktop
 +
 +
# change the default application for a MIME type
 +
$ xdg-mime default feh.desktop image/jpeg
 +
 +
# open a file with its default application
 +
$ xdg-open photo.jpeg
 +
 +
# shortcut to open all web MIME types with a single application
 +
$ xdg-settings set default-web-browser firefox.desktop
 
   
 
   
Note that Arch Linux itself does not provide any system-wide preferences for associations, but other distributions and specific desktop environments may, via mimeapps.list and/or defaults.list files. Your choice of preferred (default) applications can be conveniently set by several file managers (for example, SpaceFM, Thunar or PCManFM), or you can edit the file {{ic|~/.local/share/applications/mimeapps.list}} yourself as a user. You may also try the "official" utility [[xdg-open#Configuration|xdg-mime]] to set preferred applications, but your mileage to success may vary depending on your desktop environment, or lack of desktop environment.
+
# shortcut for setting the default application for a URL scheme
 +
$ xdg-settings set default-url-scheme-handler irc xchat.desktop
  
=== gnome-defaults-list ===
+
{{Tip|If no desktop environment is detected, MIME type detection falls back to using {{pkg|file}} which&mdash;ironically&mdash;does not implement the XDG standard. If you want {{pkg|xdg-utils}} to work properly without a desktop environment, you will need to install [[#perl-file-mimeinfo]] or another [[#xdg-open alternatives|xdg-open alternative]].}}
{{AUR|gnome-defaults-list}} is available from the [[AUR]], and contains a list of file-types and programs specific to the Gnome desktop. The list is installed to {{ic|/etc/gnome/defaults.list}}.
+
  
Open this file with a text editor. Here you can replace a given application with the name of the program of your choice. For example, the media-player {{ic|totem}} can be replaced with another, such as {{ic|vlc}}. Save the file to {{ic|~/.local/share/applications/defaults.list}}.
+
=== xdg-open alternatives ===
  
=== perl-file-mimeinfo ===
+
Because of the complexity of the [[#xdg-utils]] version of {{ic|xdg-open}}, it can be difficult to debug when the wrong default application is being opened. Because of this, there are many alternatives that attempt to improve upon it. Several of these alternatives replace the {{ic|/usr/bin/xdg-open}} binary, thus changing the default application behavior of most applications. Others simply provide an alternative method of choosing default applications.
{{Pkg|perl-file-mimeinfo}} is available from the official repositories. It can be invoked by using the following command:
+
  
$ mimeopen -d /path/to/file
+
==== perl-file-mimeinfo ====
  
You are asked which application to use when opening {{ic|/path/to/file}}:
+
{{Pkg|perl-file-mimeinfo}} provides the tools {{ic|mimeopen}} and {{ic|mimetype}}. These have a slightly nicer interface than their {{pkg|xdg-utils}} equivalents:
 +
# determine a file's MIME type
 +
$ mimetype photo.jpeg
 +
photo.jpeg: image/jpeg
 +
 +
# choose the default application for this file
 +
$ mimeopen -d photo.jpeg
 +
Please choose an application
 +
 +
    1) Feh (feh)
 +
    2) GNU Image Manipulation Program (gimp)
 +
    3) Pinta (pinta)
 +
 +
use application #
 +
 +
# open a file with its default application
 +
$ mimeopen -n photo.jpeg
  
Please choose a default application for files of type text/plain
+
Most importantly, [[#xdg-utils]] apps will actually call {{ic|mimetype}} instead of {{ic|file}} for MIME type detection, if possible. This is important because {{ic|file}} does not follow the [[#XDG standard]].
        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/perlbin/vendor/mimetype}}.
+
{{Note|But {{pkg|perl-file-mimeinfo}} does not ''entirely'' follow the [[#XDG standard]]. For example it does not read [https://github.com/mbeijen/File-MimeInfo/issues/20 distribution-wide defaults] and it saves its config in [https://github.com/mbeijen/File-MimeInfo/issues/8 deprecated locations].}}
  
=== xdg-open ===
+
==== mimeo ====
[[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.
+
  
See [[xdg-open]] for more information.  
+
{{aur|mimeo}} provides the tool {{ic|mimeo}}, which unifies the functionality of {{ic|xdg-open}} and {{ic|xdg-mime}}.
  
=== Custom file associations ===
+
# determine a file's MIME type
The following method creates a custom mime type and file association manuallyThis is useful if your desktop does not have a mime type/file association editor installedIn this example, a fictional multimedia application 'foobar' will be associated with all {{ic|*.foo}} files. This will only affect the current user.
+
$ mimeo -m photo.jpeg
 +
  photo.jpeg
 +
  image/jpeg
 +
 +
# choose the default application for this MIME type
 +
$ mimeo --add image/jpeg feh.desktop
 +
   
 +
# open a file with its default application
 +
  $ mimeo photo.jpeg
  
* First, create the file {{ic|~/.local/share/mime/packages/application-x-foobar.xml}}:
+
However a big difference with ''xdg-utils'' is that mimeo also supports custom "association files" that allow for more complex associations. For example, passing specific command line arguments based on a regular expression match:
  
  $ mkdir -p ~/.local/share/mime/packages
+
  # open youtube links in VLC without opening a new instance
$ cd ~/.local/share/mime/packages
+
vlc --one-instance --playlist-enqueue %U
$ touch application-x-foobar.xml
+
  ^https?://(www.)?youtube.com/watch\?.*v=
  
* Then edit {{ic|~/.local/share/mime/packages/application-x-foobar.xml}} and add this text:
+
{{aur|xdg-utils-mimeo}} patches ''xdg-utils'' so that {{ic|xdg-open}} falls back to mimeo if no desktop environment is detected.
{{bc|<nowiki>
+
<?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>
+
</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:
+
==== whippet ====
{{bc|<nowiki>
+
[Desktop Entry]
+
Name=Foobar
+
Exec=/usr/bin/foobar
+
MimeType=application/x-foobar
+
Icon=foobar
+
Terminal=false
+
Type=Application
+
Categories=AudioVideo;Player;Video;
+
Comment=
+
</nowiki>}}
+
Note that Categories should be set appropriately for the application type (in this example, a multimedia app).
+
  
* Now update the applications and mime database with:
+
{{aur|whippet}} provides the tool {{ic|whippet}}, which is similar to {{ic|xdg-open}}. It has X11 integration by using {{pkg|libnotify}} to display errors and {{pkg|dmenu}} to display choices between applications to open.
$ update-desktop-database ~/.local/share/applications ;#needed for gnome
+
$ 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.)
+
# open a file with its default application
 +
$ whippet -M photo.jpeg
 +
   
 +
# choose from all possible applications for opening a file (without setting a default)
 +
$ whippet -m photo.jpeg
  
=== Maintaining settings for multiple desktop environments ===
+
In addition to the standard [[#XDG standard|mimeapps.list]], ''whippet'' can also use a SQlite database of weighted application/MIME type/regex associations to determine which app to use.
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 ==
+
==== Naive replacements ====
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>
+
The following packages replace {{pkg|xdg-utils}} however they only provide an {{ic|xdg-open}} script. These versions of {{ic|xdg-open}} do not do any delegation to desktop-environment-specific tools and do not read/write the standard [[#XDG standard|mimeapps.list]] config file (each has its own custom config), so they may not integrate well with other programs that manipulate default applications. However you may find them simpler to use if you do not use a desktop environment.
export EDITOR="nano"
+
 
export BROWSER="firefox"
+
{{Warning|If you need any {{ic|xdg-*}} binaries from {{pkg|xdg-utils}} other than {{ic|xdg-open}} you should not use these applications, since they do not provide them.}}
</nowiki>}}
+
 
 +
{| class="wikitable"
 +
! Package !! Features
 +
|-
 +
| {{AUR|linopen}} || Allows regex rules, can specify fallback file opener
 +
|-
 +
| {{AUR|mimi-git}} || Can change command arguments for each MIME type
 +
|-
 +
| {{AUR|busking-git}} || similar to ''mimi'' but also supports regex rules
 +
|-
 +
| {{AUR|sx-open}} || uses a simple shell-based config file
 +
|-
 +
|}
 +
 
 +
=== lsdesktopf ===
 +
 
 +
{{AUR|lsdesktopf}} provides several methods of searching the MIME database and desktop MIME entries.
 +
 
 +
For example, to see all MIME extensions in the system's ''.desktop'' files that have MIME type {{ic|video}} you can use {{ic|lsdesktopf --gm -gx video}} or to search in the XML database files use {{ic|lsdesktopf --gdx -gx video}}. To get a quick overview of how many and which ''.desktop'' files can be associated with a certain MIME type, use {{ic|lsdesktopf --gen-mimeapps}}. To see all file name extensions in XML database files, use {{ic|lsdesktopf --gdx -gfx}}.
  
 
== Troubleshooting ==
 
== Troubleshooting ==
=== 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.
+
If a file is not being opened by your desired default application, there are several possible causes. You may need to check each case.
 +
 
 +
=== Missing .desktop file ===
 +
 
 +
A [[desktop entry]] is required in order to associate an application with a MIME type. Ensure that such an entry exists and can be used to (manually) open files in the application.
 +
 
 +
=== Missing association ===
 +
 
 +
If the application's desktop entry does not specify the MIME type under its {{ic|MimeType}} key, it will not be considered when an application is needed to open that type. Edit [[#XDG standard|mimeapps.list]] to add an association between the .desktop file and the MIME type.
 +
 
 +
=== Non-default application ===
 +
 
 +
If the desktop entry is associated with the MIME type, it may simply not be set as the default. Edit [[#XDG standard|mimeapps.list]] to set the default association.
 +
 
 +
=== Nonstandard association ===
 +
 
 +
Applications are free to ignore or only partially implement the [[#XDG standard]]. Check for usage of deprecated files such as {{ic|~/.local/share/applications/mimeapps.list}} and {{ic|~/.local/share/applications/defaults.list}}. If you are attempting to open the file from another application (e.g. a web browser or file manager) check if that application has its own method of selecting default applications.
 +
 
 +
=== Variables in .desktop files that affect application launch ===
 +
 
 +
{{Expansion|1=The fact {{ic|MimeType}} entries may be missing in the desktop files is only implied here, i.e. "Even if an application...", though this is a common cause of errors. Some openers may also associate mime types not explicitely listed in a desktop file (such as {{Pkg|exo}}). Further environment-specific factors are at play, e.g. whether {{ic|1=Terminal=true}} has an effect, though latter should arguably be expanded on in [[Desktop entries]].}}
 +
 
 +
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 {{ic|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 {{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].
 +
 
 +
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.  
 +
 
 +
{| 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 05:18, 28 September 2016

There is frequently more than one application able to handle data of a certain type, so users and even some packages assemble lists of default applications for each MIME type. While the base install of Arch Linux does not define default applications, desktop environments you install may do so. Some desktop environments also provide a GUI or a file-manager which can interactively configure default applications. If you do not use a desktop environment, you may need to install additional software in order to conveniently manage default applications.

MIME types

Before setting the default application per file type, the file type must be detected. There are two common ways that this detection is done:

  • using the file name extension e.g. .html or .jpeg
  • using a magic number in the first few bytes of the file

However it is possible that a single file type is identified by several different magic numbers and file name extensions, therefore MIME types are used to represent distinct file types. MIME types are specified by two parts separated by a slash: type/subtype. The type describes the general category of the content, while the subtype identifies the specific data type. For example, image/jpeg is the MIME type for JPEG images, while video/H264 is the MIME type for H.264 video.

Technically, every MIME type should be registered with the IANA[1], however many applications use unofficial MIME types; these often have a type starting with x-, for example x-scheme-handler/https for a HTTPS URL. For local use, the #MIME database can be used by other packages to register new MIME types.

MIME database

The system maintains a database of recognized MIME types: the Shared MIME-info Database. The database is built from the XML files installed by packages in /usr/share/mime/packages using the tools from the shared-mime-info package.

The files in /usr/share/mime/ should not be directly edited, however it is possible to maintain a separate database on a per-user basis in the ~/.local/share/mime/ tree.

New MIME types

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

Reason: Is the process different for assigning an extension to an existing MIME type? (Discuss in Talk:Default applications#)

This example defines a new MIME type application/x-foobar and assigns it to any file with a name ending in .foo. Simply create the following file:

~/.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>

And then update the MIME database

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

Of course this will not have any affect if no desktop entries are associated with the MIME type. You may need to create new desktop entries or modify mimeapps.list.

Desktop entries

Each package can use desktop entries to provide information about the MIME types that can be handled by the packaged software. In order to provide fast search in the reverse direction, the system uses the tools from the desktop-file-utils package to analyze the desktop files and to create an inverse mapping stored in the /usr/share/applications/mimeinfo.cache file. This is the only file that programs need to read to find all desktop files that might be used to handle given MIME type. Using the database is easier and faster than reading hundreds of .desktop files directly.

The files in /usr/share/applications/ should not be edited directly, it is possible to maintain a separate database on a per-user basis in the ~/.local/share/applications/ tree. See Desktop entries for details.

Set default applications

The configuration of default applications depends on which launcher is used. Unfortunately there are multiple incompatible standards and many programs even have their own custom formats.

The most common standards are explained below for manual editing. You can also use some utility to do the job, which may be using some of these standards internally or its own configuration format.

Environment variables

Console programs in particular are configured by setting an appropriate environment variable, e.g. BROWSER or EDITOR. See Environment variables#Examples.

XDG standard

The XDG standard is the most common for configuring desktop environments. Default applications for each MIME type are stored in mimeapps.list files, which can be stored in several locations. They are searched in the following order, with earlier associations taking precedence over later ones:

Path Usage
~/.config/mimeapps.list user overrides
/etc/xdg/mimeapps.list system-wide overrides
~/.local/share/applications/mimeapps.list (deprecated) user overrides
/usr/local/share/applications/mimeapps.list
/usr/share/applications/mimeapps.list
distribution-provided defaults

Additionally, it is possible to define desktop-environment-specific default applications in a file named desktop-mimeapps.list where desktop is the name of the desktop environment (from the XDG_CURRENT_DESKTOP environment variable). For example, /etc/xdg/xfce-mimeapps.list defines system-wide default application overrides for XFCE. These desktop-specific overrides take precedence over the corresponding non-desktop-specific file. For example, /etc/xdg/xfce-mimeapps.list takes precedence over /etc/xdg/mimeapps.list but is still overridden by ~/.config/mimeapps.list.

Tip: Although deprecated, several applications still read/write to ~/.local/share/applications/mimeapps.list. To simplify maintenance, simply symlink it
$ ln -s ~/.local/share/applications/mimeapps.list ~/.config/mimeapps.list 

the reverse symlink unfortunately does not work because of how xdg-mime writes to this file.

Note: You might also find files in these locations named defaults.list. This file is similar to mimeapps.list except it only lists default applications (not added/removed associations). It is now deprecated and should be manually merged with mimeapps.list.

Format

Consider the following example:

mimeapps.list
[Added Associations]
image/jpeg=bar.desktop;baz.desktop
video/H264=bar.desktop
[Removed Associations]
video/H264=baz.desktop
[Default Applications]
image/jpeg=foo.desktop

Each section assigns one or more desktop entries to MIME types.

  • Added Associations indicates that the applications support opening that MIME type. For example, bar.desktop and baz.desktop can open JPEG images. This might affect the application list you see when right-clicking a file in a file browser.
  • Removed Associations indicates that the applications do not support that MIME type. For example, baz.desktop cannot open H.264 video.
  • Default Applications indicates that the applications should be the default choice for opening that MIME type. For example, JPEG images should be opened with foo.desktop. This implicitly adds an association between the application and the MIME type. If there are multiple applications, they are tried in order.

Each section is optional and can be omitted if unneeded.

mailcap

The mailcap(4) file format is used by mail programs such as mutt and sylpheed to open non-text files. To have those programs use xdg-open, edit ~/.mailcap:

~/.mailcap
*/*; xdg-open "%s"
Warning: If you use run-mailcapAUR, it is possible for xdg-open to delegate to it. This will cause an infinite loop if you configured your .mailcap as described above.

Utilities

While it is possible to configure default applications and MIME types by directly editing mimeapps.list and the #MIME database, there are many tools that can simplify the process. These tools are also important because applications may delegate opening of files to these tools rather than trying to implement the MIME type standard themselves.

If you use a desktop environment you should first check if it provides its own utility. That should be preferred over these alternatives.

xdg-utils

xdg-utils provides the official utilities for managing MIME types and default applications according to the #XDG standard. Most importantly, it provides /usr/bin/xdg-open which many applications use to open a file with its default application. It is desktop-environment-independent in the sense that it attempts to use each environment's native default application tool and provides its own mechanism if no known environment is detected. Examples:

# determine a file's MIME type
$ xdg-mime query filetype photo.jpeg
image/jpeg

# determine the default application for a MIME type
$ xdg-mime query default image/jpeg
gimp.desktop

# change the default application for a MIME type
$ xdg-mime default feh.desktop image/jpeg

# open a file with its default application
$ xdg-open photo.jpeg

# shortcut to open all web MIME types with a single application
$ xdg-settings set default-web-browser firefox.desktop

# shortcut for setting the default application for a URL scheme
$ xdg-settings set default-url-scheme-handler irc xchat.desktop
Tip: If no desktop environment is detected, MIME type detection falls back to using file which—ironically—does not implement the XDG standard. If you want xdg-utils to work properly without a desktop environment, you will need to install #perl-file-mimeinfo or another xdg-open alternative.

xdg-open alternatives

Because of the complexity of the #xdg-utils version of xdg-open, it can be difficult to debug when the wrong default application is being opened. Because of this, there are many alternatives that attempt to improve upon it. Several of these alternatives replace the /usr/bin/xdg-open binary, thus changing the default application behavior of most applications. Others simply provide an alternative method of choosing default applications.

perl-file-mimeinfo

perl-file-mimeinfo provides the tools mimeopen and mimetype. These have a slightly nicer interface than their xdg-utils equivalents:

# determine a file's MIME type
$ mimetype photo.jpeg
photo.jpeg: image/jpeg

# choose the default application for this file
$ mimeopen -d photo.jpeg
Please choose an application

    1) Feh (feh)
    2) GNU Image Manipulation Program (gimp)
    3) Pinta (pinta)

use application #

# open a file with its default application
$ mimeopen -n photo.jpeg

Most importantly, #xdg-utils apps will actually call mimetype instead of file for MIME type detection, if possible. This is important because file does not follow the #XDG standard.

Note: But perl-file-mimeinfo does not entirely follow the #XDG standard. For example it does not read distribution-wide defaults and it saves its config in deprecated locations.

mimeo

mimeoAUR provides the tool mimeo, which unifies the functionality of xdg-open and xdg-mime.

# determine a file's MIME type
$ mimeo -m photo.jpeg
photo.jpeg
  image/jpeg

# choose the default application for this MIME type
$ mimeo --add image/jpeg feh.desktop

# open a file with its default application
$ mimeo photo.jpeg

However a big difference with xdg-utils is that mimeo also supports custom "association files" that allow for more complex associations. For example, passing specific command line arguments based on a regular expression match:

# open youtube links in VLC without opening a new instance
vlc --one-instance --playlist-enqueue %U
  ^https?://(www.)?youtube.com/watch\?.*v=

xdg-utils-mimeoAUR patches xdg-utils so that xdg-open falls back to mimeo if no desktop environment is detected.

whippet

whippetAUR provides the tool whippet, which is similar to xdg-open. It has X11 integration by using libnotify to display errors and dmenu to display choices between applications to open.

# open a file with its default application
$ whippet -M photo.jpeg

# choose from all possible applications for opening a file (without setting a default)
$ whippet -m photo.jpeg

In addition to the standard mimeapps.list, whippet can also use a SQlite database of weighted application/MIME type/regex associations to determine which app to use.

Naive replacements

The following packages replace xdg-utils however they only provide an xdg-open script. These versions of xdg-open do not do any delegation to desktop-environment-specific tools and do not read/write the standard mimeapps.list config file (each has its own custom config), so they may not integrate well with other programs that manipulate default applications. However you may find them simpler to use if you do not use a desktop environment.

Warning: If you need any xdg-* binaries from xdg-utils other than xdg-open you should not use these applications, since they do not provide them.
Package Features
linopenAUR Allows regex rules, can specify fallback file opener
mimi-gitAUR Can change command arguments for each MIME type
busking-gitAUR similar to mimi but also supports regex rules
sx-openAUR uses a simple shell-based config file

lsdesktopf

lsdesktopfAUR provides several methods of searching the MIME database and desktop MIME entries.

For example, to see all MIME extensions in the system's .desktop files that have MIME type video you can use lsdesktopf --gm -gx video or to search in the XML database files use lsdesktopf --gdx -gx video. To get a quick overview of how many and which .desktop files can be associated with a certain MIME type, use lsdesktopf --gen-mimeapps. To see all file name extensions in XML database files, use lsdesktopf --gdx -gfx.

Troubleshooting

If a file is not being opened by your desired default application, there are several possible causes. You may need to check each case.

Missing .desktop file

A desktop entry is required in order to associate an application with a MIME type. Ensure that such an entry exists and can be used to (manually) open files in the application.

Missing association

If the application's desktop entry does not specify the MIME type under its MimeType key, it will not be considered when an application is needed to open that type. Edit mimeapps.list to add an association between the .desktop file and the MIME type.

Non-default application

If the desktop entry is associated with the MIME type, it may simply not be set as the default. Edit mimeapps.list to set the default association.

Nonstandard association

Applications are free to ignore or only partially implement the #XDG standard. Check for usage of deprecated files such as ~/.local/share/applications/mimeapps.list and ~/.local/share/applications/defaults.list. If you are attempting to open the file from another application (e.g. a web browser or file manager) check if that application has its own method of selecting default applications.

Variables in .desktop files that affect application launch

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

Reason: The fact MimeType entries may be missing in the desktop files is only implied here, i.e. "Even if an application...", though this is a common cause of errors. Some openers may also associate mime types not explicitely listed in a desktop file (such as exo). Further environment-specific factors are at play, e.g. whether Terminal=true has an effect, though latter should arguably be expanded on in Desktop entries. (Discuss in Talk:Default applications#)

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