Fedora Project community

Open a URL highlighted from anywhere on your desktop with this quick tip for Fedora

Sometimes when i am using certain applications (especially text editors), the applications themselves do not make URLs that are written out clickable and openable in my default browser. Usually, this would result in me having to highlight the link, copy it to the clipboard, switch to my web browser, open a new tab, paste the link and go.

However, the quick hack for Fedora described in this tutorial will set up a keyboard shortcut so you can simply highlight the link text, do a keyboard shortcut, and the link will open up in your default browser.

Step 1 — Download xsel

first up we need to download a little command line utility called xsel that will let us get at the clipboard selection from a bash script. Install xsel with the command:

sudo yum install xsel

Step 2 — create the script in your user’s bin directory

If you don’t already have it, create a directory called bin in the .local directory in your home directory:

mkdir ~/.local/bin

Then edit a new file in ~/.local/bin (i called mine openindefaultbrowser):

gedit ~/.local/bin/openindefaultbrowser

Now, add the following code to the openindefaultbrowser file, and save it.

#!/bin/bash

xdg-open $(xsel -p)

Now, finally, make the script executable with the command:

chmod +x ~/.local/bin/openindefaultbrowser

Step 3 — bind your new script to a keyboard shortcut

Open up the keyboard preferences, and switch to the “shortcuts” tab. Click “Custom Shortcuts” from the list on the left, and then click the + button to add a new shortcut. Name the shortcut whatever you want and put the name of your script in the command field and press add.

keyboardshortcuts

Now, click on the shortcut, and assign an keyboard combo. (i chose super+F).

Ready to go

Your new shortcut should be ready to go. To use it, highlight (select) the text of a url, press your keyboard shortcut, and your webbrowser should open up with the link you highlighted. Note that this script is not smart, if you highlight something that is not a url, and press the shortcut, it will try to open it as it was a url :)

 

About Ryan Lerch

Ryan is an Interaction Designer that works on stuff for Fedora. He uses Fedora as his primary desktop, along with the best tools from the Libre Graphics world, notably, Inkscape.

9 Comments

  1. Abdiel Rosario

    This is awesome!!!

  2. The script should probably go into ~/.local/bin to make selinux happy.

    • Thanks Christoph! I have updated the post. :)

    • DeadLock

      Wait. I miss the original location but whats the problem with ~/bin and selinux?

      • It will work with the targeted policy, but not with strict.

  3. pixecs

    A slightly updated script to handle selections without http protocol (at least this is the case on my opensuse)


    #!/bin/bash
    LINK=$(xsel -p)
    if [[ $LINK != http://* ]]
    then
    LINK="http://$LINK"
    fi
    xdg-open $LINK

    • nice!

      of course, another workaround to this would be just to use the command for your browser directly in the script, rather than the xdg-open one (that opens in the default app for whatever you pass it).

  4. Hy, someone with this error?
    ./openindefaultbrowser
    xdg-open: unexpected argument '+x'
    Try 'xdg-open --help' for more information.

    cat openindefaultbrowser
    #!/bin/bash
    xdg-open $(xsel -p)

    • Rafael,

      This script works my using the currently highlighted text (aka the middle click clipboard) as the URL to pass to the browser. It seems that the last thing you highlighted before running the script was the line to change the permissions of the script (i think that is where the “+x” is coming from) if you highlight a URL, and run the script it should work as expected.