blob: 9e5990adb0d1f3fb4e7d3cb74d606dc678cde50b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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 <gio/gio.h>
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/
|