Difference between revisions of "Default applications"

From ArchWiki
Jump to: navigation, search
m (Exactly how it works: accuracy)
m (rm gap)
 
(48 intermediate revisions by 17 users not shown)
Line 1: Line 1:
 
[[Category:Desktop environments]]
 
[[Category:Desktop environments]]
[[zh-cn:Default Applications]]
+
[[ja:デフォルトアプリケーション]]
{{Poor writing|need better introduction (second point is not described)}}
+
[[ru:Changing filetype association via MIME]]
There are numerous places to configure default applications on Linux. This page will attempt to address problems related to the following issues:
+
[[zh-cn:Default applications]]
* You need to change a certain default application (e.g. after switching desktop environments), but there appears to be nowhere to configure it, or an application ignores your configuration
+
{{Related articles start}}
* You regularly switch back and forth between several desktop environments, and need to configure some applications (e.g. file manager) on a per-desktop-environment basis, but need to configure others (e.g. web browser) globally
+
{{Related|Desktop environment}}
 +
{{Related|Window manager}}
 +
{{Related articles end}}
 +
{{Poor writing|[[#File managers|File managers]] and [[#Gnome Control Center|Gnome Control Center]] are only front-ends to the [[#MIME types and desktop entries|Desktop entries method]], similarly to [[#gnome-defaults-list|gnome-defaults-list]] and following. The layout should be more logical.}}
 +
{{Expansion|{{ic|org.freedesktop.FileManager1}} [[D-Bus]] service may mess with the default file manager, e.g. when using [[Firefox]].}}
  
== Changing a default application ==
+
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.
=== xdg-open ===
+
[[xdg-open]] is a desktop-independent tool for configuring default applications.
+
  
==== Exactly how it works ====
+
== MIME types and desktop entries ==
{{Accuracy|[[xdg-open]] and many other applications now use {{ic|~/.local/share/applications/mimeapps.list}} instead of {{ic|~/.local/share/applications/defaults.list}}}}
+
This is currently very shaky because the standard in question ([http://www.freedesktop.org/wiki/Specifications/mime-actions-spec relevant freedesktop.org document], "Default application ordering" section) is still not finalised and it is even less correctly implemented.
+
  
What happens with the current version of xdg-open (extra/xdg-utils 1.1.0rc1-3) is the following: xdg-open uses xdg-mime (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. xdg-mime looks for the default application in the following files:
+
{{Style|Lacks clarity, content tacked on}}
  
/usr/share/applications/defaults.list      (global)
+
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>}}.
~/.local/share/applications/defaults.list  (per user, overrides global)
+
  
The syntax of these files is:
+
[http://www.freedesktop.org/wiki freedesktop.org] recommends how to specify default (preferred) applications for MIME-types in their [http://standards.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html Association between MIME types and applications] standard. This involves writing into files called {{ic|mimeapps.list}} which are looked up in the following order of paths:
  
[Default Applications]
+
{| class="wikitable"
mimetype=desktopfile1;desktopfile2;...;desktopfileN
+
! 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}} and <br>{{ic|/usr/share/applications/$desktop-mimeapps.list}} || distribution-provided defaults, desktop-specific
 +
|-
 +
| {{ic|/usr/local/share/applications/mimeapps.list}} and <br>{{ic|/usr/share/applications/mimeapps.list}} || distribution-provided defaults
 +
|}
 +
 
 +
In this table, {{ic|$VARS}} are environment variables and {{ic|$desktop}} is the name of the current desktop (in lowercase), for example {{ic|kde}}, {{ic|gnome}}, {{ic|xfce}}, etc.
  
E.g.
+
The default application for a given MIME-type is specified by writing into the group {{ic|[Default Applications]}} in the {{ic|mimeapps.list}} file. In the following example, application {{ic|default1.desktop}} (if it is installed) will be used for {{ic|mimetype1}}, and otherwise {{ic|default2.desktop}} (if it is installed and {{ic|default1.desktop}} is not):
  
 
  [Default Applications]
 
  [Default Applications]
  text/html=firefox.desktop;chromium.desktop
+
  mimetype1=default1.desktop;default2.desktop
  
The intent is to be able to specify applications in order of preference so the next would be selected if the previous is unavailable. However, xdg-open is currently '''broken''' and specifying more than one application for a single MIME type will result in none being used. Be sure to specify only '''one''' application per MIME type.
+
The applications are written as a semicolon-separated list of [http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id desktop file IDs].
  
Also see [[xdg-open]] for more information on what affects its behaviour. This information is sufficient for a user to configure default applications on Arch currently.
+
In the absence of such an entry, the next {{ic|mimeapps.list}} in the path hierarchy will be checked. Once all levels have been checked, if no entry could be found, then programs can pick any of the ''.desktop'' files associated with the MIME-type, taking into account added and removed associations as per these other groups which may be specified in {{ic|mimeapps.list}} files:
  
==== How it should work ====
+
[Added Associations]
However, the behaviour described in the previous section is actually wrong according to the quoted standard draft. defaults.list should only be used by distributions/DEs to specify sane defaults for that particular environment. This file should only be placed in {{Ic|/usr/share/applications/}}, it should be a system-wide setting and it should not be changed by users. Instead, users should use the files {{ic|/usr/share/applications/mimeapps.list}} (global) and {{ic|~/.local/share/applications/mimeapps.list}} (per user, overrides global) to specify custom file associations. The syntax of these files is as follows:
+
mimetype1=foo1.desktop;foo2.desktop;foo3.desktop
{{hc|mimeapps.list|
+
mimetype2=foo4.desktop
[Added Associations]
+
[Removed Associations]
mimetype&#61;desktopfile1;desktopfile2;...;desktopfileN
+
mimetype1=foo5.desktop
...
+
 
[Removed Associations]
+
The {{ic|[Added Associations]}} group defines additional associations of applications with MIME-types, as if the ''.desktop'' file was listing this MIME-type in the first place. The {{ic|[Removed Associations]}} group removes associations of applications with MIME-types, as if the ''.desktop'' file was '''not''' listing this MIME-type in the first place. The entries in {{ic|[Default Applications]}} should also be considered to add an association between application and MIME-type in the same manner.
mimetype&#61;desktopfile1;desktopfile2;...;desktopfileN
+
 
 +
{{Note|
 +
* Arch Linux itself does not provide any system-wide preferences for associations, but other distributions and specific desktop environments may do so via {{ic|mimeapps.list}} or the older but deprecated {{ic|defaults.list}} files.
 +
* As the standards for setting default applications have been recently changed, not all programs will comply with them yet. Indeed, the programs provided by freedesktop.org's {{Pkg|xdg-utils}} package do not fully follow their own standards!
 +
* Your choice of desktop environment, or none, will also affect how your default applications are stored.
 +
* When the program {{ic|update-desktop-database}} is run (usually as root during the (un)installation of a package), it updates files called {{ic|mimeinfo.cache}} in the {{ic|/usr/local/share/applications}} and {{ic|/usr/share/applications}} directories. These files keep track of which MIME-types are associated with which ''.desktop'' files overall. This file should not be edited by the user.
 
}}
 
}}
  
The [Added Associations] section is used to specify preferred (default) applications in decreasing order of preference for the specified MIME type. The [Removed Associations] section is used to explicitly remove any previously inherited associations.
+
=== File managers ===
  
Note that the upstream, yet unreleased xdg-utils were fixed to use mimeapps.list instead of defaults.list but the xdg-open bug (specifying multiple applications breaks it completely) still remains.
+
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]]:
  
Currently, the best option is to use {{AUR|mimeo}}, which implements everything explained above correctly.  There is also {{AUR|xdg-utils-mimeo}} which provides a patched xdg-open to use mimeo. Both are available in [[Arch User Repository|AUR]].
+
* {{ic|right-click}} the file-type desired
 +
* 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.
  
=== GNOME 3 ===
+
The general process will be very similar for most other popular file managers, including [[PCManFM]] and {{pkg|spacefm}}.
The xdg-open settings are usually recognized by GNOME, but specific configuration is needed for some tasks. See [[GNOME#Miscellaneous settings]] for more details.
+
  
== Custom File Associations ==
+
=== Gnome Control Center ===
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 '*.foo' files.  This will be done a per user basis (as opposed to system-wide).
+
  
* First, create the file {{ic|~/.local/share/mime/packages/application-x-foobar.xml}}:
+
If it is installed, run {{ic|gnome-control-center}}, open {{ic|System}} &gt; {{ic|Details}} &gt; {{ic|Default Applications}}.
 +
 
 +
=== gnome-defaults-list ===
 +
 
 +
{{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 ===
 +
 
 +
[[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.
 +
 
 +
=== Mimeo ===
 +
 
 +
[http://xyne.archlinux.ca/projects/mimeo Mimeo] is a command-line file association manager and launcher that can be used by applications such as web browsers and mail clients to open files. It adheres to the [https://www.freedesktop.org/wiki/Specifications/ freedesktop.org specifications] while providing optional support for the deprecated {{ic|~/.local/share/applications/mimeapps.list}} which is still commonly used. It also supports a custom and versatile regular-expression-based association file that can be used to associate arbitrary strings with applications (e.g. open youtube URLs with VLC or open protein database files with PyMol). Mimeo is available from the AUR ({{AUR|mimeo}}) as well as [http://xyne.archlinux.ca/repos/ Xyne's repos].
 +
 
 +
=== Custom file associations ===
 +
 
 +
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}}:
  
 
  $ mkdir -p ~/.local/share/mime/packages
 
  $ mkdir -p ~/.local/share/mime/packages
Line 61: Line 108:
 
  $ touch application-x-foobar.xml
 
  $ touch application-x-foobar.xml
  
* Then edit {{ic|~/.local/share/mime/packages/application-x-foobar.xml}} and add this text:
+
Then edit {{ic|~/.local/share/mime/packages/application-x-foobar.xml}} and add this text:
<pre>
+
 
 +
{{bc|<nowiki>
 
<?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">
 
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<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>
</pre>
+
</nowiki>}}
 +
 
 
Note that you can use any icon, including one for another application.
 
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:
+
Next, edit or create the file {{ic|~/.local/share/applications/foobar.desktop}} to contain something like:
  
[Desktop Entry]
+
{{bc|<nowiki>
Name=Foobar
+
[Desktop Entry]
Exec=/usr/bin/foobar
+
Name=Foobar
MimeType=application/x-foobar
+
Exec=/usr/bin/foobar
Icon=foobar
+
MimeType=application/x-foobar
Terminal=false
+
Icon=foobar
Type=Application
+
Terminal=false
Categories=AudioVideo;Player;Video;
+
Type=Application
Comment=
+
Categories=AudioVideo;Player;Video;
 +
Comment=
 +
</nowiki>}}
  
 
Note that Categories should be set appropriately for the application type (in this example, a multimedia app).
 
Note that Categories should be set appropriately for the application type (in this example, a multimedia app).
  
* Now update the mime database with:
+
Now update the applications and mime database with:
  
  $ update-mime-database ~/.local/share/mime
+
$ 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.)
+
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.)
  
== Maintaining settings for multiple desktop environments ==
+
=== Maintaining settings for multiple desktop environments ===
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}}.
+
 
 +
{{Expansion|This should be either expanded on (particularly referencing {{ic|NoDisplay}}, or merged to [[Desktop entries]]}}
 +
 
 +
The {{ic|OnlyShowIn}} field of a .desktop file may be useful; see [http://standards.freedesktop.org/menu-spec/latest/ this page].
 +
 
 +
== Using environment variables ==
 +
 
 +
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>
 +
export EDITOR="nano"
 +
export BROWSER="firefox"
 +
</nowiki>}}
  
 
== Troubleshooting ==
 
== Troubleshooting ==
=== Applications don't appear in the ''Open With...'' context menu (of a file manager) ===
+
 
 +
=== Applications do not 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.
 
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|MimeTypes}} list in the {{Ic|.desktop}} file if you install extensions that allow an application to handle additional MIME types.
+
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.

Latest revision as of 03:00, 3 March 2016

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements.Tango-edit-clear.png

Reason: File managers and Gnome Control Center are only front-ends to the Desktop entries method, similarly to gnome-defaults-list and following. The layout should be more logical. (Discuss in Talk:Default applications#)

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

Reason: org.freedesktop.FileManager1 D-Bus service may mess with the default file manager, e.g. when using Firefox. (Discuss in Talk:Default applications#)

Default applications can be set for use with particular file types (e.g. the Firefox web browser for 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.

MIME types and desktop entries

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements.Tango-edit-clear.png

Reason: Lacks clarity, content tacked on (Discuss in Talk:Default applications#)

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, gimp.desktop states MimeType=image/bmp;image/gif;....

freedesktop.org recommends how to specify default (preferred) applications for MIME-types in their Association between MIME types and applications standard. This involves writing into files called mimeapps.list which are looked up in the following order of paths:

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 and
/usr/share/applications/$desktop-mimeapps.list
distribution-provided defaults, desktop-specific
/usr/local/share/applications/mimeapps.list and
/usr/share/applications/mimeapps.list
distribution-provided defaults

In this table, $VARS are environment variables and $desktop is the name of the current desktop (in lowercase), for example kde, gnome, xfce, etc.

The default application for a given MIME-type is specified by writing into the group [Default Applications] in the mimeapps.list file. In the following example, application default1.desktop (if it is installed) will be used for mimetype1, and otherwise default2.desktop (if it is installed and default1.desktop is not):

[Default Applications]
mimetype1=default1.desktop;default2.desktop

The applications are written as a semicolon-separated list of desktop file IDs.

In the absence of such an entry, the next mimeapps.list in the path hierarchy will be checked. Once all levels have been checked, if no entry could be found, then programs can pick any of the .desktop files associated with the MIME-type, taking into account added and removed associations as per these other groups which may be specified in mimeapps.list files:

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

The [Added Associations] group defines additional associations of applications with MIME-types, as if the .desktop file was listing this MIME-type in the first place. The [Removed Associations] group removes associations of applications with MIME-types, as if the .desktop file was not listing this MIME-type in the first place. The entries in [Default Applications] should also be considered to add an association between application and MIME-type in the same manner.

Note:
  • Arch Linux itself does not provide any system-wide preferences for associations, but other distributions and specific desktop environments may do so via mimeapps.list or the older but deprecated defaults.list files.
  • As the standards for setting default applications have been recently changed, not all programs will comply with them yet. Indeed, the programs provided by freedesktop.org's xdg-utils package do not fully follow their own standards!
  • Your choice of desktop environment, or none, will also affect how your default applications are stored.
  • When the program update-desktop-database is run (usually as root during the (un)installation of a package), it updates files called mimeinfo.cache in the /usr/local/share/applications and /usr/share/applications directories. These files keep track of which MIME-types are associated with which .desktop files overall. This file should not be edited by the user.

File managers

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 thunar, the native file manager for Xfce:

  • right-click the file-type desired
  • Select Open with another application
  • Select the desired application
  • Ensure that the Use as default for this kind of file check-box is ticked
  • Click the Open button.

The general process will be very similar for most other popular file managers, including PCManFM and spacefm.

Gnome Control Center

If it is installed, run gnome-control-center, open System > Details > Default Applications.

gnome-defaults-list

gnome-defaults-listAUR is available from the AUR, and contains a list of file-types and programs specific to the GNOME desktop. The list is installed to /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 totem can be replaced with another, such as vlc. Save the file to ~/.local/share/applications/defaults.list.

xdg-open

xdg-open is a desktop-independent tool for starting default applications. Many applications invoke the xdg-open command internally. xdg-open uses xdg-mime to query ~/.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.

Mimeo

Mimeo is a command-line file association manager and launcher that can be used by applications such as web browsers and mail clients to open files. It adheres to the freedesktop.org specifications while providing optional support for the deprecated ~/.local/share/applications/mimeapps.list which is still commonly used. It also supports a custom and versatile regular-expression-based association file that can be used to associate arbitrary strings with applications (e.g. open youtube URLs with VLC or open protein database files with PyMol). Mimeo is available from the AUR (mimeoAUR) as well as Xyne's repos.

Custom file associations

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 *.foo files. This will only affect the current user.

First, create the file ~/.local/share/mime/packages/application-x-foobar.xml:

$ mkdir -p ~/.local/share/mime/packages
$ cd ~/.local/share/mime/packages
$ touch application-x-foobar.xml

Then edit ~/.local/share/mime/packages/application-x-foobar.xml and add this text:

<?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>

Note that you can use any icon, including one for another application.

Next, edit or create the file ~/.local/share/applications/foobar.desktop to contain something like:

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

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:

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

Maintaining settings for multiple desktop environments

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

Reason: This should be either expanded on (particularly referencing NoDisplay, or merged to Desktop entries (Discuss in Talk:Default applications#)

The OnlyShowIn field of a .desktop file may be useful; see this page.

Using environment variables

Most non-graphical programs use Environment variables, such as EDITOR or BROWSER. These can be set in your terminal's autostart file (e.g. ~/.bashrc):

~/.bashrc
export EDITOR="nano"
export BROWSER="firefox"

Troubleshooting

Applications do not 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 .desktop file in /usr/share/applications, edit it as root, and add %U to the end of the Exec= line. For example, Kile currently has this problem; you need to edit /usr/share/applications/kde4/kile.desktop and change the line reading Exec=kile to read Exec=kile %U. Also, please file a bug against the upstream project if you notice this problem.

You may also have to edit the MimeType list in the .desktop file if you install extensions that allow an application to handle additional MIME types.