Default applications: Difference between revisions

From ArchWiki
(add ja link)
(Replace {{AUR|handlr}} with {{pkg|handlr-regex}}. Add notice for regex support. Add replacement script for xdg-open)
 
(57 intermediate revisions by 24 users not shown)
Line 3: Line 3:
[[ru:Default applications]]
[[ru:Default applications]]
[[zh-hans:Default applications]]
[[zh-hans:Default applications]]
Programs sometimes need to open a file or a [[Wikipedia:Uniform Resource Identifier|URI]] in the user's preferred application. To open a file in the user's preferred application the filetype needs to be detected (usually using filename extensions or [[Wikipedia:List of file signatures|magic numbers]] mapped to [[Wikipedia:Media type|MIME types]]) and there needs to be an application associated with the filetype.
{{Expansion|Add more applications to the table. Do [[KDE]] applications use [https://doc.qt.io/qt-5/qdesktopservices.html#openUrl QDesktopServices::openUrl]? Mention {{man|1|exo-open}}.}}
 
Programs implement default application associations in different ways. While command-line programs traditionally use [[Environment variables#Default programs|environment variables]], graphical applications tend to use [[XDG MIME Applications]] through either the [[Wikipedia:GIO (software)|GIO]] API, the [[Qt]] API, or by executing {{ic|/usr/bin/xdg-open}}, which is part of [[xdg-utils]]. Because [[xdg-open]] and [[XDG MIME Applications]] are quite complex, various alternative [[#Resource openers|resource openers]] were developed. The following table lists example applications for each method.
 
{| class=wikitable
! Method !! Uses XDG !! Application examples !! Checking association
|-
| [https://docs.gtk.org/gio/iface.AppInfo.html GIO's GAppInfo] || Yes || [[Firefox]], [[GNOME Files]], [[PCManFM]], [[Thunar]], [[Thunderbird]], [[Telegram]] || {{ic|gio mime ''mimetype''}}
|-
| {{ic|/usr/bin/xdg-open}} || By default || [[Chromium]] (Open downloaded file) || {{ic|xdg-mime query default  ''mimetype''}}
|-
| Custom || Usually not || [[mc]], [[ranger]] || {{-}}
|-
| [[Environment variables#Default programs|Environment variables]] || No || [[man]], [[sudoedit]], [[systemctl]] || {{-}}
|-
| [[D-Bus]]'s FileManager1 || org.freedesktop.FileManager1 || [[Firefox]] (Open containing folder), Zotero (Show file), [[Telegram]] (Show in folder) || {{-}}
|}
 
Many [[desktop environment]]s and graphical [[file manager]]s provide a GUI for configuring default applications.
 
{{Note|[[XDG MIME Applications]] specification allows MIME type definition files to subclass and alias other types. This can cause non-obvious handling of some MIME types where applications do not directly support the target type, but support the one that it subclasses. For example Firefox can resolve {{ic|application/rss+xml}} via GIO to be eventually opened by itself, due to the type subclassing {{ic|application/xml}} and Firefox being the default handler for that. }}


* Some programs (particularly command-line programs) detect default programs using  [[environment variables]], see [[Environment variables#Default programs]].
== Background information ==
* Some programs (particularly heirloom UNIX programs) use [[Wikipedia:Media type#mime.types|mime.types]] for MIME type detection and [[Wikipedia:Media type#Mailcap|mailcap]] for application association.
* Many programs outsource the task completely to a [[#Resource openers|#Resource opener]].


Freedesktop.org has standardized filetype detection with the [[Shared MIME database]] specification and application association with the [[XDG MIME Applications]] specification.
Programs sometimes need to open a file or a [[Wikipedia:Uniform Resource Identifier|URI]] in the user's preferred application. To open a file in the user's preferred application the filetype needs to be detected (usually using filename extensions or [[Wikipedia:List of file signatures|magic numbers]] mapped to [[Wikipedia:Media type|MIME types]]) and there needs to be an application associated with the filetype.


Some desktop environments also provide a GUI or a file-manager which can interactively configure default applications. If you do not use a desktop environment, you may need to install additional software in order to conveniently manage default applications.
[[Heirloom]] UNIX programs used [[Wikipedia:Media type#mime.types|mime.types]] for MIME type detection and [[Wikipedia:Media type#Mailcap|mailcap]] for application association.


== Resource openers ==
== Resource openers ==
Line 19: Line 37:
* '''URI support''': allows arbitrary URI schemes to be associated with applications
* '''URI support''': allows arbitrary URI schemes to be associated with applications


{| class="wikitable"
{| class="wikitable sortable"
! Name !! Package !! XDG MIME Apps !! RegEx rules !! URI support
! Name !! Package !! XDG MIME Apps !! RegEx rules !! URI support
|-
|-
|[[xdg-open]] || [[xdg-utils]] || {{Yes}} || {{No}} || {{Yes}}
|[[xdg-open]] || [[xdg-utils]] || {{Yes}} || {{No}} || {{Yes}}
|-
|{{man|1|gio}} || {{pkg|glib2}} || {{Yes}} || {{-}} || {{Yes}}
|-
|-
|{{man|1p|mimeopen}} || {{Pkg|perl-file-mimeinfo}} || {{Yes}} || {{No}} || {{No}}
|{{man|1p|mimeopen}} || {{Pkg|perl-file-mimeinfo}} || {{Yes}} || {{No}} || {{No}}
Line 28: Line 48:
|mimeo || {{AUR|mimeo}} || {{Yes}} || {{Yes}} || {{Yes}}
|mimeo || {{AUR|mimeo}} || {{Yes}} || {{Yes}} || {{Yes}}
|-
|-
|whippet || {{AUR|whippet}} || {{Yes}} || {{No}} || {{Yes}}
|mimi || {{AUR|mimi}}, {{AUR|mimi-git}} || {{No}} || {{No}} || {{Y|partly}}
|-
|-
|linopen || {{AUR|linopen}} || {{No}} || {{Yes}} || {{Yes}}
|busking || {{AUR|busking-git}} || {{No}} || {{Yes}} || {{Yes}}
|-
|-
|mimi || {{AUR|mimi-git}} || {{No}} || {{No}} || {{Y|partly}}
|{{man|1|rifle}} || [[ranger]] || {{No}} || {{Yes}} || {{No}}
|-
|-
|busking || {{AUR|busking-git}} || {{No}} || {{Yes}} || {{Yes}}
|handlr || {{pkg|handlr-regex}} || {{Yes}} || {{Yes}} || {{Yes}}
|-
|-
|sx-open || {{AUR|sx-open}} || {{No}} || {{Yes}} || {{Yes}}
|clifm || {{AUR|clifm}} || {{No}} || {{Yes}} || {{Y|partly}}
|-
|-
|{{man|1|rifle}} || [[ranger]] || {{No}} || {{Yes}} || {{No}}
|mimejs-git || {{AUR|mimejs-git}} || {{No}} || {{No}} || {{Yes}}
|}
|}


Line 50: Line 70:


{{Pkg|perl-file-mimeinfo}} provides the tools {{man|1p|mimeopen}} and {{man|1p|mimetype}}. These have a slightly nicer interface than their {{pkg|xdg-utils}} equivalents:
{{Pkg|perl-file-mimeinfo}} provides the tools {{man|1p|mimeopen}} and {{man|1p|mimetype}}. These have a slightly nicer interface than their {{pkg|xdg-utils}} equivalents:
  # determine a file's MIME type
  # determine a file's MIME type
  $ mimetype photo.jpeg
  $ mimetype photo.jpeg
Line 67: Line 88:
  $ mimeopen -n photo.jpeg
  $ mimeopen -n photo.jpeg


Most importantly, [[xdg-utils]] programs will actually call {{ic|mimetype}} instead of {{ic|file}} for MIME type detection, if it does not detect your [[desktop environment]]. This is important because {{ic|file}} does not follow the XDG standard.
Most importantly, [[xdg-utils]] programs will actually call {{ic|file}} instead of {{ic|mimetype}} for MIME type detection if it does not detect your [[desktop environment]]. This is important because {{ic|file}} does not follow the XDG standard.


{{Note|{{pkg|perl-file-mimeinfo}} before 0.28-1 does not ''entirely'' follow the XDG standard. For example it does not read [https://github.com/mbeijen/File-MimeInfo/issues/20 distribution-wide defaults] and it saves its config in [https://github.com/mbeijen/File-MimeInfo/issues/8 deprecated locations].}}
{{Note|{{pkg|perl-file-mimeinfo}} before 0.28-1 did not ''entirely'' follow the XDG standard. For example it did not not read [https://github.com/mbeijen/File-MimeInfo/issues/20 distribution-wide defaults] and it saved its configuration in [https://github.com/mbeijen/File-MimeInfo/issues/8 deprecated locations].}}


=== mimeo ===
=== mimeo ===


{{aur|mimeo}} provides the tool {{ic|mimeo}}, which unifies the functionality of {{ic|xdg-open}} and {{ic|xdg-mime}}.
{{aur|mimeo}} provides the tool {{ic|mimeo}}, which unifies the functionality of {{ic|xdg-open}} and {{ic|xdg-mime}}.
In the following example we see how to associate SVG files with Inkscape:


  # determine a file's MIME type
  # determine a file's MIME type
  $ mimeo -m photo.jpeg
  $ mimeo --mimetype Svg_example2.svg
  photo.jpeg
  Svg_example2.svg
  image/jpeg
  image/svg+xml
# determine desktop file associated with executable "inkscape"
$ mimeo --app2desk inkscape
inkscape
  org.inkscape.Inkscape.desktop
 
# find desktop file path
$ mimeo --finddesk org.inkscape.Inkscape.desktop
/usr/share/applications/org.inkscape.Inkscape.desktop
   
   
  # choose the default application for this MIME type
  # choose the default application for this MIME type
  $ mimeo --add image/jpeg feh.desktop
  $ mimeo --prefer image/svg+xml org.inkscape.Inkscape.desktop
   
   
# check association
$ mimeo --mime2desk image/svg+xml
image/svg+xml
  org.inkscape.Inkscape.desktop
 
  # open a file with its default application
  # open a file with its default application
  $ mimeo photo.jpeg
  $ mimeo Svg_example2.svg
 
One can also find the path to the {{ic|mimeapps.list}} file:
$ mimeo --mimeapps-list
/home/user/.config/mimeapps.list


However a big difference with ''xdg-utils'' is that mimeo also supports custom "association files" that allow for more complex associations. For example, passing specific command line arguments based on a regular expression match:
However a big difference with ''xdg-utils'' is that mimeo also supports custom "association files" that allow for more complex associations. For example, passing specific command line arguments based on a regular expression match:
Line 94: Line 135:
{{aur|xdg-utils-mimeo}} patches ''xdg-utils'' so that {{ic|xdg-open}} falls back to mimeo if no desktop environment is detected.
{{aur|xdg-utils-mimeo}} patches ''xdg-utils'' so that {{ic|xdg-open}} falls back to mimeo if no desktop environment is detected.


=== whippet ===
=== handlr ===
 
{{pkg|handlr-regex}}, written in Rust, provides the functionality of {{ic|xdg-open}} and {{ic|xdg-mime}} with a streamlined interface.
{{pkg|handlr-regex}} is a fork of {{AUR|handlr}} with regex support.


{{aur|whippet}} provides the tool {{ic|whippet}}, which is similar to {{ic|xdg-open}}. It has X11 integration by using {{pkg|libnotify}} to display errors and {{pkg|dmenu}} to display choices between applications to open.
Compared to {{ic|xdg-utils}}, it includes:
* setting associations by extension, removing the need to look up or remember mime types
* validation for mime types and extensions
* removal of invalid entries from {{ic|mimeapps.list}}
* intelligent detection of mime types from file content in case filename is ambiguous
* autocompletion of mimes, extensions, and desktop entries
* setting arbitrary commands as handlers based on regular expressions


  # open a file with its default application
  # The following two are identical
  $ whippet -M photo.jpeg
handlr set .png feh.desktop
handlr set image/png feh.desktop
# List default apps
handlr list
# Get the handler for a mime/extension
  $ handlr get .png --json
{"handler":"pqiv.desktop","name":"pqiv","cmd":"/usr/bin/pqiv"}
   
   
  # choose from all possible applications for opening a file (without setting a default)
  # Launch a handler with optional path/URL
  $ whippet -m photo.jpeg
handlr launch x-scheme-handler/https
handlr launch x-scheme-handler/https -- https://google.ca
 
To use {{ic|handlr}} as a replacement for {{ic|xdg-open}}, shadow it with following script:
 
#!/usr/bin/bash
  handlr open "$@"
 
Alternatively, {{aur|xdg-utils-handlr}} provides a similar script replacing {{ic|/usr/bin/xdg-open}}, however previous co-maintainer carsme [https://aur.archlinux.org/packages/xdg-utils-handlr#comment-927741 recommended] to place the script in a user-scoped location such as {{ic|~/.local/bin}} instead.
 
=== clifm ===
 
''Lira'', {{ic|clifm}}'s built-in resource opener, can be used as a standalone resource opener via the {{ic|--open}} command line option. The configuration file ({{ic|~/.config/clifm/profiles/PROFILE_NAME/mimelist.clifm}}) supports regular expressions for both MIME types and file names (or file extensions). A few examples:


In addition to implementing [[XDG MIME Applications]], ''whippet'' can also use a SQlite database of weighted application/MIME type/regex associations to determine which app to use.
# Open a regular file
clifm --open /etc/hosts
# Open a directory
clifm --open /media/data
# Open an URL (via the application associated to the text/http MIME type in the configuration file)
clifm --open www.archlinux.org


=== Minimalist replacements ===
=== Minimalist replacements ===
Line 112: Line 189:
If you want to use one of these resource openers while still being able to use {{Pkg|xdg-utils}}, install them manually in a PATH directory before {{ic|/usr/bin}}.
If you want to use one of these resource openers while still being able to use {{Pkg|xdg-utils}}, install them manually in a PATH directory before {{ic|/usr/bin}}.


* {{AUR|linopen}} - 170-line Bash script, supports regex rules
* {{AUR|mimi}}, {{AUR|mimi-git}} - 130-line Bash script, can change command arguments for each MIME type
* {{AUR|mimi-git}} - 130-line Bash script, can change command arguments for each MIME type
* {{AUR|busking-git}} - 80-line Perl script similar to ''mimi'' but also supports regex rules
* {{AUR|busking-git}} - 80-line Perl script similar to ''mimi'' but also supports regex rules
* {{AUR|sx-open}} - 60-line Bash script, uses a simple shell-based config file


==== run-mailcap ====
==== run-mailcap ====


{{Warning|If you use {{AUR|run-mailcap}}, it is possible for {{ic|xdg-open}} to delegate to it. This will cause an infinite loop if you are using the {{ic|/etc/mailcap}} from {{Pkg|mailcap}}, because it also delegates to {{ic|xdg-open}}.}}
{{Warning|If you use {{AUR|run-mailcap}}, it is possible for {{ic|xdg-open}} to delegate to it. This will cause an infinite loop if you are using the {{ic|/etc/mailcap}} from {{Pkg|mailcap}}, because it also delegates to {{ic|xdg-open}}.}}

Latest revision as of 12:33, 22 April 2024

This article or section needs expansion.

Reason: Add more applications to the table. Do KDE applications use QDesktopServices::openUrl? Mention exo-open(1). (Discuss in Talk:Default applications)

Programs implement default application associations in different ways. While command-line programs traditionally use environment variables, graphical applications tend to use XDG MIME Applications through either the GIO API, the Qt API, or by executing /usr/bin/xdg-open, which is part of xdg-utils. Because xdg-open and XDG MIME Applications are quite complex, various alternative resource openers were developed. The following table lists example applications for each method.

Method Uses XDG Application examples Checking association
GIO's GAppInfo Yes Firefox, GNOME Files, PCManFM, Thunar, Thunderbird, Telegram gio mime mimetype
/usr/bin/xdg-open By default Chromium (Open downloaded file) xdg-mime query default mimetype
Custom Usually not mc, ranger
Environment variables No man, sudoedit, systemctl
D-Bus's FileManager1 org.freedesktop.FileManager1 Firefox (Open containing folder), Zotero (Show file), Telegram (Show in folder)

Many desktop environments and graphical file managers provide a GUI for configuring default applications.

Note: XDG MIME Applications specification allows MIME type definition files to subclass and alias other types. This can cause non-obvious handling of some MIME types where applications do not directly support the target type, but support the one that it subclasses. For example Firefox can resolve application/rss+xml via GIO to be eventually opened by itself, due to the type subclassing application/xml and Firefox being the default handler for that.

Background information

Programs sometimes need to open a file or a URI in the user's preferred application. To open a file in the user's preferred application the filetype needs to be detected (usually using filename extensions or magic numbers mapped to MIME types) and there needs to be an application associated with the filetype.

Heirloom UNIX programs used mime.types for MIME type detection and mailcap for application association.

Resource openers

  • XDG MIME Apps: implements the XDG MIME Applications specification
  • RegEx rules: allows MIME types to be associated with applications using regular expressions
  • URI support: allows arbitrary URI schemes to be associated with applications
Name Package XDG MIME Apps RegEx rules URI support
xdg-open xdg-utils Yes No Yes
gio(1) glib2 Yes Yes
mimeopen(1p) perl-file-mimeinfo Yes No No
mimeo mimeoAUR Yes Yes Yes
mimi mimiAUR, mimi-gitAUR No No partly
busking busking-gitAUR No Yes Yes
rifle(1) ranger No Yes No
handlr handlr-regex Yes Yes Yes
clifm clifmAUR No Yes partly
mimejs-git mimejs-gitAUR No No Yes

xdg-open

xdg-open (part of xdg-utils) implements XDG MIME Applications and is used by many programs.

Because of the complexity of the xdg-utils version of xdg-open, it can be difficult to debug when the wrong default application is being opened. Because of this, there are many alternatives that attempt to improve upon it. Several of these alternatives replace the /usr/bin/xdg-open executable, thus changing the default application behavior of most applications. Others simply provide an alternative method of choosing default applications.

perl-file-mimeinfo

perl-file-mimeinfo provides the tools mimeopen(1p) and mimetype(1p). These have a slightly nicer interface than their xdg-utils equivalents:

# determine a file's MIME type
$ mimetype photo.jpeg
photo.jpeg: image/jpeg

# choose the default application for this file
$ mimeopen -d photo.jpeg
Please choose an application

    1) Feh (feh)
    2) GNU Image Manipulation Program (gimp)
    3) Pinta (pinta)

use application #

# open a file with its default application
$ mimeopen -n photo.jpeg

Most importantly, xdg-utils programs will actually call file instead of mimetype for MIME type detection if it does not detect your desktop environment. This is important because file does not follow the XDG standard.

Note: perl-file-mimeinfo before 0.28-1 did not entirely follow the XDG standard. For example it did not not read distribution-wide defaults and it saved its configuration in deprecated locations.

mimeo

mimeoAUR provides the tool mimeo, which unifies the functionality of xdg-open and xdg-mime.

In the following example we see how to associate SVG files with Inkscape:

# determine a file's MIME type
$ mimeo --mimetype Svg_example2.svg
Svg_example2.svg
 image/svg+xml

# determine desktop file associated with executable "inkscape"
$ mimeo --app2desk inkscape
inkscape
 org.inkscape.Inkscape.desktop
 
# find desktop file path
$ mimeo --finddesk org.inkscape.Inkscape.desktop
/usr/share/applications/org.inkscape.Inkscape.desktop

# choose the default application for this MIME type
$ mimeo --prefer image/svg+xml org.inkscape.Inkscape.desktop

# check association
$ mimeo --mime2desk image/svg+xml
image/svg+xml
 org.inkscape.Inkscape.desktop
 
# open a file with its default application
$ mimeo Svg_example2.svg

One can also find the path to the mimeapps.list file:

$ mimeo --mimeapps-list
/home/user/.config/mimeapps.list

However a big difference with xdg-utils is that mimeo also supports custom "association files" that allow for more complex associations. For example, passing specific command line arguments based on a regular expression match:

# open youtube links in VLC without opening a new instance
vlc --one-instance --playlist-enqueue %U
  ^https?://(www.)?youtube.com/watch\?.*v=

xdg-utils-mimeoAUR patches xdg-utils so that xdg-open falls back to mimeo if no desktop environment is detected.

handlr

handlr-regex, written in Rust, provides the functionality of xdg-open and xdg-mime with a streamlined interface. handlr-regex is a fork of handlrAUR with regex support.

Compared to xdg-utils, it includes:

  • setting associations by extension, removing the need to look up or remember mime types
  • validation for mime types and extensions
  • removal of invalid entries from mimeapps.list
  • intelligent detection of mime types from file content in case filename is ambiguous
  • autocompletion of mimes, extensions, and desktop entries
  • setting arbitrary commands as handlers based on regular expressions
# The following two are identical
handlr set .png feh.desktop
handlr set image/png feh.desktop

# List default apps
handlr list

# Get the handler for a mime/extension
$ handlr get .png --json
{"handler":"pqiv.desktop","name":"pqiv","cmd":"/usr/bin/pqiv"}

# Launch a handler with optional path/URL
handlr launch x-scheme-handler/https
handlr launch x-scheme-handler/https -- https://google.ca

To use handlr as a replacement for xdg-open, shadow it with following script:

#!/usr/bin/bash
handlr open "$@"

Alternatively, xdg-utils-handlrAUR provides a similar script replacing /usr/bin/xdg-open, however previous co-maintainer carsme recommended to place the script in a user-scoped location such as ~/.local/bin instead.

clifm

Lira, clifm's built-in resource opener, can be used as a standalone resource opener via the --open command line option. The configuration file (~/.config/clifm/profiles/PROFILE_NAME/mimelist.clifm) supports regular expressions for both MIME types and file names (or file extensions). A few examples:

# Open a regular file
clifm --open /etc/hosts

# Open a directory
clifm --open /media/data

# Open an URL (via the application associated to the text/http MIME type in the configuration file)
clifm --open www.archlinux.org

Minimalist replacements

The following packages conflict with and provide xdg-utils because they provide their own /usr/bin/xdg-open script.

If you want to use one of these resource openers while still being able to use xdg-utils, install them manually in a PATH directory before /usr/bin.

  • mimiAUR, mimi-gitAUR - 130-line Bash script, can change command arguments for each MIME type
  • busking-gitAUR - 80-line Perl script similar to mimi but also supports regex rules

run-mailcap

Warning: If you use run-mailcapAUR, it is possible for xdg-open to delegate to it. This will cause an infinite loop if you are using the /etc/mailcap from mailcap, because it also delegates to xdg-open.