December 8, 2007

Gmail notify for dzen

16 June 2008 Update: I have updated the notifier - please read here for details


Notification of new mail in my gmail account is very important to me on my desktop and therefore that was the first thing I tried to solve once I set up xmonad and dzen. Fortunately, it wasn't that difficult. The following script checks the mail and formats the output for the latest development version of dzen2 (see previous post).
To use it, add your username and password and edit the other variables to change the color of the output, number of new mails to show and words to show in the subject for each mail. Note that I have opted to put the password in a hidden file with proper permissions and the script reads the file for the password.

You can read more about this and download the script from the dzen wiki here.




#!/usr/bin/env python

#========================================================================
# Copyright 2007 Raja <rajajs@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#==========================================================================

# ======================================================================
# Modified from code originally written by Baishampayan Ghose
# Copyright (C) 2006 Baishampayan Ghose <b.ghose@ubuntu.com>
# ======================================================================


import urllib
import feedparser

_url = "https://mail.google.com/gmail/feed/atom"

################## Edit here #######################

#pwd = xxxx # pwd stored in script
_pwdfile = '/path_to/hidden/pwd_file' # pwd stored in a file
_username = 'XXXX'
_calmcolor = 'white'
_alertcolor = 'red'
_maxmails = 5 # maximum new mails to show
_maxwords = 3 # maximum words to show in each mail header

###########################################################

class GmailRSSOpener(urllib.FancyURLopener):
'''Logs on with stored password and username
Password is stored in a hidden file in the home folder'''

def prompt_user_passwd(self, host, realm):
#uncomment line below if password directly entered in script.
pwd = open(_pwdfile).read()
return (_username, pwd)

def auth():
'''The method to do HTTPBasicAuthentication'''
opener = GmailRSSOpener()
f = opener.open(_url)
feed = f.read()
return feed

def showmail(feed):
'''Parse the Atom feed and print a summary'''
atom = feedparser.parse(feed)
newmails = len(atom.entries)
if newmails == 0:
title = "^fg(%s) You have no new mails" % (_calmcolor)
elif newmails == 1:
title = "^fg(%s) You have 1 new mail" % (_alertcolor)
else:
title = "^fg(%s) You have %s new mails" % (_alertcolor,newmails)

# print the title with formatting
print "^tw()" +title
#clear the slave window
print "^cs()"

#then print the messages
for i in range(min(_maxmails,newmails)):

emailtitle = atom.entries[i].title
# show only first few words if title is too long
if len(emailtitle.split()) > _maxwords:
emailtitle = ' '.join(emailtitle.split()[:_maxwords])

print "^fg(%s) %s from %s" % (_calmcolor, emailtitle, atom.entries[i].author)

if __name__ == "__main__":
feed = auth()
showmail(feed)




This bash script calls the dzenGmailNotify script at specified intervals.


#!/bin/bash

#checkgmail.sh

while true ; do
python dzenGmailNotify.py
sleep 30
done


Finally, make the above executable and add this to .xsession to display in dzen.


./checkgmail.sh | dzen2 -ta r -l 5 -bg '#808080' -x 700 -w 380 &

November 11, 2007

Xmonad on Ubuntu - setting it up

My setup (and what seems to be the most common way to set it up) consists of modifying xmonad 'Config.hs' and configuring dzen, dmenu and the '.xsession' file.

Config.hs
Xmonad does not load settings dynamically from a configuration file. Instead, the settings are compiled into the binary. This means that changes in the settings require xmonad to be recompiled and reloaded. This is not as hard as it sounds, however.
Looking at the Config.hs, you will find it fairly simple and easy to understand. The first thing I did was changes the number of workspaces to 4 with names for them.


workspaces :: [WorkspaceId]
workspaces = ["terminal", "web", "work", "coding"]


The next setting you may want to change is the modMask which determines which will be the modifier key (Alt is the default) - I was content with the default.
The other import change to make is to create a gap for the dzen bar at the top - like this...


defaultGaps :: [(Int,Int,Int,Int)]
defaultGaps = [(18,0,0,0)] -- 15 for default dzen font


In the window rules, you might want to choose the applications that should always display in the floating mode (that is, not tiled). You can also choose here to open some applications only on a specific workspace, browsers in the 'web' workspace, editors in the 'coding' workspace and so on.
Next is the section for changing the keybindings though I will stick to the default keybindings for now.

Compiling
Once the changes are done, you have to compile Xmonad again. If you want to experiment with some settings and will be doing this repeatedly, it is logical to put these lines as a shell script.


#!/bin/bash
#recompile.sh

runhaskell Setup.lhs build
rm `which xmonad`
runhaskell Setup.lhs install --user


Now you just run recompile.sh and then press 'Alt-q' to restart xmonad on the fly. For examples of users config files see http://haskell.org/haskellwiki/Xmonad/Config_archive. If Alt-q doesnt seem to work (didnt for me at first), it is because xmonad is not in your PATH. Add this to your .bashrc -


PATH=$PATH:/path/to/xmonad
export PATH


