Highlight and Search – Google Search from Any Ubuntu Linux Application

Some time ago, I wrote a post to show how to modify terminator to enable easy google searching. However I have trumped myself and have an even better solution now. Google Search from any application. Modus Operandi is: Highlight Text => Press Key Combination => Script picks up the query from the clipboard => Formats the query and opens the URL in your defualt browser.

Lets start with the script. Copy-Paste the below code into a file (Lets name it google_search).

QUERY=`xclip -o | tr ' ' '+'`  #sanitize the query, change spaces to the plus sign
QUERY='"'$QUERY'"'    #put quotes around the query, exact search

if [ -x ~/bin/open_browser.sh ]; then
  open_browser.sh http://www.google.com/webhp?hl=en#hl=en\&q=$QUERY
  x-www-browser http://www.google.com/webhp?hl=en#hl=en\&q=$QUERY

Copy the file to the bin directory under your home directory. Make the script executable.

cp google_search ~/bin
chmod +x ~/bin/google_search

Now lets take care of a few dependencies.

Install xclip. This utility is used to interact with the clipboard.

sudo aptitude install xclip

Update x-www-browser. See this tutorial for more instructions.

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

Now lets create a keyboard shortcut for the script we saved earlier.

Tutorial for KDE Users
Tutorial for Gnome Users

Thats it. Now you can highlight any text and press the keyboard shortcut you created in the last step and a google search for the selected phrase will open up in your default browser.

This script also supports the open_browser.sh script. Look here for more info.
When you press the keyboard combination, the browser may or may not become the active window. That depends on the focus model. However if you check your browser, the google search should be open in a new tab

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.