Open Links in Your Linux Distro’s Current Browser Instead of the Default

This post is inspired by this lifehacker post which shows how to open links in your Mac’s current browser instead of the default. I also use multiple browsers and face the same annoyance. Hence I decided to create a similar solution for linux too.

Features:
1) Opens the currently active browser even if its not the default browser.
2) If more than 2 browsers are active, opens the default browser if its running, otherwise it opens the longest running browser.
3) If no browser is active, opens the default browser.

To make sure the solution works, you will have to set the default x-www-browser for your distribution using the update-alternatives command. Use the command below.

sudo update-alternatives --config x-www-browser

Enter the number corresponding to the browser you want as your default browser.

Once that is done, its time to incorporate the script. I have included all the popular browsers included in most linux distributions, but its fairly easy to add any unsupported browser to this script. Copy the script at the end of this post and save it to a file preferably in the bin directory inside your home directory (For eg. /home/pratik/bin/open_browser.sh). Now, lets make the script executable.

chmod u+x open_browser.sh

Its time to set your window manager’s default browser setting. I am including the screenshots for kde.

Open “Default Applications” through the Run Command Interface (Alt + F2) or the Application Launcher (Alt + F1).

Set open_browser.sh that was created above as your default browser.

For gnome, the default application should be set to something like:

open_browser.sh %s

Thats it, now open a link from Tweetdeck or any other application and it should open the way you want it to.

#!/bin/bash
browsers=( firefox chromium-browse opera epiphany galeon midori konqueror rekonq dillo )
noofbrowsers=${#browsers[*]}
browserstatus=0
for browser in ${browsers[*]}
do 
  pgrep $browser >> /tmp/log
  active=$?
  if [ "0" == "$active" ]; then
    activebrowser=$browser
  fi
  let browserstatus+=$active
done
if [ "$browserstatus" == "$noofbrowsers" ]; then  #no browsers actives, open the default browser
  x-www-browser $1 &
elif (( $browserstatus < $((noofbrowsers-1)) )); then #more than two browsers are open, open the default browser
#more than two browsers open, open the default if its active, or the longest running browser
    defaultbrowser=`update-alternatives --query x-www-browser | grep Value | tr '/' '\n' | tail -1`
    if [ "$defaultbrowser" == "chromium-browser" ]; then
        defaultbrowser="chromium-browse"    #hack for chromium
    fi
    pgrep $defaultbrowser
    if [ "0" == "$?" ]; then
        x-www-browser $1 &
    else
        command="ps O stime"
        for browser in ${browsers[*]}
        do
            command="$command -C ${browser}"
        done
    activebrowser=`$command | head -2 | tail -1 | tr ' ' '\n' | tail -1 | tr '/' '\n' | tail -1`
        $activebrowser $1 &
    fi

else #open the active browser
  if [ "$activebrowser" == "chromium-browse" ]; then
        activebrowser="chromium-browser"    #hack for chromium
    fi
  $activebrowser $1 &
fi


Let me know if this solution worked for you.

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

3 comments

Leave a Reply to montuos Cancel reply