Console Junkie: Conky Makes Your Desktop Awesome!  

Posted by Vedang
What is conky?

Conky is a light-weight system monitor, which can display any information you want on the desktop. You can get it here, or you can simply install it with:

sudo apt-get install conky

If you want my conky setup though, you should compile it from source. I've explained why further in the post, so read through the whole thing before you go setting up your own conky. Here's what my desktop looks like:



As you can see, I show music stats on the right hand side. I use mpd for music. Now, conky has in-built mpd support, which means using these conky variables for mpd make it faster and lighter on resources. However, these variables are disabled by default in the version available in Ubuntu repos. You won't be able to use them if you do a sudo apt-get install conky. Hence the compile-from-source bit. If you are not using mpd for music, you might as well do a sudo apt-get install. If you are compiling from source, this is an excellent guide.

So that's done. Next you will need to download my conky config files and other scripts required for the setup. You can download them from here. Extract them and rename the folder as scripts. I keep all my scripts at /home/vedang/Source/scripts/, and this path is harcoded into the scripts everywhere. Please search for the string and change it appropriately.

I invoke two instances of conky (one for the left hand side and one for the right), and this is done in the startconky script. So you can get the conky setup started by typing ./startconky in the terminal. The hddmonit script is used to get the hard-disk temperature. In the ctwitter-dummy.py script, the username and password of your twitter account is required. Fill this in, and then rename the file to ctwitter.py

Now, you need to put conky in startup applications for it to start automatically with the system. Make an entry in System->Preferences->Startup Applications. For the command, give the path to the runconky script. This script does nothing but sleep for 15 secs and call startconky. This is because compiz needs to load before conky in order for conky to work correctly.

Finally, no conky script can be written from scratch without any help from the net. Here are the people I would like to acknowledge:

TheSeanKelly: The excellent fonts are something I discovered from his setup. Also, the temperature monitoring script is verbatim from his files.

Travist120: The python script for displaying twitter updates on conky is his.

And that's it. We're done.

Giving the HOME directory it's own partition  

Posted by Vedang
The more you use linux, the more you begin to realise that your HOME directory should be on a different partition from your ROOT directory. This way, your config files remain unaffected by system crashes, OS reinstalls etc. Now _if_ you started out without a seperate partition for your home directory, here's how you can rectify the problem.

Basically, we have to do three tasks.
1) Choose a partition for your HOME directory
2) Copy ALL files from your current home to this new partition
3) Make an entry in fstab to mount this partition as /home on bootup.

We'll be using a tool called GParted to make our life easy.

sudo apt-get install gparted

Yes, it's a GUI tool and yes, I also know how to do the same things in a more complicated way using pure CLI. However, this is a beginner guide. So I'll leave that part as an exercise for the interested reader.

Right! So lets get started:

1) Choose a partition for your HOME directory

a) Run GParted. You will have to use sudo, as you need root privilegdes to use gparted. gparted messes with your partitions, so don't fool around unless you know what you are doing.

b) Select the partition. It obviously has to have enough free space to hold all the files in your current home. You may or may not format it, though I recommend you format it with ext4 as the filesystem.

c) Note the UUID of the partition. You can do this by selecting the partition, clicking on the Partition tab in the main menu, and clicking on Information. Also note the device name of the partition (something like /dev/sda3)

2) Copy ALL files from your current home to this new partition

For this part of the tutorial, CLI is still the best way to go.

We have to make sure we've copied everything from the /home directory. find and cpio commands will do this for us. After that we rename the home to home_backup (just to have a safety net if stuff goes wrong). Lets not forget to remake a home dir. This is how it's done:

mkdir /media/tempdir
sudo mount /dev/sda3 /media/tempdir
cd /home
find . -depth -print0 | cpio --null --sparse -pvd /media/tempdir
mv /home /home_backup
mkdir /home

Remember to replace /dev/sda3 with the device name you noted down earlier.

3) Make an entry in fstab to mount this partition as /home on bootup.

Open the file /etc/fstab in your favorite editor. You'll need sudo privilegdes to edit it, so remember to open it accordingly. Add an entry to it as follows:

UUID=UUID_you_noted_earlier /home ext4 relatime,errors=remount-ro 0 1

