Archive for the ‘Geek Stuff’ Category

A quick and powerful Emacs tip: Repeat last complex command

August 23, 2010

Emacs never fails to surprise me. I think I’ve discovered this much later than I should have, and pretty much by fluke, but here goes:

C-x ESC ESC runs the function repeat-complex-command which will let you “edit and re-evaluate last complex command”. A complex command is one which uses the mini-buffer. Using this, you can easily re-execute commands when you want to without having to type the whole thing out again. The command to be executed is shown to you in elisp form, so you can even modify it if you need to. What if I don’t want to execute the last complex command? What if I want to execute a command I have used a bit earlier? Well, you can traverse the mini-buffer history by using M-n and M-p! Super-cool!

Emacs Tip of the Day: VC – The Emacs Version Control Interface

April 14, 2010

VC is the Emacs Version Control Interface that can be used to interact with a variety of Version Control Systems. What this means is that regardless of which version control system you use, you have a uniform command set that you can use to interact with it. So even if you move to a new version control system, you are still ahead on the learning curve. This, of course, is a corner case and not the real reason you should use VC. The real reason you should use VC is that it allows you to use your version control through your editor itself, making it a part of your workflow and therefore effortless.

The way I work is as follows: Once I’ve made my changes I review the diff to see if it is satisfactory (C-x v =). Next, I use the magical C-x v v (Perform the appropriate next version control operation) which ‘guesses’ what I want to do and prepares the commit. Preparing the commit means asking for the commit message and doing the actual commit. And Lo! I’m done.

Sometimes I’ve made a lot of changes in multiple files which I want to check-in together in a single commit. In this case, I run vc-dir (invoked by C-x v d) which gives me a buffer with version controlled file information. From here, I can mark the files I want to commit and run C-x v v to ‘do the right thing’. Simple and straight forward, as always.

Emacs Tip of the Day: Start Using IBuffer ASAP.

March 25, 2010

IBuffer is a drastic improvement on the current emacs buffer management system. It’s main strength is that it allows you to operate on a group of buffers in one go, and it provides an unbelievably large array of keyboard shortcuts to perform said group operations. Using IBuffer is simple, just stick this in your .emacs file:

