Difference between revisions of "Default applications"

From ArchWiki
Jump to: navigation, search
m (Corrected MimeTypes to MimeType)
(move some content around with xdg-open)
Line 2: Line 2:
 
[[zh-cn:Default Applications]]
 
[[zh-cn:Default Applications]]
 
{{Poor writing|need better introduction (second point is not described)}}
 
{{Poor writing|need better introduction (second point is not described)}}
There are numerous places to configure default applications on Linux. This page will attempt to address problems related to the following issues:
+
There are numerous places to configure default applications on Linux. This page will explain the most used ones: using MIME types and using environment variables.
* 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
+
* 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
+
  
== Changing a default application ==
+
== Using MIME types and desktop entries ==
=== xdg-open ===
+
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>}}.
[[xdg-open]] is a desktop-independent tool for configuring default applications.
+
 
+
==== Exactly how it works ====
+
{{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:
+
  
/usr/share/applications/defaults.list     (global)
+
A list of MIME type and corresponding default application is stored in {{ic|$XDG_DATA_HOME/applications/mimeapps.list}} (for a single user; {{ic|$XDG_DATA_HOME}} defaults to {{ic|~/.local/share}}) and {{ic|/usr/share/applications/defaults.list}} (system wide). This list can be edited by using [[xdg-open#xdg-mime|xdg-mime]].
~/.local/share/applications/defaults.list (per user, overrides global)
+
  
The syntax of these files is:
+
This file looks like this:
  
 
  [Default Applications]
 
  [Default Applications]
Line 29: Line 19:
 
  text/html=firefox.desktop;chromium.desktop
 
  text/html=firefox.desktop;chromium.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.
+
A distribution could also ship a {{ic|/usr/share/applications/mimeapps.list}} to provide system-wide defaults, but Arch Linux does not do this. To override these system-wide defaults, one can add [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.
  
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.
+
=== 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.  
==== How it should work ====
+
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:
+
{{hc|mimeapps.list|
+
[Added Associations]
+
mimetype&#61;desktopfile1;desktopfile2;...;desktopfileN
+
...
+
[Removed Associations]
+
mimetype&#61;desktopfile1;desktopfile2;...;desktopfileN
+
}}
+
 
+
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.
+
 
+
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.
+
 
+
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]].
+
  
=== GNOME 3 ===
+
See [[xdg-open]] for more information.  
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 ==
+
=== 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 be done a per user basis (as opposed to system-wide).
+
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}}:
 
* First, create the file {{ic|~/.local/share/mime/packages/application-x-foobar.xml}}:
Line 62: Line 36:
  
 
* 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">
Line 72: Line 46:
 
</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:
 
+
{{bc|<nowiki>
[Desktop Entry]
+
[Desktop Entry]
Name=Foobar
+
Name=Foobar
Exec=/usr/bin/foobar
+
Exec=/usr/bin/foobar
MimeType=application/x-foobar
+
MimeType=application/x-foobar
Icon=foobar
+
Icon=foobar
Terminal=false
+
Terminal=false
Type=Application
+
Type=Application
Categories=AudioVideo;Player;Video;
+
Categories=AudioVideo;Player;Video;
Comment=
+
Comment=
 
+
</nowiki>}}
 
Note that Categories should be set appropriately for the application type (in this example, a multimedia app).
 
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 mime database with:
 
 
  $ update-mime-database ~/.local/share/mime
 
  $ 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}}.
 
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 ==
 +
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 ==

Revision as of 11:12, 2 November 2013

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

Reason: need better introduction (second point is not described) (Discuss in Talk:Default applications#)

There are numerous places to configure default applications on Linux. This page will explain the most used ones: using MIME types and using environment variables.

Using MIME types and desktop entries

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

A list of MIME type and corresponding default application is stored in $XDG_DATA_HOME/applications/mimeapps.list (for a single user; $XDG_DATA_HOME defaults to ~/.local/share) and /usr/share/applications/defaults.list (system wide). This list can be edited by using xdg-mime.

This file looks like this:

[Default Applications]
mimetype=desktopfile1;desktopfile2;...;desktopfileN

E.g.

[Default Applications]
text/html=firefox.desktop;chromium.desktop

A distribution could also ship a /usr/share/applications/mimeapps.list to provide system-wide defaults, but Arch Linux does not do this. To override these system-wide defaults, one can add [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.

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.

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 mime database with:
$ 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

The OnlyShowIn field of a .desktop file may be useful; see this page. I haven't tried setting this field yet; please update this wiki page if you have any info about using OnlyShowIn.

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