Author Archive

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)

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”


November 25, 2009

Well, I’ve been away for ages haven’t I? I’ve been a little busy, and truth be told, the well of inspiration has been a little dry for a while now. Anyhoo, I decided that I needed to *just start* writing again and the ideas will begin to flow. Today’s entry is about humour. In fact, it is downright hilarious.

In programming circles, parsing HTML with Regular Expressions is considered a bad idea. “Exactly how bad?”, you ask. Bad enough that one user on Stack Overflow says it’ll bring about the end of the world. Or something like that. You can’t really make sense of the crazy in the later bits!

“You can’t parse [X]HTML with regex. Because HTML can’t be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n​erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg​ex parsers for HTML will ins tantly transport a programmer’s consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection wil​l devour your HT ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi ght he com̡e̶s, ̕h̵i s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq uid pain, the song of re̸gular exp​ression parsing will exti nguish the voices of mor​tal man from the sp here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ”

The rant had me literally rolling on the ground convulsing in laughter. Good Stuff. Thank you Jeff Atwood, for showing me this rant!

P.S: I have some posts lined up, so don’t go deleting me from your RSS just yet :). From now on, I’ll try to keep it one post per week!