From 5fff3c1ac9c5d0f2ed4f0f5814dd237f94b35646 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Thu, 15 Jan 2026 08:50:39 +0100 Subject: New post: Change default application on Linux from terminal --- ...6-01-15-change-default-applications-on-linux.md | 101 +++++++++++++++++++++ templates/index.html | 2 +- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 content/posts/2026-01-15-change-default-applications-on-linux.md diff --git a/content/posts/2026-01-15-change-default-applications-on-linux.md b/content/posts/2026-01-15-change-default-applications-on-linux.md new file mode 100644 index 0000000..9e5990a --- /dev/null +++ b/content/posts/2026-01-15-change-default-applications-on-linux.md @@ -0,0 +1,101 @@ +--- +title: Change default applications on Linux from terminal +url: change-default-applications-on-linux-from-terminal.html +date: 2026-01-15T16:13:13+02:00 +type: post +draft: false +tags: [] +--- + +Changing default applications is done with command `xdg-mime`. This is a +command line tool for querying information about file type handling and adding +descriptions for new file types. Make sure you have this program installed. + +Application will use `.desktop` files to associate applications with specific +types. + +## Location and structure of `.desktop` files + +```sh +ls /usr/share/applications +ls ~/.local/share/applications +``` + +You can add your own `.desktop` files to `~/.local/share/applications`. + +An example of `.desktop` file for Brave browser located in +`~/.local/share/applications/brave.desktop`. + +```ini +[Desktop Entry] +Exec=/home/m/Applications/brave +Type=Application +Categories=Applications +Name=Brave Browser +``` + +## Query current associations + +You can query specific types with `xdg-mime`. + +```sh +xdg-mime query default text/plain +xdg-mime query default text/html +xdg-mime query default x-scheme-handler/http +xdg-mime query default x-scheme-handler/https +xdg-mime query default inode/directory +``` + +Or you can look at the files containing this data. + +```sh +less ~/.config/mimeapps.list +less /usr/share/applications/mimeapps.list +``` + +## Set default application + +```sh +# Set Brave as default browser. +xdg-mime default brave.desktop x-scheme-handler/http +xdg-mime default brave.desktop x-scheme-handler/https + +# Set Thunar as default file explorer. +xdg-mime default thunar.desktop inode/directory + +# Set Mousepad as default txt editor. +xdg-mime default mousepad.desktop text/plain +``` + +## Interfacing with C + +This simple example lists all registered types. But you can see where this can +go. This could be turned into ncurses CLI application that is used for setting +and changing default applications. + +```c +#include + +int main(void) { + GList *types = g_content_types_get_registered(); + + for (GList *l=types; l!=NULL; l=l->next) { + g_print("%s\n", (char *)l->data); + } + + g_list_free_full(types, g_free); + return 0; +} +``` + +Compile with `clang -o main main.c $(pkg-config --cflags --libs gio-2.0)`. + +## Reading material + +- https://wiki.archlinux.org/title/XDG_MIME_Applications +- https://commandmasters.com/commands/xdg-mime-linux/ +- https://noman.sh/en/pages/xdg-mime +- https://linux.die.net/man/1/xdg-mime +- https://wiki.archlinux.org/title/XDG_MIME_Applications +- https://gnome.pages.gitlab.gnome.org/libsoup/gio/ +- https://docs.gtk.org/gio/ diff --git a/templates/index.html b/templates/index.html index 36b828d..7de8ba2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,7 +9,7 @@ {{ if and (not .Draft) (or (eq .Type "note") (eq .Type "post")) }}
  • - + {{ if (eq .Type "note") }} -- cgit v1.2.3