diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-15 08:50:39 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-15 08:50:39 +0100 |
| commit | 5fff3c1ac9c5d0f2ed4f0f5814dd237f94b35646 (patch) | |
| tree | 2dfb34a8cf6b1bacdb1e0fb6623dec5569900dce /content/posts/2026-01-15-change-default-applications-on-linux.md | |
| parent | fa69f57e4b1886e83862050cc660110ffa4c0609 (diff) | |
| download | mitjafelicijan.com-5fff3c1ac9c5d0f2ed4f0f5814dd237f94b35646.tar.gz | |
New post: Change default application on Linux from terminal
Diffstat (limited to 'content/posts/2026-01-15-change-default-applications-on-linux.md')
| -rw-r--r-- | content/posts/2026-01-15-change-default-applications-on-linux.md | 101 |
1 files changed, 101 insertions, 0 deletions
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 @@ | |||
| 1 | --- | ||
| 2 | title: Change default applications on Linux from terminal | ||
| 3 | url: change-default-applications-on-linux-from-terminal.html | ||
| 4 | date: 2026-01-15T16:13:13+02:00 | ||
| 5 | type: post | ||
| 6 | draft: false | ||
| 7 | tags: [] | ||
| 8 | --- | ||
| 9 | |||
| 10 | Changing default applications is done with command `xdg-mime`. This is a | ||
| 11 | command line tool for querying information about file type handling and adding | ||
| 12 | descriptions for new file types. Make sure you have this program installed. | ||
| 13 | |||
| 14 | Application will use `.desktop` files to associate applications with specific | ||
| 15 | types. | ||
| 16 | |||
| 17 | ## Location and structure of `.desktop` files | ||
| 18 | |||
| 19 | ```sh | ||
| 20 | ls /usr/share/applications | ||
| 21 | ls ~/.local/share/applications | ||
| 22 | ``` | ||
| 23 | |||
| 24 | You can add your own `.desktop` files to `~/.local/share/applications`. | ||
| 25 | |||
| 26 | An example of `.desktop` file for Brave browser located in | ||
| 27 | `~/.local/share/applications/brave.desktop`. | ||
| 28 | |||
| 29 | ```ini | ||
| 30 | [Desktop Entry] | ||
| 31 | Exec=/home/m/Applications/brave | ||
| 32 | Type=Application | ||
| 33 | Categories=Applications | ||
| 34 | Name=Brave Browser | ||
| 35 | ``` | ||
| 36 | |||
| 37 | ## Query current associations | ||
| 38 | |||
| 39 | You can query specific types with `xdg-mime`. | ||
| 40 | |||
| 41 | ```sh | ||
| 42 | xdg-mime query default text/plain | ||
| 43 | xdg-mime query default text/html | ||
| 44 | xdg-mime query default x-scheme-handler/http | ||
| 45 | xdg-mime query default x-scheme-handler/https | ||
| 46 | xdg-mime query default inode/directory | ||
| 47 | ``` | ||
| 48 | |||
| 49 | Or you can look at the files containing this data. | ||
| 50 | |||
| 51 | ```sh | ||
| 52 | less ~/.config/mimeapps.list | ||
| 53 | less /usr/share/applications/mimeapps.list | ||
| 54 | ``` | ||
| 55 | |||
| 56 | ## Set default application | ||
| 57 | |||
| 58 | ```sh | ||
| 59 | # Set Brave as default browser. | ||
| 60 | xdg-mime default brave.desktop x-scheme-handler/http | ||
| 61 | xdg-mime default brave.desktop x-scheme-handler/https | ||
| 62 | |||
| 63 | # Set Thunar as default file explorer. | ||
| 64 | xdg-mime default thunar.desktop inode/directory | ||
| 65 | |||
| 66 | # Set Mousepad as default txt editor. | ||
| 67 | xdg-mime default mousepad.desktop text/plain | ||
| 68 | ``` | ||
| 69 | |||
| 70 | ## Interfacing with C | ||
| 71 | |||
| 72 | This simple example lists all registered types. But you can see where this can | ||
| 73 | go. This could be turned into ncurses CLI application that is used for setting | ||
| 74 | and changing default applications. | ||
| 75 | |||
| 76 | ```c | ||
| 77 | #include <gio/gio.h> | ||
| 78 | |||
| 79 | int main(void) { | ||
| 80 | GList *types = g_content_types_get_registered(); | ||
| 81 | |||
| 82 | for (GList *l=types; l!=NULL; l=l->next) { | ||
| 83 | g_print("%s\n", (char *)l->data); | ||
| 84 | } | ||
| 85 | |||
| 86 | g_list_free_full(types, g_free); | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | ``` | ||
| 90 | |||
| 91 | Compile with `clang -o main main.c $(pkg-config --cflags --libs gio-2.0)`. | ||
| 92 | |||
| 93 | ## Reading material | ||
| 94 | |||
| 95 | - https://wiki.archlinux.org/title/XDG_MIME_Applications | ||
| 96 | - https://commandmasters.com/commands/xdg-mime-linux/ | ||
| 97 | - https://noman.sh/en/pages/xdg-mime | ||
| 98 | - https://linux.die.net/man/1/xdg-mime | ||
| 99 | - https://wiki.archlinux.org/title/XDG_MIME_Applications | ||
| 100 | - https://gnome.pages.gitlab.gnome.org/libsoup/gio/ | ||
| 101 | - https://docs.gtk.org/gio/ | ||
