Difference between revisions of "Xdg-open"

From ArchWiki
Jump to: navigation, search
(mimetype: add a link to shared mime info)
(run-mailcap: removing the section and replacing it with something relevant to xdg-open. If you want to have run-mailcap here, make sure it *works*, then add it to the alternative table)
Line 76: Line 76:
 
=== run-mailcap ===
 
=== run-mailcap ===
  
{{Accuracy|Supposed to be "way easier", then goes into a loop of ugly hacks. Should be either deleted or drastically simplified}}
+
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}}:
  
The ''.mailcap'' file format is used by mail programs usch as {{Pkg|mutt}} and {{Pkg|sylpheed}}. The {{AUR|run-mailcap}} package from the AUR provides a executable that can be symlinked to {{ic|/usr/bin/xdg-open}}, but its parser is unable to handle directories or links. A workaround is to install ''mimeo'' from the AUR too and create a {{ic|/usr/bin/xdg-open}} with the following content:
+
{{hc|~/.mailcap|
 
+
*/*; xdg-open "%s
#!/bin/sh
+
}}
run-mailcap $1 || mimeo $1
 
 
 
Then use the {{ic|mimeapps.list}} at {{ic|~/.local/share/applications/}} to associate URLs and folders with applications and the ''.mailcap'' file for normal mimetypes.
 
  
 
=== mimetype  ===
 
=== mimetype  ===

Revision as of 18:24, 22 January 2015

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 (e.g. GNOME, KDE, or Xfce), xdg-open simply passes the arguments to that desktop environment's file-opener application (gvfs-open, kde-open, or 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), 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 ~/.local/share/applications/mimeapps.list.

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 to a specific mime type, run xdg-mime query default mime/type. Common mime types include inode/directory (file browser), image/jpeg (JPEG images), and application/pdf (PDF viewer).

To change the default desktop entry, use xdg-mime default application.desktop mime/type. To for example set Thunar as the default file browser, run:

$ xdg-mime default Thunar.desktop inode/directory

This command can take multiple mime-types, allowing related mime types to be handled by the same program. This example matches a pattern against /etc/mime.types (from mime-types), to match Emacs against all text files:

$ xdg-mime default emacs.desktop $(grep 'text/*' /etc/mime.types | awk '{ print $1 }')

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):// links:

$ 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

Drop-in replacements and useful tools

xdg-open replacements

Name/Package Method Based on Configuration file
busking-gitAUR 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
easeAUR MIME-type, name, regular expressions mimeapps.list, SQLite database
ayrAUR MIME-type, name, regular expressions file or perl-file-mimeinfo mimeapps.list, mimeinfo.cache, (defaults.list) to determine mimetype associations)
sx-open Regular expressions file, bash regex custom

run-mailcap

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.