The executable in Linux that opens a resource with your "favorite" program is called xdg-open
. It is somewhat complicated, but you can add additional rules using xdg-mime install
. However, I don't know if you can even do this for something like a specific URL.
This becomes further complicated in that specific desktop environments use different executables and methods for opening their default programs. For instance KDE has kde-open
, but not every KDE application seems to use this.
Regardless, I got this partially working just using a shell script and a list of domains/URLs in a file. You can tweak it to your liking.
Note: You may need to repeat this process for other executables depending on your desktop environment. For example, Gnome has gvfs-open
, KDE has kde-open
, and XFCE has exo-open
. (Even then, it might not work for every application.)
Note: The shell script is dependent on pcregrep
being installed on your system.
Find the xdg-open
executable
which xdg-open
Create a new directory in your home folder
mkdir -p ~/.local/bin
Add the following to your ~/.bashrc
file
~/.bashrc
export PATH="${HOME}/.local/bin:${PATH}"
Create the shell script in the newly created folder. (Be sure to edit the top four variables so they are correct for your system!)
~/.local/bin/xdg-open
#!/bin/bash
DOMAIN_LIST_FILE=~/'domains.txt'
OTHER_BROWSER='/path/to/other-browser' # For instance /usr/bin/firefox
BROWSER_OPTIONS='' # Optional, for command line options passed to browser
XDG_OPEN='/path/to/xdg-open'
if echo "$1" | pcregrep -q '^https?://'; then
matching=0
while read domain; do
if echo "$1" | pcregrep -q "^https?://${domain}"; then
matching=1
break
fi
done < "$DOMAIN_LIST_FILE"
if [[ $matching -eq 1 ]]; then
"$OTHER_BROWSER" $BROWSER_OPTIONS ${*}
exit 0
fi
fi
"$XDG_OPEN" ${*}
Create the list of domains in your home folder
~/domains.txt
stackexchange.com
stackoverflow.com
superuser.com
Log out and log back in to have the settings take effect