dmenu
dmenu provides a simple way to launch applications in xmonad. On pressing 'Alt-p' (the default keybinding), dmenu pops up. It allows you to type the name of the application to launch. As you type, the possible candidates are shown and you need not type the whole name if there is only one option left. I installed dmenu from the repository (sudo aptitude install dmenu), but while it worked perfectly in gutsy, the dmenu in Feisty was not showing the completions. I solved this by downloading the source (http://www.suckless.org/download/dmenu-3.4.tar.gz) and then compiling it.

dzen
dzen is a very versatile program used for notification and display of status messages. The best way to get is to compile from source. Once started, it fits into the 'gap' that we created in the xmonad display. Here is dzen in action -



Though this screenshot shows the latest release, you should consider checking out the development version.


svn checkout http://dzen.googlecode.com/svn/trunk dzen


The development version makes it easier to pipe output selectively to the master and slave windows and also has better gadgets (gcpubar instead of cpubar).

xsession
And finally modify the xsession file to add programs you want at startup. My .xsession file currently looks like this


unclutter -idle 1 &
/home/raja/xmonad/genstatusmessage | dzen2 -ta l -fg '#ffffff' -bg '#808080' -x 0 -w 430 &
/home/raja/xmonad/cpubar | dzen2 -fg '#ffffff' -bg '#808080' -x 430 -w 300 &
/home/raja/xmonad/genemailmessage | dzen2 -ta l -l 3 -fg '#ffffff' -bg '#808080' -x 730 -w 350 &
gnome-settings-daemon &
gnome-power-manager &
rxvt -bg black -fg white &
firefox &
xmonad


The first line starts unclutter (available in the Ubuntu repos) which hides the mouse cursor after the set idle time (1 sec). Watch out for problems with loss of keyboard focus that has been occasionally reported with unclutter.
The next three lines start 3 instances of dzen. The first dzen shows the output of my genstatusmessage script shown here


#!/bin/bash
while true ; do
temp=`cat /proc/acpi/thermal_zone/THM0/temperature | awk '{print $2}'`
bt=`acpi -b | awk '{print $5,$6}'`
m=`date +"%H:%M %a %b %d"`
printf "%s | Temp: %s C | Batt: %s \n" "$tm" "$temp" "$bt"
sleep 30
done


The other script genemailmessage checks my gmail and gives the count of new messages as well as the headers of the new messages - but that is for another post!
Starting the gnome-power-manager allows suspend on closing the laptop lid.

October 27, 2007

Installing Xmonad on Ubuntu

Xmonad is the new kid on the block among the tiling window managers. Tiling window managers (some of the other prominent ones being ratpoison, dwm or Ion, arrange windows automatically to tile the screen without overlap or gaps, maximizing screen usage. To those who remember the tiling windows in old Windows and Mac systems, this might seem like a step backward. But in todays world of wide screen monitors and multi-head displays, tiling window managers can be highly productive, not to mention the fact that the mouse is mostly superfluous in most of these window managers. Anyway, it doesnt hurt to try it out and a choice of window managers is one of the fantastic things about Linux. If you just want to try out a tiling window manager, dwm is available in the Ubuntu repos. But if you want to try Xmonad, you have to build it from source. This is what worked for me. Most of the instructions are based on those given at the site, but some modifications are needed for the specific dependencies in Ubuntu and to install the latest version of Xmonad. These are the instructions for installing on Ubuntu Feisty and have not been tested on Gutsy.

1. Install the Haskell compiler and the mtl core library.


sudo aptitude install ghc6
sudo aptitude install libghc6-mtl-dev


2. Download and install the X11 bindings for Haskell


cd /tmp
wget http://hackage.haskell.org/packages/archive/X11/1.2.2/X11-1.2.2.tar.gz
tar -xzf X11-1.2.2.tar.gz
cd X11-1.2.2
runhaskell Setup.hs configure --prefix=$HOME
runhaskell Setup.hs build
runhaskell Setup.hs install --user
cd ..


3. Download and install the X11-extras bindings


wget http://hackage.haskell.org/packages/archive/X11-extras/0.4/X11-extras-0.4.tar.gz
tar -xzf X11-extras-0.4.tar.gz
cd X11-extras-0.4
runhaskell Setup.lhs configure --prefix=$HOME
runhaskell Setup.lhs build
runhaskell Setup.lhs install --user
cd ..


4. Install dmenu and dzen (Optional, provides status bar and menu)


sudo aptitude install dmenu

wget http://gotmor.googlepages.com/dzen2-latest.tar.gz
tar -xzf dzen2-latest.tar.gz
cd dzen2-0.8.5
sudo make clean install
cd gadgets
sudo make clean install


5. Download and build Xmonad


mkdir ~/xmonad
cd ~/xmonad
wget http://hackage.haskell.org/packages/archive/xmonad/0.4/xmonad-0.4.tar.gz
tar xzf xmonad-0.4.tar.gz
cd xmonad-0.4
runhaskell Setup.lhs configure --prefix=$HOME
runhaskell Setup.lhs build
runhaskell Setup.lhs install --user


Note that I have opted to store the xmonad source files in an accessible directory. Dont delete it yet. Any changes in the configuration of Xmonad are made in the source code and then you have to recompile the binary.

The best way to add xmonad to the gdm menu is to create a .xsession file. In the simplest form this will be


#~/.xsession
xmonad


Now when you log in you will have an option titled 'xsession script' and choosing it will launch xmonad. Look at the xmonad site to learn the keyboard shortcuts before you start. We have to configure xmonad to create a space for dzen and write some scripts to pipe some output to the dzen status bar. This and other customizations in the next post.

See my next post for setting up Xmonad.

October 15, 2007

Upgrading to Gutsy - lessons learnt

Having previously had problems when upgrading from Edgy to Feisty, I took extra care this time around. I saved an image of my root and home partitions (with partimage on systemrescue cd) to begin with. I then uninstalled Beryl and commented out unofficial repositories in my sources list. Having done that, I started the upgrade with 'update-manager -d' (needed only till the final release is out). The upgrade went quite well this time and soon I had gutsy running smoothly on my desktop. Few of the lessons I learnt were -

1. Dont know if it is required to uninstall Beryl, but that worked for me.
2. Good idea to make sure you have about 50o-1000 MB free space on your root partition before you start.
3. While it is not required to 'babysit' during the long upgrade process, check on it once in a while. Once, coming back after a couple of hours, I found it waiting for me to declare if I wanted to keep some old configuration files or change them.
4. Virtualbox worked after running the setup once. This made it easy since there is no package for gutsy at the virtualbox site and the repos only have the OSE (which is lacking in some features).

All in all, it was a pretty painless upgrade and if you are, like me, debating if you should upgrade or do a fresh install, it seems worthwhile trying the upgrade if you have the system already set up to your liking.

September 13, 2007

Carry the gibbon in your pocket: How to make a liveUSB of Gutsy Gibbon with persistence

The live CD is a fantastic tool to try out linux and many distros have them now. The newer feature is 'persistence' where changed settings are stored for future sessions. Combining persistence with a live USB allows you to really carry your desktop with you on just a USB stick. These are the steps that I needed to make a live USB with the Tribe 5 release of Ubuntu Gutsy.

This is not intended to be a standalone howto. I am not duplicating all steps and workarounds detailed in the Ubuntu wiki, so I suggest that you have that by the side if you are trying this. Also, I refer to the disk as /dev/sdb because that was what it was for me. I cannot emphasize enough that this may be different for you and blindly copying the commands can end up in formatting another drive

WHAT YOU NEED:
1. An USB drive with capacity of atleast 1 GB.
2. A live cd of Gutsy - The iso will also do.
3. A PC with linux or Windows.
I am doing this with a Maxtor 2 GB drive using a pc running Ubuntu Fiesty and a live cd of Ubuntu Gutsy.


STEP ONE: Partition the disk

You will need to create atleast 2 partitions.
One will be a primary FAT16 partition of 750 MB that is bootable.
Second will be a primary ext2/ext3 partition for the persistence feature.
I used 200 MB for the second partition and created a third partition in the remaining free space as FAT16.
So plug in the USB drive and identify the device name for the drive. It is /dev/sdb for me and it is important to find this correctly so you dont partition or format another drive. The output for 'dmesg |tail', 'df -h' or 'mount' should help identify the correct drive. Opening the gnome partition editor also makes it easy to recognize the device.
The partitioning itself can be done with either gparted, cfdisk or fdisk. The disk must be unmounted before doing the partitioning and all three methods are fairly easy, though someone used to GUIs may be more comfortable with gparted. These are a couple of screenshots with gparted.







Using fdisk and following the tutorial in the wiki also works perfectly (I tried it too). You will not yet be able to assign a label with any of these methods. As seen in the last screenshot, I ended up with the three partitions.
Now the following commands assign labels to these partitions - Ubuntu keeps automounting the partitions, so I had to unmount them before this step.


sudo mkfs.vfat -F 32 -n gutsy /dev/sdb1
sudo mkfs.ext2 -b 4096 -L casper-rw /dev/sdb2
sudo mkfs.vfat -F 32 -n data /dev/sdb3


Remember to change the device name if the name is not /dev/sdb* for you.
So I have the three partitions with the labels now. The second partition has to be labeled 'casper-rw' so that is is recognised by the system as the partition to use for storing changes. You can choose different labels for the other two, if you wish.
If the device is not mounted now, mount it manually or just unplug it and plug it back in to allow it to be automounted.


STEP TWO: Copy necessary files

I used the cd I had already burnt as the source for the files. If you have an iso only, you can mount the iso like this and use it as the source


mkdir /tmp/ubuntu
sudo mount /path/to/iso /tmp/ubuntu -t iso9660 -o loop


Now cd into the source directory (/media/cdrom or /tmp/ubuntu) from a terminal and then do the following to copy the files to the gutsy partition on the disk. I am assuming here that the partition is mounted as /media/gutsy.



#cd into source directory
cd /media/cdrom

#Copy these folders
cp -rf casper disctree dists install pics pool preseed .disk /media/gutsy

#Copy all files from isolinux directory
cp isolinux/* /media/gutsy

#Copy these files into the root destination directory
cp md5sum.txt README.diskdefines ubuntu.ico /media/edgy

#Copy the kernel image and initrd from casper directory and mt86plus from install dir
cp casper/vmlinuz casper/initrd.gz install/mt86plus /media/edgy

#rename isolinux.cfg as syslinux.cfg
mv isolinux.cfg syslinux.cfg


You can modify syslinux.cfg like this. Alternatively you can copy this and paste it in syslinux.cfg replacing everything else.


DEFAULT custom
GFXBOOT bootlogo
APPEND preseed/file=preseed/ltsp.seed boot=casper initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash --
LABEL custom
menu label ^Start Ubuntu in persistent mode
kernel vmlinuz
append preseed/file=preseed/ltsp.seed boot=casper persistent initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash --
LABEL live
menu label ^Start or install Ubuntu
kernel vmlinuz
append preseed/file=preseed/ltsp.seed boot=casper initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash --
LABEL xforcevesa
menu label Start Ubuntu in safe ^graphics mode
kernel vmlinuz
append preseed/file=preseed/ltsp.seed boot=casper xforcevesa initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash --
LABEL check
menu label ^Check CD for defects
kernel vmlinuz
append boot=casper integrity-check initrd=initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash --
LABEL memtest
menu label ^Memory test
kernel mt86plus
append -
LABEL hd
menu label ^Boot from first hard disk
localboot 0x80
append -
DISPLAY isolinux.txt
TIMEOUT 300
PROMPT 1
F1 f1.txt
F2 f2.txt
F3 f3.txt
F4 f4.txt
F5 f5.txt
F6 f6.txt
F7 f7.txt
F8 f8.txt
F9 f9.txt
F0 f10.txt


Note that you have most of the boot options as in the live cd, but the default is the custom option, which is the persistent mode here.


STEP THREE: Make USB drive bootable

We will install syslinux and mtools and use them to make the drive bootable


sudo aptitude install syslinux mtools
sudo umount /dev/sdb1
syslinux /dev/sdb1


Worked perfectly for me.


STEP FOUR: Checking it

Reboot and change bios settings so that you boot from the USB drive. I was able to boot in without problems. I created a username and password (default user is 'ubuntu' without a password). I logged into my new account and changed terminal settings, changed the metacity theme and installed gnumeric. Shutdown, reboot again - and voila - all the changes are there!

I had problem in a later session where I could not log into gnome with gnome complaining that it could not read the .ICEauthority file. Logging into a failsafe terminal and deleting the ~/.ICEauthority file allowed me to start gnome again.
The casper-rw partition is the place where all data and changes are stored without any compression. So if you store any data in your home folder, you can see it here - that means it is is easy to retrieve it without booting in with the drive and also has implications for privacy.

Things to explore:
Find what is the ideal size for the casper-rw partition?
Using an encrypted home or data folder?
Dual boot with another linux distro?
Is it possible to not automount the gutsy and casper-rw partitions by modifying udev rules so that all three partitions dont mount every time I insert the drive in my laptop?

Summary:
The process was much simpler than I had expected. Now I have a bootable Ubuntu drive with me all the time, while the 1 GB FAT partition is enough storage to use as a regular USB stick. Now when I am urging a friend to try out Linux, I know I have the gibbon ready in my pocket!

August 7, 2007

A simple stopclock for kde and gnome

Zenity and Kdialog are two awesome applications to rapidly create simple applications with a gui frontend. Needing a simple way to start a timer for a few seconds to minutes, I chose to write a small script using zenity. Translating this later to work on kde with kdialog was an interesting exercise with some things being more difficult, but there being more flexibility too.

Feel free to try these scripts in the DE of your choice. Typing 'stopclock 10' would start a countdown to 10, for example. Not something designed for practical use, but more as a demonstration of the use of zenity and kdialog. Also this is a good illustration of the use of dcop - another very powerful application that is not very well known or documented.

1. stopclock.sh for KDE


#!/bin/sh

if [ $# -ne 1 ]
then
echo $#
echo "Usage: stopclock (number of seconds)"
exit
fi

stime=$(date +%s) #starting time
etime=0 #elapsed time
stoptime=$1 #time at which to stop
ltime=$stoptime #time left

d=$(kstart --ontop --alldesktops kdialog --progressbar "Starting countdown..." $stoptime)
dcop $d setAutoClose true

while [ "$etime" -lt "$stoptime" ]; do
sleep 1
ctime=$(date +%s) #current time
: $((etime=$ctime-$stime))
: $((ltime-=1))
label=$ltime" seconds left"
dcop $d setLabel "$label"
dcop $d setProgress $etime
done


2. stopclock.sh for Gnome


#!/bin/bash

if [ $# -ne 1 ]
then
echo $#
echo "Usage: stopclock (number of seconds)"
exit
fi

stime=$(date +%s) #starting time
etime=0 #elapsed time
stoptime=$1 #time at which to stop
ftime=0 #fraction of time elapsed
label="counting down "$stoptime" seconds"

while [ "$etime" -lt "$stoptime" ]; do
sleep 1
ctime=$(date +%s) #current time
: $((etime=$ctime-$stime))
ftime=$(echo $etime*100/$stoptime | bc -l)
echo $ftime
done | zenity --progress --text="$label"

June 30, 2007

Archlinux - First Impressions

After having been faithful to Ubuntu for about 3 years, I decided a month back to try another distro on my Thinkpad X 40. Debian or Suse did not seem to be different enough to be of interest. So I decided to try out something that is more 'hands on' than Ubuntu. After trying out a couple of distros on Virtualbox (by the way - another use for virtual machines), I decided to give Archlinux a try.
Now I have Arch running for a week and am actually typing this from Arch - so it is time to give my first impressions. The main things that stand out in comparison to Ubuntu are -

System configuration

The Arch philosophy is to avoid using GUIs for configuration. Most of the configuration is done in /etc/rc.conf. The configuration files are well organized and nicely commented so that just a few days of using them and you begin to wonder why on earth you need a GUI for this.

Lean System

The recommended way to install Arch is to install the base only first and then add the packages you want. It is amazing how lean and fast the system feels when you do this. Ubuntu is very scaleable so that you can again do a server only install and then install the required packages, but I have never installed it that way and dont know if that would give a system as lean and fast as this.
I have installed the packages I want and overall the system is significantly faster than when I use Ubuntu - I think it is the combination of having only required packages and these being i686 optimized. Suspend works beautifully and suspend and resume are much faster too.

Rolling updates

Arch has a system of rolling updates so that the different 'releases' are little more than arbitrary snapshots. Each day is a new release! I did the base install from an older release, but it is a breeze from there to update the base packages and then proceed. The rolling release means that the packages are often newer than those in Ubuntu (where I have had problems with older versions for quite a few apps).

Package Manager

Pacman is an awesome package manager. While aptitude (or apt-get) has been good in Ubuntu, I think pacman is faster and more advanced. In Ubuntu, there is also some confusion with many ways of installing packages (Synaptic, Add/Remove, apt-get, aptitude, etc). Having one way to do it is simpler.

Summary

Archlinux is a splendid choice for an intermediate to advanced linux user who wants to try a leaner, faster and more flexible system. It is more demanding and takes more time to install than Ubuntu, but it is not as bad as Gentoo while still resulting in a very fast system and I think the extra time and effort is well spent.
Looking at Ubuntu a little critically, I think it has become a gateway for newcomers emigrating from Windows. So most of the development seems to be going into easier installs, newbie-friendly single click configuration, GUIs for everything, etc. Arch, with almost opposite philosophy has been a refreshing change.

June 2, 2007

10 tips to customise you new Ubuntu install

One of the best things about linux is that you can customise every aspect of it so that your computer really feel like yours. Here are a list of 10 things I do with a fresh install. These only reflect my preferences, but they should serve to let a newcomer know what tweaks are possible.
The first three are related to the terminal - so those resolved not to touch the terminal can safely skip them.

1. Terminal - looks

(i) Open the terminal, go to Edit -> Profiles and make a new profile with your name. In the 'General' tab, change the font and the font size to something you like.
(ii) In the 'Title and Command' tab, you can choose to keep a fixed title (maybe just your name or a favorite quote) or if you enable dynamically-set title, you can have the title show dynamic information like the current directory.
(iii) In the next tab 'Colors' you can choose the background and foreground colours for your terminal and then you can set some degree of transparency in the next tab if you like it.
(iv) Finally, in the scrolling tab, you can disable the scrollbar if you prefer. In addition, if you right click in the terminal and uncheck 'Show Menubar', you have a pristine looking terminal without a distracting menubar or scrollbar and in colours and fonts that you like !

2. Terminal - prompt

The default prompt in ubuntu is usually in the form 'user@hostname:directory'. No worries, you can change it to your heart's desire. Again open ~/.bashrc. 'PS1' represents you command prompt and is defined in this file. Below the line defining your default prompt, you will find a commented out section defining a color prompt. You can uncomment this to get a prompt which is of a different color from the rest of the text. In addition, you can change the format of the font. Just remember that '\u' stands for username, '\h' for hostname and '\w' for the present working directory.

3. Terminal - aliases

Setting up aliases for commands that you use commonly makes life a lot easier. You can define these in the .bashrc file again. Towards the end of the file you will notice that there are some aliases already defined and commented out - you can use them if you want to. In addition you can define your own aliases. My favorite is to set up an alias for installing programs so that I dont have to type 'sudo aptitude install' or 'sudo apt-get install' everytime. So at the end of my bashrc I have these lines -

#My aliases
alias getme='sudo aptitude install $1'
alias batt='acpi -V | grep Battery'

Now I can just type 'getme abiword', for example, when I want to instal abiword ! The other alias allows me to check the battery status on my laptop from the terminal by just typing 'batt'. Very useful when I have all the panels hidden and therefore cannot see the battery status applet.

4. Desktop - themes

Changing the wallpaper and windows themes to suit your taste is of course the most common way of customizing the looks. http://www.gnome-look.org/ is an excellent resource for wallpapers and themes if you want to go beyond the choices in the default install. A good idea is to choose the wallpaper first and then select a theme that matches the wallpaper in color and look. The themes are usually downloaded as 'xyz.tar.gz'. Dont uncompress them - just go to System -> Preferences -> Theme and drag the file you downloaded to install it.

5. Desktop - panels

Once you have the wallpapers and the theme set up, you can turn to the panels. You can add more panels or remove existing panels according to your taste. If you have a small screen, autohiding panels is useful to increase your screen estate. However, when you right click on a panel and choose 'autohide', you will find that a bit of the panel still sticks out. This is because the autohide size is 6 pixels by default. If you dont like it, open gconf-editor and then go to '/apps/panel/toplevels/top_panel_screen0' and change 'auto_hide_size' to 0. Now, the panels stay well hidden and you have the full screen.
However, to see something on the panel now, you have to hover over the edge with the mouse. A cool trick is to bind toggling the autohide to a keypress, so that you can hide and show the panels by just pressing a key. See this post to learn how to do this.

6. Custom keymaps

Extra keys that you dont need or that dont function can be mapped to suit your needs. On my thinkpad, for example, this allows me to use the 'Access IBM' key and the forward and backward keys.
So let us say you want to map a key to run the hide panel toggle script. We first need to find the keycode for this key. Open a terminal and type 'xev' and press enter. A small window opens now. Press the key and look at the output in the terminal. Among the output, you will find something like 'keycode 44'. We can now map this key as F13 for instance. To do this create a file called .Xmodmap in your home directory. Type 'keycode 44 = F13' in this file. The exact keycode will vary depending on the key you are mapping, of course. Save the file and reboot for the key to become active. Now you can map any command to this key.

7. Grub

(i) Open /boot/grub/menu.lst. There are a couple of things you can modify according to your taste. Look for the entry 'default'. This determines the default choice that will be booted. 0 means the first entry is default, you can count down the entries to select another to make the default.
(ii) If you rarely use the other options (like me), you can uncomment the line 'hiddenmenu'. This means that you wont see the menu during boot up normally, but you can press the Esc key to bring it up when you want it.
(iii) The entry 'howmany' determines how many kernels you can see in the menu. You can change it from default all to something like 'howmany=3' if you dont want all the kernels to accumulate in you menu.lst.
(iv) Finally, those who miss the text during the boot up, remove 'quiet' from the kernel line - now you will see the bootup steps being displayed while the system starts.

8. Boot up - splash

If you want to change the image that comes at the splash screen, you can look at http://www.gnome-look.org/ for some alternatives. Once you choose an image you like, gnome-splashscreen-manager is a nice frontend to modify the grub splashscreen. Just install this with aptitude or synaptic.

9. GDM

Similarly, you can change the gdm image that comes up when you log in. Again choose alternatives you like. You can run 'gdm-setup' to change the image.

10. Firefox - themes and add-ons

Finally, to get to the magic number of 10 - install a theme and add-ons for firefox. Again, you can choose the theme to match the system theme and the wallpaper. The add-ons that I find most useful are 'Download statusbar', 'Faviconize tab', 'Image zoom', 'Noscript' and 'tabmixplus'.

April 15, 2007

How to install Virtualbox in Ubuntu Feisty

04 October 2008
This post is considerably outdated. Virtualbox 2.0 and Ubuntu Intrepid (8.10) have been released. So while some of the steps might still be valid, it would be wiser to refer to the current documentation.


Virtualbox is a new opensource virtualization solution made by Innotek. It has all the features that vmware offers and is a smaller download and easier to install. It therefore appears poised to replace vmware as the main virtualization software in Ubuntu. This is a brief howto for installing Virtualbox on Ubuntu Feisty. It is based on two installations I did recently. By providing the relevant instructions on one page, I hope to make it easier for others to follow. Any feedback about problems or suggestions to improve the tutorial are welcome. Here we go.

First install the dependencies that are not present in Feisty by default.

sudo aptitude install libqt3-mt libxalan110 libxerces27

Now download the debian package for edgy and install it.

*UPDATE: A debian package is now available for Feisty. Replace 'edgy' with 'feisty' in the next two lines to use it.

cd /tmp
wget http://www.virtualbox.org/download/1.3.8/VirtualBox_1.3.8_Ubuntu_edgy_i386.deb
sudo dpkg -i VirtualBox_1.3.8_Ubuntu_edgy_i386.deb

You will have to accept the PUEL license and select "yes" when asked if the vboxdrv kernel module should be compiled. The kernel should be created successfully now. A group called vboxusers is created and you have to add yourself as a user of this group.

sudo adduser $USER vboxusers

If you want to access usb devices from the virtual machine, follow these steps. You can omit them and directly reboot if you are not going to be using usb devices from the guest.

sudo addgroup usbfs
sudo adduser $USER usbfs

** For the next step you have to know the id for the usbfs group that you have created. To do this try

cat /etc/group | grep usbfs

and look for the number after "usbfs:x:". In the next command, replace 1002 in the next command with this number**
echo "none /proc/bus/usb usbfs devgid=1002,devmode=664 0 0" | sudo tee -a /etc/fstab

Now reboot to allow the new group and user permissions to be updated.

start VirtualBox

VirtualBox &

Once you create a virtual machine and install an OS on it, you may want to set up a shared folder between the host and the guest. Suppose the name of the virtual machine is "winXP" and the folder you wish to share is at /home/user/shared. Power off the virtual machine and then do this :

VBoxManage sharedfolder add winXP -name "sharedfolder" -hostpath "/home/user/shared"

Now you can mount the shared folder within the guest. For a windows guest you would boot up and then in the terminal type

net use G: \\vboxsvr\sharedfolder

Now you should be able to access the shared folder from within the guest and the host systems. To access a USB device from the guest, select the device in the USB controller in VirtualBox and enable it. Remember that the device may not be accessible to the guest if it is mounted in the host.


Edit (21 April 2007) - Corrected command for appending line to fstab. Thanks to ebash from Ubuntu forums for pointing out the right way to do it.


Powered by ScribeFire.

April 9, 2007

Hide and show panels with a keyboard shortcut in Ubuntu


I usually keep the default top and bottom panels in gnome, but especially on my laptop, place great value on the screen estate that I can get by hiding them. The usual way I do this is by using autohide and then setting the hidden size to 0 or 1. Mostly I dont need the panels because I can launch applications with Alt-F2 or with shortcuts. But when I need to access something from the panel, I have to mouse over the hidden panel to bring it up. The other minor irritation is that the panel may spring out when you dont really want it if your mouse wanders close to it.

In a recent post in the Ubuntu forums, it was suggested that it would be nice to set up a keyboard shortcut to show the panels when needed. Since it is easy to access the gconf-editor from the command line, it was easy to write a script to toggle the hide status of the panel. Here is a short how-to if someone is interested.

Copy this script, paste it a file and save it as toggle.sh. A good way to place it will be /home/username/.toggle.sh.

#!/bin/bash

#find the current state of the panels
state=`gconftool-2 --get "/apps/panel/toplevels/top_panel_screen0/auto_hide"`

#if autohide on, turn it off
if [ $state = "true" ]; then
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/unhide_delay" --type integer "0"
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/auto_hide" --type bool "false"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/unhide_delay" --type integer "0"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/auto_hide" --type bool "false"
fi

#if autohide off, turn it on
if [ $state = "false" ]; then
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/unhide_delay" --type integer "100000"
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/auto_hide" --type bool "true"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/unhide_delay" --type integer "100000"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/auto_hide" --type bool "true"
fi


Make the script executable:

chmod +x ~/.toggle.sh


Open gconf-editor and under /apps/metacity/keybinding_commands, change the value of command_1 (or any other unused command) to /home/username/.toggle.sh.

Then under /apps/metacity/global_keybindings, change the value of run_command_1 (if you used command_1 for the script) to controlF12 or any other key combination that you want. There, you are done! Now pressing the key combination will toggle the panels to hide or show. I have tested this with metacity and beryl on gnome and it works perfectly.

Edit: 10 April 2007 - Thanks to dzv (in the Ubuntu forums) for comments and the suggestion to change the unhide delay so that the panel stays hidden.





Powered by ScribeFire.

April 6, 2007

An interactive Linux command line tutorial

Edit: A new version of cmd-tutor is now available. See here.


There has been a gradually increasing influx into the linux community in recent years. The release of Vista has encouraged a lot of people to take the plunge and switch to linux. And while it is not any more essential to use the command line to manage your linux desktop today, many newcomers are interested in knowing what it is all about.

A lot of excellent tutorials are available, like this for example, but there were some requests for an interactive tutorial. Those who have tried the online ruby tutorial will know what I am talking about.

So, I wrote this tutorial in python. Keeping in line with Unix philosophy, the mechanism for presenting the lessons is kept separate from the lessons itself. Therefore it is easy to extend it by adding more lessons later. Currently there are 5 lessons that I have written. These would serve as a gentle introduction to a newcomer.

You have to be running linux to use this. You can download cmd_tutor here. The instructions to install are in the package. Briefly, you have to extract the package somewhere, navigate to the src directory from the terminal and then type 'python install.py install' with superuser privileges. Once installed, you can type 'cmd_tutor' in the terminal to start the tutor.

Any comments and suggestions will be welcome.

March 17, 2007

Tagging pdf documents with python

Tagging as a method of organizing and searching is commonly used for music files, pictures and favourite websites. For documents, the traditional method of searching has been based on indexing the content. All the modern desktop searches will index your pdf files. But using tags obviously has its advantages and people would go to great pains to manually add tags to each file as metadata or for organizing with itunes !

My personal itch is the need to search my collection of journal articles saved as pdf files. The problem with full text indexing of these files is that there is a long reference list at the end of these articles which misleads any attempt to search for a particular author, journal or title. I have struggled with this for some time and looked at applying tags to each file as a solution. The tags could be added as extended attributes in a linux file system like this or as metadata in a windows environment and used for searching. Even more useful might be applications that allow tagging and searching by tags. Tracker and leaftag come to mind for this purpose in Linux. The main hurdle, however, is that adding these tags manually is too tedious. So I experimented with ways to get the information for each pdf file from pubmed. The Biopython module provides a simple interface to the pubmed database from Python. So all one has to do is convert the pdf to text and then parse the text for some information that will allow correct identification at pubmed. The first step is relatively easy. Xpdf provides tools for pdf to text conversion, though I preferred to use beagle's data extraction tool since I already had beagle installed.

import commands
convertcommand = "beagle-extract-content \"" +pdffile +"\""
pdftext = commands.getoutput(convertcommand)


The second part is more difficult because there is no consistent formatting of contents between different publishers. The best way to do it turned out be using the doi. The doi or Digital Object Identifier is a unique name given to any digital object and is usually included in the publication. Parsing it was a matter of searching for
'DOI:' or 'doi:' in the text.

doi = pdftext.lower().split('doi:')[1].strip().split(' ')[0]
searchstring = doi +"[AID]"


The searchstring is constructed by adding the tag [AID] for Article Identifier. Searching in pubmed with this string with this string turns up the pubmed ID (pid) for the article. This allows retrieval of the formatted article information including
title, authors, journal name, year of publication, volume and page numbers.

from Bio import PubMed
from Bio import Medline

rec_parser = Medline.RecordParser()
medline_dict = PubMed.Dictionary(parser = rec_parser)

pmid = PubMed.search_for(searchstring)[0]

record = medline_dict[pmid]


print "title is ", record.title

print "author is ", record.authors
print "source = ", record.source


Some journals, however, still do not provide the doi in the publication. So we need something to fall back on. I chose to use the article information which is usually published on the first page in a standard format like this - yr;volume:first page - last page (example 2007;41:272-275). There may sometimes be a space after the
semicolon or the colon, so the search with regex looks like this.

import re
texttosearch = pdftext[:6000]
pattern = "[0-9]{4,};[ ]*[0-9]+:[ ]*[0-9]+"
m = re.search(pattern,texttosearch)
info = m.group(0)
yr = info.split(';')[0]
(vol,pg) = info.split(';')[1].split(':')
searchstring = vol +"[volume] AND "+ pg +"[page] AND "+ yr +"[pdat] + "+
"English[lang]"

For testing, I chose 10 journals comprising the prominent publications in medicine and cardiology and randomly picked an article each from Sep 2005 and Feb-Mar 2007. Correct information was obtained for all the 20 articles. Here is part of the output.
In conclusion, this seems a promising approach to automatically obtain information for each pdf file in my library. This information could be added to the file as extended attributes or used as tags for the file. Like Ruby, Python also has an xattr library and adding them automatically would be easy. The automatic retrieval will fail for some files, but the information could be added manually in such cases.


March 4, 2007

The User Interface of the future ?

Arguing whether the Command Line Interface (CLI) is superior to the Graphical User Interface (GUI) or vice versa strikes me as a futile exercise. It quickly becomes clear to anyone who has used both that the best way to interact with the computer is to use both methods. A CLI + GUI interface is vastly more powerful than a plain GUI interface. As you use both, you find more and more uses for the former, where it clearly surpasses the GUI. Why, you even start ordering your pizzas from the command line.

However, the difficulty many face in approaching the CLI initially is what Eric Raymond calls the 'mnemonic load'. Don Norman writes how the next major UI breakthrough should be in CLI. He anticipates the development of a more flexible command line language, with more resemblance to natural language and not requiring a strict adherence to an idiosyncratic syntax.

An alternative to an entire new language for the command line is to use a user-friendly intermediate layer which translates the user input into the syntax that the command line understands. This is analogous to the frequently used concept in Unix when a user friendly GUI actually uses command line tools, but provides a user friendly front end.

What you see below is a working prototype of such a program written in python. In its main loop, it collects user input and outputs a command to the terminal. When the input is a valid command line input, it is passed unchanged. But when it is not, it is 'translated' into a valid one. I call it the Genie. Here are a few examples of the genie in action.


The prompt includes a battery status monitor - I find it useful and it shows that the prompt is easily customizable. 'Normal' shell commands are interpreted directly - like 'ls -l | grep 2007-03' in the example. Navigation to usual places is easy just - 'go home' or 'go desk'. 'space' functions like an alias mapping to 'df -h / /home'. Note the genie says line which lets you know the command that is passed to the shell. So that a newcomer also learns shell syntax along the way.


When you want to install an application just 'install '. If the app is found in the apt-cache, installation is started. Otherwise you are allowed to choose from the matches in the apt-cache.


Any calculations are automatically recognized and passed on to bc. Finally, a listing of directory names is stored and searchable. So if you want to navigate to the python site-packages folder and didn't remember where it was, genie can help you.

Obviously, the possibilities are almost endless. The genie can be taught to understand new commands as you desire. It may be necessary to be able to carry genie around so that you have your custom genie on any computer you have to use. But finally, will the added ease of use facilitate introduction of the command line to new users or will the use of a simple interface like this preclude users from learning shell commands and thereby never being able to make full use of it ? Comments are welcome.

February 24, 2007

Hide and show panels with a keyboard shortcut in Ubuntu

I usually keep the default top and bottom panels in gnome, but especially on my laptop, place great value on the screen estate that I can get by hiding them. The usual way I do this is by using autohide and then setting the hidden size to 0 or 1. Mostly I dont need the panels because I can launch applications with Alt-F2 or with shortcuts. But when I need to access something from the panel, I have to mouse over the hidden panel to bring it up. The other minor irritation is that the panel may spring out when you dont really want it if your mouse wanders close to it.



In a recent post in the Ubuntu forums, it was suggested that it would be nice to set up a keyboard shortcut to show the panels when needed. Since it is easy to access the gconf-editor from the command line, it was easy to write a script to toggle the hide status of the panel. Here is a short how-to if someone is interested.



Copy this script and save it as "toggle.sh". A good location to keep it would be /home/&lt;username&gt;/.toggle.sh.&nbsp; Make the file executable :



chmod&nbsp; +x&nbsp; ~/.toggle.sh



#!/bin/bash



#find the current state of the panels

state=`gconftool-2 --get "/apps/panel/toplevels/top_panel_screen0/auto_hide"`



#if autohide on, turn it off

if [ $state = "true" ]; then

&nbsp;&nbsp;&nbsp; gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/auto_hide" --type bool "false"

&nbsp;&nbsp;&nbsp; gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/auto_hide" --type bool "false"

fi



#if autohide off, turn it on

if [ $state = "false" ]; then

&nbsp;&nbsp;&nbsp; gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/auto_hide" --type bool "true"

&nbsp;&nbsp;&nbsp; gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/auto_hide" --type bool "false"

fi



Open gconf-editor now, and in /apps/metacity/keybinding_commands, change the value of command_1 (or any other command which is unused) to /home/&lt;username&gt;/.toggle.sh.

&nbsp;

Then go to /apps/metacity/global_keybindings and change the value of run_command_1 (if you mapped the script to command_1) to &lt;Control&gt;F12 or any other key combination you choose. Close gconf-editor and try it out ! I tested this with both metacity and beryl and it works perfectly.

February 17, 2007

Keeping up with literature is now easy. Just read the feed !

Everyone in medicine knows very well the difficulty of keeping up with current literature. More than 6 million articles are published each year and even the fraction of these that an individual physician has to read can be overwhelming. Of course, today I don't need to go down to the library to scan through the recently published articles. But even looking at each journal's website for the abstract of the latest articles can be a daunting task. In this article I will show you how I use RSS today to keep up with the journals I am interested in.

Step 1: Pick an RSS reader to use. While I use Google reader and therefore use it in my examples, there are a wealth of RSS readers for you to choose from.

Step 2: Subscribe to RSS feeds from the website for journals that offer RSS feeds. For example this shows the RSS link from Heart Online. All you have to do is copy the link and add it as a subscription in google reader.



Step 3: For the many journals that still do not provide RSS, you can set up an RSS fees of their contents from pubmed. Set up a search using limits set to the journal of your interest. Then select 'send to RSS' from the dropdown menu to get the link.

Now Google reader (or any other reader you choose to use) becomes a central location for you to review the titles and abstracts of the latest publications in the journals you have chosen. Note how I have organize the journals under one folder in Google reader. I also mark items I want to read in detail later with a star. Using Google reader and RSS has given me an immense advantage in keeping abreast of the latest literature in my field of interest. Try it out and let me know how you find it!

More Info:
Creating RSS for a feedless journal
RSS from pubmed search