Quick google search in Awesome window manager

I’m back to using AwesomeWM on my work desktop; not sure what brought me back, but I will say that overall I prefer the way it handles multiple monitors and multiple desktops a little better than how KDE does it.  That, and KWin’s tiling mode is still useless with dual monitors even in 4.8.

Something about running a window manager like Awesome makes you uber-sensitive to operations that require you to do a lot of mouse-maneuvering or manual window management, and thus encourages you to streamline these operations.  One such thing for me is searching google for something, an activity which I’m bound to do at least six dozen times during a workday, especially when developing (since I can’t remember API’s for squat).

With a little help from a bash script and surfraw, I came up with a pretty cool solution. 

First, you need to install surfraw.  Surfraw is a command-line tool for searching different search engines; it’s available in the Debian and Ubuntu repositories.  Once installed, you can call it like this:

sr google bunch of search terms

And it will open a new tab in your browser (or open a browser, if need be) with the google search.  Of course, you don’t have to use google, as surfraw comes with a variety of other search options.

You’ll also need wmctrl installed.

Once those were installed, I created a script in my ~/bin directory (which I’ve added to my path — you can of course put this script in /usr/local/bin/ or wherever) and called it “google_search.sh”:

#!/bin/bash
term=$@
foo=''
#search with surfraw
sr google $term
#wait until the window exists and registers with the WM
while [ -z "$foo" ]; do
 foo=$(wmctrl -l |grep -i "$term - Google Search")
 sleep .5
done
#could use wmctrl -a, but it's not always reliable in awesome with dual screens
wmctrl -r "$term - Google Search" -b add,demands_attention
echo "awful.client.urgent.jumpto()" |awesome-client

Basically this script does the following:

  1. assigns all arguments to a variable “term”
  2. runs surfraw with the search terms
  3. the while loop watches the window list for a window with your search terms and “Google Search” in the title
  4. Once it shows up, we mark the given window as “demands_attention” a.k.a. “urgent”
  5. Finally, we tell awesome to switch to the urgent client.

As I noted in the script, on a single monitor situation I could just use “wmctrl -a” to switch to the window instead of doing all that urgent nonsense, but I’ve found that to be unreliable with dual monitors (e.g., sometimes it switches to the right tag, but on the wrong screen).

Once I have this script somewhere in my $PATH, I add the shortcut to awesome, by adding these lines to the “globalkeys” table in my rc.lua:

-- surfraw search
 awful.key({ modkey, }, "g",
 function()
 awful.prompt.run({ prompt = "<span foreground='#7f9f7f'>Google Search:</span> "},
 mypromptbox[mouse.screen].widget,
 function(input)
 awful.util.spawn_with_shell("google_search.sh " .. input)
 end, nil
 )
 end
 ),

(Incidentally, you may want to remove or change the pango markup on the prompt to match your theme. I’m using zenburn, so it matches nicely).

After restarting Awesome, Mod4-g gives me a prompt to do a google search.  Works a treat!

2 Thoughts on “Quick google search in Awesome window manager

  1. This is how I’m doing this, based on your own code:

    awful.key({ modkey }, “g”, function ()
    local promptbox = mypromptbox[mouse.screen]
    awful.prompt.run(
    {prompt = “Search: “},
    promptbox.widget,
    function (string)
    local command = awful.util.spawn(“firefox https://duckduckgo.com/?q=” .. string)
    local result = awful.util.spawn(command)
    if type(result) == “string” then
    promptbox.widget.text = result
    end
    end,
    awful.completion.shell,
    awful.util.getdir(“cache”) .. “/history”)
    end)

    1. I’ve “awful.util.spawn” twice there, but you get the hang of it 😉

Leave a Reply

Your email address will not be published. Required fields are marked *