Remember to change ext4 with the filesystem of _your_ partition.

That's it. Reboot and we are done. If you don't want to immediately reboot you can manually mount your partition. sudo mount /dev/sda3 /home

Console Junkie: Installing MPD and grooving away to glory!  

Posted by Vedang
What is MPD?

Quoting from this article:
Music Player Daemon (MPD) allows remote access for playing music (MP3, Ogg Vorbis, FLAC, AAC, Mod, and wave files) and managing playlists. MPD is designed for integrating a computer into a stereo system that provides control for music playback over a local network. It is also makes a great desktop music player, especially if you are a console junkie, like frontend options, or restart X often.

In this post, I'll be explaining how to get MPD up and running on Ubuntu (9.04). If you are working on some other platform, you can simply compile from source wherever applicable. Also, this post is mainly about using mpd as a desktop music player. The configuration part of it is common across distributions. So lets get started!

First, we need to install mpd.

sudo apt-get install mpd

Let us now configure it and get it up and running. You can either directly configure mpd for system-wide usage (/etc/mpd.conf) or you can configure it on a per user basis (~/.mpdconf).

sudo cp /etc/mpd.conf ~/.mpdconf

Before we edit our .mpdconf file, we need to create a few directories.

mkdir -p ~/.mpd/playlists

Now, re-define the following parameters to your .mpdconf file. (These will already be present in your file, just change the value in the quotes to shown values. I'll explain the meaning of the options further on.)

music_directory "~/Music"
playlist_directory "~/.mpd/playlists"
db_file "~/.mpd/tag_cache"
log_file "~/.mpd/mpd.log"
error_file "~/.mpd/errors.log"
pid_file "~/.mpd/pid"
port "6600"

Now, music_directory is your music directory; and you probably don't have all of your music in one directory. What we need to do is add symlinks to all the places where you do have your music files.

cd ~/Music
ln -s "path_to_music_1"
ln -s "path_to_music_2"

pid_file stores the pid of the running daemon. This is useful if you want to kill mpd, you only have to say mpd --kill. The db_file maintains your music database. The rest, I believe, is self-explanatory.

Okay, so we're all set up now. Start mpd by typing mpd in the terminal.

mpd

mpd is now running. We will now build a database of our music for mpd to work with.

mpd --create-db

This will go through your music_directory and create your db_file.

Now all we need is a client to connect to the daemon. One of the more popular clients for mpd is gmpc, which is fancy to look at and comes with fancy plugins like last-fm scrobbler, lyricwiki, album covers from amazon etc. A good gmpc installation guide can be found here. However, I'm more of a console person, and I prefer to use ncmpc. This can be installed as follows:

sudo apt-get install ncmpc

I keep both gmpc as well as ncmpc on my machine, because sometimes I like to use gmpc for album covers, lyrics and jamendo/last.fm. Mostly though, I use ncmpc. ncmpc is unbelievably powerful. It is also so simple to use that I believe you will be able to figure it out all on your own. Just type ncmpc in the terminal!

Happy grooving!

Emacs Tip of the Day # Collapsing Functions #  

Posted by Vedang
So, you've opened the CPP file you are working on. For the moment, you just want to see all the functions that have been defined, to get an idea of the functionality provided. But your file is 102KB. If only there were some way you could collapse all the code!

*blaring trumpet sounds* EMACS to the rescue!

Simply fire up a mode called outline-minor-mode. M-x outline-minor-mode

Now, to collapse the code, hit M-x hide-other. Voila!

When you find the function you were looking for, you might want to read the code for it. Hit M-x show-subtree.

Finally, to expand all of the code back again, hit M-x show-all

That is all folks!

P.S.
This blog is increasingly getting to be an Emacs blog! This is what happens when a geek is separated from his beloved Unix environment and has only Emacs as consolation. Since I code in C/C++ at the workplace, I'm now planning to write a bit about Object Oriented Programming. Or some such things :)

An Emacs Diff (Ediff) tutorial  

Posted by Vedang
Since Jitesh is currently writing an extremely interesting series of posts on diff and patch (1,2), I decided to pitch in with my contribution. Ediff is one of those things which make Emacs so much more than just an editor. It is an extremely convenient way of viewing differences in files / buffers / directories.. You get the point.

Following the teaching tradition I prefer, lets get our hands down and dirty, and figure out the theory along the way:

Step 1:
Write two test files for our purposes.



Step 2:
Fire up ediff mode for our two files.

M-x ediff-files

The mini-buffer will ask you to enter the two files to compare (A and B). The order in which we enter the files does not matter, but tradition dictates that file A should be the original and file B should be the modified file.


Can you see the small blue window at the top? That is the ediff floating window. All ediff commands are processed (for lack of a better word) by this window. This window can be expanded to offer the quick help for ediff, which I believe is self-explanatory.


To point out the simple stuff, 'v/V' will scroll the two buffers (simultaneously) up and down, so you can read the orig and changed code. 'n' will take you directly to the next diff point, and the highlighting is super-cool, as you can see. So on and so forth, go read the quick help.


Step 3:
If you want to compare to directories of code, you use the command

M-x ediff-directories

In this case, all files / directories inside your directories are bunched into sessions.A powerful (and easy-to-understand) UI is provided for manipulating sessions. In this way, you can go through the differences file by file, disposing the session once you are done with it.

Points to note:

1) You can apply differences from one buffer to another region by region, i.e selectively apply a patch

