Results 1 to 4 of 4

Thread: Web Searches on the X Selection

  1. #1
    Join Date
    Oct 2007
    Beans
    26
    Distro
    Ubuntu 9.10 Karmic Koala

    HOWTO: Use the X Selection to Search the Web

    Kia-ora! This is my first tutorial.

    Testbed

    I have used this on Firefox, versions 3.0.3 through 3.5.7 as well as Google Chrome, version 5.03 and Ubuntu Feisty through Karmic including Ubuntu Netbook Remix, and Mint Linux 6 Felicia.

    The aim is to create simple keyboard short cuts that will perform various web searches on the currently highlighted text, from any application on the desktop.

    There are two methods given here. If you have Compiz and CompizConfig Settings Manager installed, skip down to the The CompizConfig Method, otherwise read on for The xbindkeys Method.

    An alternative method given below by simon_w is to attach the function to a panel launcher via a script.

    The xbindkeys Method

    *I have a quick one-liner if you are the impatient type skip ahead to - The Quick and Dirty Method

    1. Install the tools
      The method I use combines 3 small tools. This tutorial assumes that you have none of them installed. To install them, open the Terminal and enter the following commands:

      Code:
      sudo apt-get install xbindkeys xbindkeys-config xsel
      In brief, xbindkeys is a small but powerful command line application that assigns commands to user defined key combinations. Incidentally, it is hugely useful for customizing or enabling multimedia keyboards or multi-button mice.

      xbindkeys-config provides a graphical user interface for assigning these keyboard short cuts. I have experienced errors with this tool when starting without first creating a configuration file - (~/.xbindkeysrc). Create an ~/.xbindkeysrc file with the command:

      Code:
      xbindkeys --defaults > ~/.xbindkeysrc
      It shouldn't surprise you that configuration changes can be achieved by editing the ~/.xbindkeysrc file by hand. If you want to edit the configuration by hand or examine it more closely, have a look at the 'MANUAL CONFIGURATION' section below.

      Aside from making sure an ~/.xbindkeysrc file has been created in advance, I have found xbindkey-config to be a very useable application.

      Note:

      xbindkeys has to be running before xbindkeys-config will start. In addition xbindkeys has to be running before your short cuts will work. I start xbindkeys automatically on session startup using the following method:


      • From the Desktop, Go to System > Preferences > Session, (In Jaunty this has been changed to System >Preferences > Startup Applications)
      • Select Startup Programs, click "Add", and type in xbindkeys in the Name and Command fields. Click Add and close the Startup dialogue.



      xsel is a flexible and extremely useful command line application. Among other uses, xsel can output the contents of the text currently highlighted (sometimes referred to as the X Selection), into another command.

      These three applications are all in the software repositories for Dapper through Karmic.

    2. Create the keyboard short cut
      In this example we will create a basic Google search.

      To configure xbindkeys manually, skip forward to 'Manual xbindkeys Configuration', else continue:

      In a Terminal, enter the following command to start the xbindkeys-config application:

      Code:
      xbindkeys
      xbindkeys-config
      This should open the xbindkeys-config application. You should see a list box to the left, some controls to the bottom and fields and controls to the right, similiar to this:



      To create the shortcut, click 'New' at the bottom.

    3. Give the short cut a suitable name in the 'Name:' field.

    4. Click 'Get Key'. A small blank window will open. Key in your chosen keyboard short cut. (I have used 'Ctrl G')

      After you have selected your short cut, the small blank window will close and a new string of text will appear in the 'Key:' field. In my 'Ctrl G' example I get the string:

      'Control+Mod2 + g | m:0x14 + c:42'

      but you may get something different.

    5. Lastly in the 'Action:' field, enter the command you would like assigned to the short cut. To search www.google.co.nz, enter:

      firefox http://www.google.co.nz/search?q=`xsel -p|perl -pe 'tr/ /+/'`

      or in the case of Google Chrome:

      google-chrome http://www.google.co.nz/search?q=`xsel -p|perl -pe 'tr/ /+/'`
    6. Save your short cut and close the application by clicking 'Save & Apply & Exit'
    7. Test your short cut by highlighting some text and keying your short cut combination.


    Another excellent tip by simon_w below is to use gconftool to find the default browser with:

    Code:
    gconftool-2 --get '/desktop/gnome/url-handlers/http/command'
    'Manual xbindkeys Configuration'

    This section describes a more hands-on configuration method. Dispense with the steps above, ensuring only that you have xbindkeys and xsel installed on your system:

    Code:
    sudo apt-get install xbindkeys xsel
    xbindkeys includes a nifty control flag '--keys' (or '-k' for short), which allows the capture of keyboard commands. It prints the actual key code required in the xbindleys configuration file ~/.xbindkeysrc. If that sounds confusing, try these instructions:

    1. Ensure xbindkeys is running and you have created a file ~/.xbindkeysrc. If not, start xbindkeys and create a default configuration file in a terminal with the command:

      Code:
      xbindkeys --defaults > ~/.xbindkeysrc
    2. Open this file for editing. I use gedit in this example because emacs isn't available to me on a vanilla install :

      Code:
      gedit ~/.xbindkeysrc &
    3. Scroll down to the last entry, which should look something like this:

      Code:
      ## Control + mouse button 2 release event starts rxvt
      #"rxvt"
      #  Control + b:2 + Release
    4. Insert a suitable title. I will use the example 'Super + G performs a Google search of highlighted text':

      Code:
      ## Super + G performs a Google search of highlighted text
    5. Next you will insert the required configuration for your keyboard shortcut. Return to the terminal and bring up the keyboard capture dialog, with the command:

      Code:
      xbindkeys -k
    6. Key your desired shortcut. Once you have selected one the terminal will print the required configuration. We are only interested in the last 3 lines. In my example:

      Code:
      "(Scheme function)"
          m:0x40 + c:40
          Mod4 + d
    7. Copy these three lines into your edited ~/.xbindkeysrc, below your new title:

      Code:
      ## Super + G performs a Google search of highlighted text
      "(Scheme function)"
          m:0x40 + c:40
          Mod4 + d
    8. Next we will insert our desired command. Replace '(Scheme function)' with your desired command. In my example this is firefox http://www.google.co.nz/search?q=`xsel -p|perl -pe 'tr/ /+/'`:

      Code:
      ## Super + G performs a Google search of highlighted text
      "firefox http://www.google.co.nz/search?q=`xsel -p|perl -pe 'tr/ /+/'`"
          m:0x40 + c:40
          Mod4 + d
    9. Save ~/.xbindkeysrc, exit your text editor and you're done!
    10. Test your short cut by highlighting some text and keying your short cut combination.


    Websearch X?

    Setting other short cuts is as simple as repeating steps 2 through 7 using the appropriate url search string.

    eg. to search www.torrentz.com, set the xbindkeys 'Action:' to:

    firefox http://www.torrentz.com/search?q=`xsel -p|perl -pe 'tr/ /+/'`

    And assign an appropriate short cut.

    Thanks simon_w for your suggestion to replace

    Code:
    `xsel -p|perl -pe 'tr/ /+/'`
    with

    Code:
    `xsel -p | tr [:space:] +`
    Other searches I use include:


    I have found this desktop-wide search ability enormously useful. Error messages that pop up on Gnome or Ubuntu or even in the Terminal can be 'Googled' in a flash. I am using Wikipedia searches to help me with a Management essay I am plagiarising at the moment. . Translations, currency and unit conversions, searches of the repository, image search, Google Desktop search...

    There are other ways to achieve this functionality but hopefully someone out there will save a little of the time I spent trying to get this working.

    References

    X Window System
    xbindkeys
    Configuring Startup Applications

    The Quick and Dirty Method

    I use this command to save myself time (I back-up my ~/.xbindkeysrc file remotely at dotfiles.org):

    Code:
    sudo apt-get install xbindkeys xsel curl && xbindkeys --defaults > ~/.xbindkeysrc && curl -N http://dotfiles.org/~light50/.xbindkeysrc > ~/.xbindkeysrc && xbindkeys
    The CompizConfig Method


    Install xsel

    1. Code:
      sudo apt-get install xsel
      xsel is a flexible and extremely useful command line application. Among other uses, xsel can output the contents of the text currently highlighted (sometimes referred to as the X Selection), into another command.

      xsel is in the software repositories for Dapper through Karmic.


    Create the keyboard short cut
    1. Select Preferences then CompizConfig Settings Manager.
    2. Select General Options.
    3. Select the Commands tab then the Commands drop down section.
    4. To search www.google.co.nz, insert the following command into a free Command line space:

      firefox http://www.google.co.nz/search?q=`xsel -p|perl -pe 'tr/ /+/'`
    5. Open the Key bindings drop down section.
    6. Activate the Edit the Run command dialog that corresponds with your command.
    7. Select Grab key combination.
    8. Key your desired shortcut.
    9. Select OK.
    10. Exit the CompizConfig Settings Manager.
    11. Test your short cut by highlighting some text and keying your short cut combination.


    I have Firefox set to open new tabs where ever possible as I prefer them.

    So for me at present if Firefox is open, each xsel search I perform will open in a new tab.

    If you have used Firefox for a while you will appreciate that the short cut may behave a little differently depending on your Firefox tab and window settings.

    Edits

    10:40 AM Friday, March 21 2008 - Tidied up readability
    11:05 AM Friday, March 21 2008 - Added note on running and automatically starting xbindkeys
    12:11 PM Sunday, March 23 2008 - Added note on successful install on Hardy
    3:46 AM Sunday, June 1 2008 - Corrected typo '...steps 2 through 9 using...' to '...steps 2 through 7 using...'
    8:47 AM Saturday, October 25 2008 - Updated testbed conditions
    8:23 AM Sunday, March 8 2009 - Added note on successful install on Intrepid
    11:59 AM Sunday, April 26 2009 - Added note on successful install on Jaunty
    12:14 PM Sunday, April 26 2009 - Updated reference to 'Configuring Startup Applications'
    10:19 PM Friday, May 15 2009 - Corrected typo '...torrentx.com...' to '...torrentz.com...'
    10:28 PM Friday, May 15 2009 - Added instruction to create an ~/.xbindkeysrc file
    12:17 PM Saturday, May 16 2009 - Added 'Manual xbindkeys COnfiguration' section
    12:29 PM Saturday, May 16 2009 - Tidied up format
    7:38 PM Tuesday, May 19 2009 - Added 'The Quick and Dirty Method'
    2:37 PM Saturday, May 31 2009 - Added 'CompizConfig Settings Manager Method'
    6:23 PM Monday, January 25 2010 - Added note on successful install on Karmic and UNR
    6:43 PM Monday, January 25 2010 - Added suggestions by simon_w
    5.23 PM Tuesday January 26 2010 - Updated Firefox version to 3.5.7
    8.10 AM Thursday April 15 2010 - Updated Firefox version. Added Google Chrome Compatibility. Added Currency COnversion function
    Last edited by light50; April 14th, 2010 at 10:44 PM.

  2. #2
    Join Date
    May 2006
    Location
    100acrewood
    Beans
    7,294
    Distro
    Kubuntu 17.10 Artful Aardvark

    Re: Web Searches on the X Selection

    Nice one! Thank you.

  3. #3
    Join Date
    Oct 2007
    Beans
    26
    Distro
    Ubuntu 9.10 Karmic Koala

    Smile Re: Web Searches on the X Selection

    *bump*

    Hi. I have made a few additions to this tut and would be keen to get any feedback. Have fun. Thanks.
    HP Pavilion zd8000 . HP Mini 1004TU . Ubuntu 9.04 Jaunty Jackalope
    Want to perform Web Searches on the X Selection?

  4. #4
    Join Date
    Oct 2007
    Location
    Taipei 台北
    Beans
    59

    Re: Web Searches on the X Selection

    this is cool

    if you want to use the default browser as specified in the Gnome settings, you can use this:

    Code:
    $(gconftool-2 --get '/desktop/gnome/url-handlers/http/command' | awk '{print $1}')
    also, I reckon `xsel -p | tr [:space:] +` is an improvement on `xsel -p|perl -pe 'tr/ /+/'` since it will cope with tabs and newline chars.

    Here is a short script I wrote which I use in panel launchers. it can also be bound to hotkeys using System -> Preferences -> Keyboard shortcuts, which is less hassle than messing with xbindkeys. still need xsel though.

    Code:
    #!/bin/bash
    #
    # by simon, Nov. 08
    #
    # Requirements:
    #
    #  assumes the Gnome Desktop environment (because that's what I use!),
    #   but could be extended easily enough
    #  xsel (sudo apt-get install xsel)
    #
    # some suggestions for use:
    #
    #  google.co.uk:
    #    xsel_to_web.sh http://www.google.co.uk/search?q=
    #
    #  dictionary.com:
    #    xsel_to_web.sh http://dictionary.reference.com/browse/
    #
    #  Wikipedia (en):
    #    xsel_to_web.sh http://en.wikipedia.org/wiki/
    #
    #
    
    # get the user's chosen browser (assumes Gnome)
    BROWSER=$(gconftool-2 --get '/desktop/gnome/url-handlers/http/command' | awk '{print $1}')
    
    # get website to link to from command line, or default to google
    if [ -z "$1" ]
    then
        LINK="http://www.google.com/search?q="
    else
        LINK=$1
    fi
    
    # get the search term from the X selection
    SEARCHTERM=$(xsel -p | tr [:space:] +)
    
    # go!
    ${BROWSER} "${LINK}${SEARCHTERM}"
    thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •