Difference between revisions of "Xdg-open"

From ArchWiki
Jump to: navigation, search
(xdg-open replacements: added 'Configuration file' column)
(Undo revision 416921 by Cirrus (talk) - How is something like this "full"? Just look into /etc/mime.types, no need for separate lists that get outdated.)
 
(101 intermediate revisions by 28 users not shown)
Line 1: Line 1:
 +
{{lowercase title}}
 
[[Category:Desktop environments]]
 
[[Category:Desktop environments]]
xdg-open is a desktop-independent tool for configuring the default applications of a user. Many applications invoke the {{ic|xdg-open}} command internally.
+
[[ja:Xdg-open]]
 +
[[ru:Xdg-open]]
 +
{{Related articles start}}
 +
{{Related|Default applications}}
 +
{{Related|Environment variables}}
 +
{{Related|Desktop entries}}
 +
{{Related articles end}}
 +
{{Merge|Default applications|[[Talk:Xdg-open#About Merge]]}}
 +
'''xdg-open''' is a desktop-independent tool for configuring the [[default applications]] of a user. Many applications invoke the {{ic|xdg-open}} command internally.
  
Inside a [[desktop environment]] (e.g. [[GNOME]], [[KDE]], [[Xfce]], etc.), {{ic|xdg-open}} simply passes the arguments to that desktop environment's file-opener application ({{ic|gvfs-open}}, {{ic|kde-open}}, or {{ic|exo-open}}, respectively), which means that the associations are left up to the desktop environment. When no desktop environment is detected (for example when one runs a standalone [[window manager]], e.g. [[Openbox]]), {{ic|xdg-open}} will use its own configuration files.
+
Inside a [[desktop environment]] (like [[GNOME]], [[KDE]], or [[Xfce]]), ''xdg-open'' simply passes the arguments to those desktop environment's file-opener application (eg. ''gvfs-open'', ''kde-open'', or ''exo-open''). which means that the associations are left up to the desktop environment.  
 +
 
 +
When no desktop environment is detected (for example when one runs a standalone [[window manager]] like eg. [[Openbox]]), ''xdg-open'' will use its own configuration files.
  
 
== Installation ==
 
== Installation ==
xdg-open is part of the {{Pkg|xdg-utils}} package available in the [[official repositories]]. xdg-open is for use inside a desktop session only. It is not recommended to use xdg-open as root.
+
 
 +
''xdg-open'' is part of the {{Pkg|xdg-utils}} package available in the [[official repositories]]. It is for use inside a desktop session only, and should not be run as root.
 +
 
 +
If you run ''xdg-open'' without a desktop environment, you should also install {{Pkg|perl-file-mimeinfo}}, or {{AUR|xdg-utils-mimeo}} and {{AUR|mimeo}} from the [[AUR]] for a faster alternative.
  
 
== Configuration ==
 
== Configuration ==
=== The config file ===
 
{{Merge|Default Applications|Default Applications article describes (or at least should describe) the standard in general, as well as manual method of configuration}}
 
{{ic|xdg-open}} uses configuration files located at {{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). Its format is as follows:
 
[Default Applications]
 
mimetype=desktopfile1;desktopfile2...
 
For example:
 
[Default Applications]
 
text/html=firefox.desktop
 
inode/directory=kde4-dolphin.desktop;Thunar.desktop;nautilus.desktop
 
  
The best way to look up the MIME type is to read the {{ic|MimeTypes}} list in your application's {{ic|.desktop}} file (see [[Desktop Entries]] for details).
+
''xdg-open'' is configured by the files mentioned in [[Default applications#MIME types and desktop entries|Default applications]]. ''xdg-mime'' modifies the local file {{ic|~/.config/mimeapps.list}} and {{ic|~/.local/share/applications/mimeapps.list}} (deprecated).
  
Some tools are available to edit the configuration files:
+
To '''query''' the mime type used by an existing file, use
 +
$ xdg-mime query '''filetype''' ''file.ext''
  
=== xdg-mime ===
+
Reversely, to query the default [[desktop entry]] associated with a specific mime type, run
* To get mime type of {{ic|photo.jpg}}:
+
$ xdg-mime query '''default''' ''mime/type''
{{hc|$ xdg-mime query filetype photo.jpg|
+
photo.jpg
+
    image/jpeg
+
}}
+
  
* To get default {{ic|.desktop}} file starter for {{ic|image/jpeg}} mime type:
+
Examples for ''mime/type'' can be:
{{hc|$ xdg-mime query default image/jpeg|
+
inode/directory (file browser)
gpicview.desktop;
+
image/jpeg (JPEG images)
}}
+
application/pdf (PDF viewer)
 +
or others
  
* To make Thunar the default file-browser, i.e. the default application for opening folders:
+
To change an associated desktop entry, use:
 +
$ xdg-mime '''default''' ''application.desktop'' ''mime/type''
 +
 
 +
For example set [[Thunar]] as the default file browser just run:
 
  $ xdg-mime default Thunar.desktop inode/directory
 
  $ xdg-mime default Thunar.desktop inode/directory
  
* To use {{Pkg|xpdf}} as the default PDF viewer:
+
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 xpdf.desktop application/pdf
+
  $ xdg-mime default emacs.desktop $(grep '^text/x-*' /usr/share/mime/types)
  
Previous command should create an entry in your local MIME database:
+
=== Set the default browser ===
{{hc|~/.local/share/applications/defaults.list|<nowiki>
+
[Default Applications]
+
application/pdf=xpdf.desktop
+
</nowiki>}}
+
  
==== Setting default browser for xdg-open ====
+
{{Accuracy|There are many more MIME types for browsers}}
This can be tricky to find, so to save you some work:
+
  
  $ xdg-mime default firefox.desktop x-scheme-handler/http
+
To set the default application for {{ic|http(s)://}} web URLs, write
  $ xdg-mime default firefox.desktop x-scheme-handler/https
+
 
 +
$ xdg-mime default ''browser''.desktop x-scheme-handler/http
 +
$ xdg-mime default ''browser''.desktop x-scheme-handler/https
 +
 
 +
To do so for {{ic|.html}} files:
 +
 
 +
$ xdg-mime default ''browser''.desktop text/html
 +
 
 +
Alternatively, run:
 +
 
 +
$ xdg-settings set default-web-browser ''browser''.desktop
 +
 
 +
To test if this was applied successfully, try to open an URL with ''xdg-open'' as follows:
 +
 
 +
$ xdg-open https://archlinux.org
 +
 
 +
=== perl-file-mimeinfo ===
 +
 
 +
''xdg-open'' uses {{Pkg|perl-file-mimeinfo}} as a fallback ("generic") method if no [[desktop environment]] is detected. It can be invoked directly with:
 +
 
 +
$ mimeopen -d /path/to/file
 +
 
 +
You are asked which application to use when opening {{ic|/path/to/file}}:
 +
 
 +
Please choose a default application for files of type text/plain
 +
        1) notepad  (wine-extension-txt)
 +
        2) Leafpad  (leafpad)
 +
        3) OpenOffice.org Writer  (writer)
 +
        4) gVim  (gvim)
 +
        5) Other...
 +
 
 +
Your answer becomes the default handler for that type of file. Mimeopen is installed as {{ic|/usr/bin/vendor_perl/mimeopen}}.
  
 
== Drop-in replacements and useful tools ==
 
== Drop-in replacements and useful tools ==
  
 
=== xdg-open replacements ===
 
=== xdg-open replacements ===
 +
 +
{{Style|There may be some benefit to making this a table, but [[Template:App]] including upstream URLs gives all needed information in a simple manner}}
  
 
{| class="wikitable"
 
{| class="wikitable"
! Name/Package !! Description !! Based on !! Configuration file
+
! Name/Package !! Method !! Based on !! Configuration file
 
|-
 
|-
| {{AUR|busking-git}} || A simple, regex-based xdg-open replacement. || {{pkg|perl-file-mimeinfo}} || custom
+
| {{AUR|busking-git}}{{Broken package link|{{aur-mirror|busking-git}}}} || Regular expressions || {{Pkg|perl-file-mimeinfo}} || custom
 
|-
 
|-
| {{AUR|expro}} || Opens objects in associated applications by matching regular expressions against object name or MIME-type. || {{pkg|file}} (but alternatives can be configured) || custom
+
| {{AUR|linopen}} || || {{Pkg|file}} || custom
 
|-
 
|-
| {{AUR|linopen}} || An intelligent and suckless replacement for xdg-open. || {{pkg|file}} || custom
+
| {{AUR|mimeo}} || MIME-type, regular expressions || {{Pkg|file}} || {{ic|mimeapps.list}}, {{ic|defaults.list}}; custom is optional
 
|-
 
|-
| {{AUR|mimeo}} || Open files by MIME-type or file name using regular expressions. Also requires {{AUR|xdg-utils-mimeo}}. || {{pkg|file}} || standard {{ic|mimeapps.list}} or {{ic|defaults.list}}; custom is optional
+
| {{AUR|mimi-git}} || || {{Pkg|file}} || custom
 
|-
 
|-
| {{AUR|mimi-git}} || A working replacement for xdg-open. || {{pkg|file}} || custom
+
| {{AUR|whippet}} || MIME-type, name, regular expressions || SQLite database or {{Pkg|file}}, {{Pkg|perl-file-mimeinfo}}, etc|| custom SQLite database or {{ic|mimeapps.list}}
 +
|-
 +
| {{AUR|ayr}} || MIME-type, name, regular expressions || {{Pkg|file}} or {{Pkg|perl-file-mimeinfo}}, etc || {{ic|mimeapps.list}}, {{ic|defaults.list}}
 +
|-
 +
| {{AUR|sx-open}}{{Broken package link|{{aur-mirror|sx-open}}}} || Regular expressions || {{Pkg|file}}, bash regex || custom
 +
|-
 +
| {{Pkg|ranger}} (rifle command) || MIME-type, name, regular expressions ||  || custom
 
|}
 
|}
  
===run-mailcap===
+
{{Note|Some of the above packages replace {{ic|xdg-utils}}. Those that do not can be symbolically linked to ''xdg-open'' in the user's {{ic|$PATH}} above {{ic|/usr/bin}}, but some applications hard-code the absolute path {{ic|/usr/bin/xdg-open}}. In this case, install {{AUR|xdg-utils-no-open}} from the [[AUR]] and copy the replacement to {{ic|/usr/bin/xdg-open}}.}}
The {{ic|.mailcap}} file format used by the mutt mail program is way easier to read and write. The {{AUR|run-mailcap}} package from the AUR provides a executable that could be easily symlinked to {{ic|/usr/bin/xdg-open}}, but that parser is unable to handle directories or links. The simplest solution would be to install mimeo from the AUR too and create a {{ic|/usr/bin/xdg-open}} with the following content:
+
  
#!/bin/sh
+
=== mailcap ===
run-mailcap $1 || mimeo $1
+
  
Then use the {{ic|defaults.list}} at {{ic|~/.local/share/applications/}} to associate URLs and folders with applications and the {{ic|.mailcap}} file for normal mimetypes.
+
{{Expansion|When using {{AUR|run-mailcap}}, ''xdg-open'' may refer to it.[http://cgit.freedesktop.org/xdg/xdg-utils/tree/scripts/xdg-open.in#n266] It should then clearly not be combined with the below file to prevent endless loops.}}
 +
 
 +
The ''.mailcap'' file format is used by mail programs such as {{Pkg|mutt}} and {{Pkg|sylpheed}}. To have those programs use ''xdg-open'', edit {{ic|~/.mailcap}}:
 +
 
 +
{{hc|~/.mailcap|
 +
*/*; xdg-open "%s"
 +
}}
  
 
=== mimetype  ===
 
=== mimetype  ===
mimetype in {{Pkg|perl-file-mimeinfo}} package can display some mimetype-related information about a file.
+
 
 +
''mimetype'' in {{Pkg|perl-file-mimeinfo}} package can display some mimetype-related information about a file.
  
 
For example:
 
For example:
Line 90: Line 135:
  
 
returns a description of that mimetype.
 
returns a description of that mimetype.
 +
 +
When ''xdg-open'' fails to detect one of the [[desktop environments]] it knows about, it normally falls back to using {{ic|file -i}}, which uses only file contents to determine the mimetype, resulting in some file types not being detected correctly. With ''mimetype'' available, ''xdg-open'' will use that instead, with better detection results, as ''mimetype'' uses the information in the [http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html shared mime info database].
  
 
=== Environment variables ===
 
=== Environment variables ===
Some environment variables, such as {{ic|BROWSER}}, {{ic|DE}}, and {{ic|DESKTOP_SESSION}}, will change the behaviour of the default xdg-open. See [[Environment Variables]] for more information.
 
  
== See also ==
+
Some environment variables, such as {{ic|BROWSER}}, {{ic|DE}}, and {{ic|DESKTOP_SESSION}}, will change the behaviour of the default ''xdg-open''. See [[Environment variables]] for more information.
* [[Default Applications]] - Desktop-specific instructions/overview of alternatives to xdg-open
+
* [[Environment Variables]]
+

Latest revision as of 08:39, 24 January 2016

Merge-arrows-2.pngThis article or section is a candidate for merging with Default applications.Merge-arrows-2.png

xdg-open is a desktop-independent tool for configuring the default applications of a user. Many applications invoke the xdg-open command internally.

Inside a desktop environment (like GNOME, KDE, or Xfce), xdg-open simply passes the arguments to those desktop environment's file-opener application (eg. gvfs-open, kde-open, or exo-open). which means that the associations are left up to the desktop environment.

When no desktop environment is detected (for example when one runs a standalone window manager like eg. Openbox), xdg-open will use its own configuration files.

Installation

xdg-open is part of the xdg-utils package available in the official repositories. It is for use inside a desktop session only, and should not be run as root.

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.

Configuration

xdg-open is configured by the files mentioned in Default applications. xdg-mime modifies the local file ~/.config/mimeapps.list and ~/.local/share/applications/mimeapps.list (deprecated).

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

$ xdg-mime query filetype file.ext

Reversely, to query the default desktop entry associated with a specific mime type, run

$ xdg-mime query default mime/type

Examples for mime/type can be:

inode/directory (file browser)
image/jpeg (JPEG images)
application/pdf (PDF viewer)
or others

To change an associated desktop entry, use:

$ xdg-mime default application.desktop mime/type

For example set Thunar as the default file browser just run:

$ xdg-mime default Thunar.desktop inode/directory

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

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

Reason: There are many more MIME types for browsers (Discuss in Talk:Xdg-open#)

To set the default application for http(s):// web URLs, write

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

To do so for .html files:

$ xdg-mime default browser.desktop text/html

Alternatively, run:

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

To test if this was applied successfully, try to open an URL with xdg-open as follows:

$ xdg-open https://archlinux.org

perl-file-mimeinfo

xdg-open uses perl-file-mimeinfo as a fallback ("generic") method if no desktop environment is detected. It can be invoked directly with:

$ mimeopen -d /path/to/file

You are asked which application to use when opening /path/to/file:

Please choose a default application for files of type text/plain
       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.

Drop-in replacements and useful tools

xdg-open replacements

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

Reason: There may be some benefit to making this a table, but Template:App including upstream URLs gives all needed information in a simple manner (Discuss in Talk:Xdg-open#)
Name/Package Method Based on Configuration file
busking-gitAUR[broken link: archived in aur-mirror] Regular expressions perl-file-mimeinfo custom
linopenAUR file custom
mimeoAUR MIME-type, regular expressions file mimeapps.list, defaults.list; custom is optional
mimi-gitAUR file custom
whippetAUR MIME-type, name, regular expressions SQLite database or file, perl-file-mimeinfo, etc custom SQLite database or mimeapps.list
ayrAUR MIME-type, name, regular expressions file or perl-file-mimeinfo, etc mimeapps.list, defaults.list
sx-openAUR[broken link: archived in aur-mirror] Regular expressions file, bash regex custom
ranger (rifle command) MIME-type, name, regular expressions custom
Note: Some of the above packages replace xdg-utils. Those that do not can be symbolically linked to xdg-open in the user's $PATH above /usr/bin, but some applications hard-code the absolute path /usr/bin/xdg-open. In this case, install xdg-utils-no-openAUR from the AUR and copy the replacement to /usr/bin/xdg-open.

mailcap

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

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

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"

mimetype

mimetype in perl-file-mimeinfo package can display some mimetype-related information about a file.

For example:

$ mimetype file.ext

returns the mimetype of a file,

$ mimetype -d file.extension

returns a description of that mimetype.

When xdg-open fails to detect one of the desktop environments it knows about, it normally falls back to using file -i, which uses only file contents to determine the mimetype, resulting in some file types not being detected correctly. With mimetype available, xdg-open will use that instead, with better detection results, as mimetype uses the information in the shared mime info database.

Environment variables

Some environment variables, such as BROWSER, DE, and DESKTOP_SESSION, will change the behaviour of the default xdg-open. See Environment variables for more information.