New post: Change default application on Linux from terminal

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)
-rw-r--r-- content/posts/2026-01-15-change-default-applications-on-linux.md 101
-rw-r--r-- templates/index.html 2
2 files changed, 102 insertions, 1 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
  
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/
diff --git a/templates/index.html b/templates/index.html
...
9
		{{ if and (not .Draft) (or (eq .Type "note") (eq .Type "post")) }}
9
		{{ if and (not .Draft) (or (eq .Type "note") (eq .Type "post")) }}
10
		<li>
10
		<li>
11
			<time datetime="{{ .Created.Format "2006-01-02" }}" class="static-prefix">{{ .Created.Format "2006-01-02" }}</time>
11
			<time datetime="{{ .Created.Format "2006-01-02" }}" class="static-prefix">{{ .Created.Format "2006-01-02" }}</time>
12
			<span>
12
			<span title="Note">
13
				<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 512 512" style="display: block">
13
				<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 512 512" style="display: block">
14
					{{ if (eq .Type "note") }}
14
					{{ if (eq .Type "note") }}
15
					<path fill="black" fill-rule="evenodd" d="M448 343.045 343.045 448H64V64h384v279.045Zm-42.667-65.712V106.667H106.667v298.666h170.666v-128h128Zm0 42.667H320v85.333h5.333l80-80V320Z"/>
15
					<path fill="black" fill-rule="evenodd" d="M448 343.045 343.045 448H64V64h384v279.045Zm-42.667-65.712V106.667H106.667v298.666h170.666v-128h128Zm0 42.667H320v85.333h5.333l80-80V320Z"/>
...