# Default applications (简体中文)

翻译状态： 本文是英文页面 Default_Applications 的翻译，最后翻译时间：2016-09-01，点击这里可以查看翻译后英文页面的改动。

本页面需要更新翻译，内容可能已经与英文脱节。要贡献翻译，请访问简体中文翻译组。

附注: please use the first argument of the template to provide more detailed indications.

相关文章

- Desktop entries
- Desktop environment
- Window manager

Setting default applications per file type involves detection of the file type as the first step. Since the application launcher cannot fully understand all file types, the detection is based on reading only a small part of the file. There are two common ways to determine the file type:

- using the file name extension, for example .html or .jpeg
- using the so-called "magic bytes" at the start of the file

The first method is very simple and fast, but inaccurate if the file is not named "correctly". The second is more accurate, but slower.

Since files are not required to have an extension and multiple file name extensions can be used for the same file type, MIME types are commonly used instead to uniquely represent the type of a file. In Arch, tools from the shared-mime-info package are used to maintain the MIME type database, which is used by other packages to register new MIME types. Each package can also use the Desktop entries to provide information about the MIME types that can be handled by the packaged software. There is frequently more than one application able to handle data of a certain MIME type, so users and even some packages (e.g. desktop environments) assemble lists of default applications for each MIME type.

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 are the freedesktop Association between MIME types and applications specification and related parts of the Shared MIME-info Database specification.

## MIME types and desktop entries

This article or section needs expansion.

Reason: Complete list available at: [1]. Not sure if it's needed to maintain a summarized version in this article. (Discuss in Talk:Default applications (简体中文)#)

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`, to search available in .xml configuration/database files use `lsdesktopf --gdx -gx video`.

If you ever require to create a custom association of a new file extension to a MIME-type, see the the short #Example: .xml and related .desktop configuration and the Association between MIME types and applications standard.

## Set default applications

In order to set a default application, you need to

- decide which of the #Default mimeapps.list files is applicable for your case and,
- change the #Configuration of the mimeapps.list file accordingly.

Any manual configuration of the #Shared MIME-info database is only required, if the application is not setup correctly or desktop does not comply to the standard yet.

### Default mimeapps.list files

This article or section needs expansion.

Reason: Moving deprecated configuration files to a Note, rather than include them in the table, would improve overview. More context on the changing of the file location could also be useful. (Discuss in Talk:Default applications (简体中文)#)

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

### Configuration 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.

If the application installed a correct desktop entry file (examples used below `default1.desktop`, `foo1.desktop`, etc.), it contains the registered MIME-types it can handle. To set a default application, all you need to do is adjust the associations in the respective `mimeapps.list` (see also the freedesktop specification).

For example, the following sets `default1.desktop` to open `mimetype1`:

    [Default Applications]
    mimetype1=default1.desktop

Defined additional associations of applications to MIME-types (these might appear in file manager Open with GUI, for example):

    [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:

- `/usr/share/libalpm/hooks/update-mime-database.hook` from shared-mime-info

  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

- `/usr/share/libalpm/hooks/update-desktop-database.hook` from desktop-file-utils

  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

The following is a short example to create files to associate an application with a MIME-type. It only needs to be followed, if the application has not setup and registered its configuration sufficiently.

User-specific paths are used to override system defaults, while keeping the integrity of the rest 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>

Tip: To see all file name extensions in .xml configuration or database files, that available in glob pattern, with their description in comment use lsdesktopfAUR: `lsdesktopf --gdx -gfx`.

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.)

This article or section needs language, wiki syntax or style improvements.

Reason: The relation between environment variables and the above discussion on MIME types is not immediate. Setting `BROWSER` e.a, while also related to default applications, is a different approach entirely. (Discuss in Talk:Default applications (简体中文)#)

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:

The factual accuracy of this article or section is disputed.

Reason: Packages such as sx-openAUR may replace xdg-utils in the sense that they have a provides/replaces field to pacman, but they miss tools used by other applications such as `xdg-screensaver` and `xdg-icon-resource`. This is not clear from the table. The table should also be better adapted to the section below, or the "based on" section could simply be left out. (Discuss in Talk:Default applications (简体中文)#)

Name/Package | Method | Based on | xdg-utils replacement | Configuration file
--- | --- | --- | --- | ---
mime-editor | MIME-type | Utility for ROX applications |  |
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 | file |  | custom
sx-openAUR | regex | file bash regex | yes | custom
whippetAUR | MIME-type name regex | SQLite database file perl-file-mimeinfo, etc | xdg-open | 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

This article or section needs language, wiki syntax or style improvements.

Reason: Unclear what is meant by "it is two standards". Going by the `Magic -> XML` entries, the first seems to be a "compiled database", without indication how it is created. The second the XML files as described in [2]. Latter can also define file contents, and the "extension associated with one program but content is associated with another MIME-type" is vague at best. (Discuss in Talk:Default applications (简体中文)#)

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[broken link: invalid section] configuration files.

The factual accuracy of this article or section is disputed.

Reason: The following sentence is true only when perl-file-mimeinfo is installed. Otherwise, file is used and hence the detection method is different. (Discuss in Talk:Default applications (简体中文)#)

Here is examples of utility xdg-mime that first checking association of extension in *.xml[broken link: invalid section] 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

This article or section needs language, wiki syntax or style improvements.

Reason: Why start over with sections when we already have a table? Pick one. (Discuss in Talk:Default applications (简体中文)#)

#### xdg-open

xdg-open (from the xdg-utils package) 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

mimeopen (from the perl-file-mimeinfo package) can launch 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.

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.

#### mailcap

The mailcap file.

This article or section needs expansion.

Reason: When using run-mailcapAUR, xdg-open may refer to it.[3] 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

This article or section needs language, wiki syntax or style improvements.

Reason: Redundant and oddly named header; it is also odd that alternatives are discussed before the regular xdg-utils. (Discuss in Talk:Default applications (简体中文)#)

#### xdg-open

The factual accuracy of this article or section is disputed.

Reason: Both xdg-mime and xdg-settings ignore the `~/.config/mimeapps.list` file for writing, instead they use the deprecated `~/.local/share/applications/mimeapps.list`. If the former exists, the latter is ignored, so the changes have no effect.[4] (Discuss in Talk:Default applications (简体中文)#)

xdg-mime modifies the local file `~/.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

This article or section needs expansion.

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

Category:

- Desktop environments (简体中文)

Hidden categories:

- Pages or sections flagged with Template:Expansion
- Pages or sections flagged with Template:Style
- Pages or sections flagged with Template:Accuracy
- Pages with broken section links