Automatically switching the default browser
I use two browsers in my daily life: Chromium for work-related stuff and Firefox for everything else. This helps me to keep a strict separation between these two worlds, including: logins, bookmarks, history, settings (e.g. proxy, synchronization), etc. Let’s see how to make a default browser switch automatically to the correct one.
Note: You could probably do the same using profiles, but for me, it is much easier to just have two completely independent browsers
🔗CLI magic
To set the default browser (Firefox in this case) on a user-level, you need to execute two commands:
The first one tells your Desktop Environment to use the new default. However, to my testing, this is not always enough. We need to update ~/.config/mimeapps.list
file as well and that is what the second command is for.
🔗Automation with systemd
This is all nice and simple, but our goal is to stop thinking when to do the switch, so we should automate this. Cron is not a good choice for this: our computer might be down. There is also at utility, but I am unsure if it will execute commands after boot if the time is missed. We will rely on systemd for this.
First, let’s create one-shot “services” with our commands. We can do this on a user-level:
[Unit]
Set Firefox as the default browser
[Service]
/usr/bin/xdg-settings set default-web-browser firefox.desktop
/usr/bin/xdg-mime default firefox.desktop x-scheme-handler/http x-scheme-handler/https
oneshot
[Unit]
Set Chromium as the default browser
[Service]
/usr/bin/xdg-settings set default-web-browser chromium.desktop
/usr/bin/xdg-mime default chromium.desktop x-scheme-handler/http x-scheme-handler/https
oneshot
Now we need to create corresponding timers:
[Unit]
Set Firefox as default browser in the evenings
[Timer]
_-_-\* 18:00:00
true
[Install]
.target
timers
[Unit]
on workdays
Set Chromium as default browser
[Timer]
-Fri _-_-\* 06:00:00
Montrue
[Install]
.target
timers
All we need to do now is to enable and start these timers:
We can check if they are scheduled correctly:
Hint When going on vacation you can just disable one of the timers:
systemctl disable --user set-browser-chromium.timer
Done!