(require 'ibuffer)
(global-set-key (kbd "C-x C-b") 'ibuffer-other-window)

I like to see my buffers sorted by major-mode, so I add this bit too:

(setq ibuffer-default-sorting-mode 'major-mode)

To mark a set of buffers for group operations, press ‘m’. To unmark, press ‘u’.
Now that we know the basics, let’s look at some of the awesomeness at our disposal:
(Listed in decreasing order of frequency of use)

'O' - ibuffer-do-occur
– Do an occur on the selected buffers.
This does a regex search on all the selected buffers and displays the result in an *occur* window. It is unbelievably useful when browsing through code. It becomes truly awesome when you combine it with the ‘filter’ powers of ibuffer (coming up ahead). Eg: Do C-x C-b, mark all files using (say) Perl major-mode, do occur to find out all places where a certain function is mentioned in these files. Navigate to the point at will through the Occur window.

'M-s a C-s' - ibuffer-do-isearch
– Do an incremental search in the marked buffers.
This is so awesome that you have to try it right this instant. Select two or more buffers, hit the hotkey, search for something that occurs in all these buffers. These two features alone are enough to make me a lifelong fan of IBuffer. Go do it now!

'Q' - ibuffer-do-query-replace
– Query replace in each of the marked buffers.
I don’t think I have to sell this feature now that I’ve explained the first two. ‘I’ is it’s regex brother.

'E' - ibuffer-do-eval
– Evaluate a form in each of the marked buffers. (i.e. evaluate your own emacs-lisp code on each buffer)
Eg: suppose you are viewing a set of logs that are being updated in real-time. You want to activate auto-revert-mode on all the logs so that you can see all the changes simultaneously and finally catch that timing issue. Mark the logs in ibuffer, hit ‘E’ and say (auto-revert-mode 1)

Some other magic:
=================

't' - Unmark all currently marked buffers, and mark all unmarked buffers.
'* *' - Unmark all marked buffers.
'* M' - Mark buffers by major mode.
'* s' - Mark special buffers
'/ m' - Add a filter by major mode.
'/ n' - Add a filter by buffer name.
'/ c' - Add a filter by buffer content.
'/ e' - Add a filter by an arbitrary Lisp predicate.
'/ /' - Remove all filtering currently in effect.
's f' - Sort the buffers by the file name.
's v' - Sort the buffers by last viewing time.
's s' - Sort the buffers by size.
's m' - Sort the buffers by major mode.
'=' - View the differences between this buffer and its associated file.

On a final note, I can stack any of these operations on top of each other. I can do a logical OR or a logical AND of the operations. My creativity is limited only by my imagination.

Fibonacci Numbers

January 21, 2010

I’m guessing that all my blog readers know what the Fibonacci sequence is. For the uninitiated, it is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34…. ad infinitum.

Each number is the sum of the preceding 2 numbers. That is,

F(n) = F(n-1) + F(n-2)

where,
F(0) = 0
F(1) = 1

A Neat Trick:
————-
How can you easily convert miles to kilometers? You can use Fibonacci numbers: 5 miles is 8 kilometers (actually 8.045 km). 8 miles is 13 kilometers (12.87 km). 13 miles is, yes you guessed it, 21 kilometers (20.917 km). “But hey, this will only work for Fibonacci numbers!”. Well, I’m not done yet! Say you want to convert 20 miles to kilometers. Simply express 20 as the sum of Fibonacci numbers:

20 = 13 + 5 + 2.

Convert each number according to our mile-kilometer rule:

= 21 + 8 + 3
= 32

Voila! In actuality, 20 miles comes to 32.18 km. Neat huh?

Can you guess the logic behind this? The reason this works is as follows:
To convert from miles to kilometers, we have to multiply by a factor of 1.609. Now, the Fibonacci sequence has an extremely interesting property – the ratio of two consecutive Fibonacci numbers approaches the Golden Ratio: 1.618. Mystery solved!

A Puzzle:
———
Now here is a question I was once asked:

“For a flight of stairs with n steps, write a program to print the number of ways you can climb it if you can only move either one or two steps forward at any point.”

I couldn’t figure it out in time, but understood the approach later (I think Prajwalit was the one who told me the answer). Since this post is about Fibonacci numbers, it’s a given that they make an appearance in this puzzle. The challenge is to explain why the Fibonacci numbers show up.

Finally, for a tougher programmatic challenge, modify the problem as follows:

“For a flight of stairs with n steps, write a program to print all the combinations of ways you can climb it if you can only move either one or two steps forward at any point. If n is entered as 3, your program should show the following output –
n = 3
1 1 1
1 2
2 1”

UBI/UBIFS on NANDSIM

January 20, 2010

NAND simulator (NANDSIM) is an extremely useful debugging and development tool which simulates NAND flashes in RAM or a file. NANDSIM can emulate various errors and report wear statistics, which is extremely useful when testing how flash software handles errors.
Steps involved in the working of NANDSIM :

1 ) We perform all the following steps as a super-user.

$ sudo su

2) Now we mount the NANDSIM module. That is,we create a virtual raw flash device.
The parameters are as follows :

first_id_byte : The first byte returned by NAND Flash ‘read ID’ command (manufacturer ID)

econd_id_byte : The second byte returned by NAND Flash ‘read ID’ command (chip ID).The entire table of chip ids is given in the file nand_ids.c in the source code given here.

third_id_byte and fourth_id_byte are optional parameters which are initialized to 0xff (empty) by the system if not specified by the user. They are the third and fourth ID returned by the READ ID command.

By default NANDSIM uses RAM but if you do not have enough RAM, you can make it emulate the flash on top of a file using the cache_file nandsim module parameter.

(more…)