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
- 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.
- 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.
- Give the short cut a suitable name in the 'Name:' field.
- 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.
- 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/ /+/'`
- Save your short cut and close the application by clicking 'Save & Apply & Exit'
- 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:
- 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
- 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 &
- 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
- 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
- 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:
- 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
- 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
- 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
- Save ~/.xbindkeysrc, exit your text editor and you're done!
- 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
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
- Select Preferences then CompizConfig Settings Manager.
- Select General Options.
- Select the Commands tab then the Commands drop down section.
- 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/ /+/'`
- Open the Key bindings drop down section.
- Activate the Edit the Run command dialog that corresponds with your command.
- Select Grab key combination.
- Key your desired shortcut.
- Select OK.
- Exit the CompizConfig Settings Manager.
- 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
Bookmarks