2) You can merge two files into a third buffer.

3) Ediff understands version control systems, so you can compare two versions of the same file

4) Ediff indicates exact word differences. If the only differences in two regions are whitespace or line breaks, ediff will tell you so.

5) All of the above can be carried out on three files / buffers / directories! I'm not even going here till I get a complete hang of this!

So, that's it folks. This is without doubt the easiest way of browsing code diffs that I know. Hope you find ediff as useful as I found it!

Note: This post is based on a one day study of Ediff, something I did because I was interested in the feature for a long time. This means, as always, I have barely scratched the surface. I'll post more stuff if I come across something super-cool

Further InformationThe Ediff Manual

Quick-Post: Coding Style  

Posted by Vedang
Here is a document specifying Google's C++ Style Guide: Google C++ Style Guide.
I have not read it yet, but it promises to be good. I'm hoping it will help readers develop a uniform coding style.

P.S : Linux kernel's document on coding guidelines can be found in the kernel code at Documentation/CodingStyle.

Emacs Tip of the Day # Are you a fan of Tab Completion? #  

Posted by Vedang
Tab Completion is a great, great feature. It saves me a lot of time and effort, in bash as well as in the Emacs minibuffer.
I don't know if you are aware of this, but Emacs also comes with a built-in feature called dabbrev-expand (M-/ in the minibuffer). Here is what it does. If you type out the first few letters of your word and hit the key-combo, it auto-completes the word after searching the current buffer and the other open buffers for completion. You can cycle through all possible completions by repeatedly reinvoking the function. This is very, very cool. The only problem is that M-/ is not conducive to speed.
So can we do something super cool like re-bind the function to the TAB key? Without destroying other tab related functions like indentation? Turns out we can!

(global-set-key (kbd "TAB") 'smart-tab)
(defun smart-tab ()
"This smart tab is minibuffer compliant: it acts as usual in
the minibuffer. Else, if mark is active, indents region. Else if
point is at the end of a symbol, expands it. Else indents the
current line."
(interactive)
(if (minibufferp)
(unless (minibuffer-complete)
(dabbrev-expand nil))
(if mark-active
(indent-region (region-beginning)
(region-end))
(if (looking-at "\\_>")
(dabbrev-expand nil)
(indent-for-tab-command)))))

Add this to your .emacs file, and start using TAB in everything you type. You will find that your typing speed triples!

[hat-tip]THE EMACS WIKI

From Jitesh's Blog: One Build to Rule Them All  

Posted by Vedang
Yes, I'm a content thief. The original article is here.

LOTR fan weekly status report: Worked on "one build to rule them all" infrastructure

Reply from the manager:

Three Builds for the Linux-kings in the MSI,
Seven for the SQA-lords in their shield-halls of stone,
Nine for MIPL Mortal Men doomed to code,
One for the Dark Lord Kedar on his Bamboo throne

In the Land of Linux where the RPMs lie.
One Build to rule them all, One Build to find them,
One Build to bring them all and in the flash-ness bind them
In the Land of Linux where the RPMs lie.