Wednesday, December 29, 2010

Finding command names and customizing key-bindings in Emacs

Of the zillion things that make Emacs a great editor, one is the ability to access all its functionality using the keyboard. However, you may find that a particular key combination is not convenient for you. For example, I find it inconvenient to use M-/ (pressing the ALT key and the / key together) for word completion. No problem! Of those zillion things, another one is the ability to modify the key bidings to your liking. I explain below how to assign word completion to S-SPC (that is, getting word completion by pressing Shift and Space keys together).

1.   The first step in customizing key bindings is finding the name of the command for a given key sequence. This is done using:

    C-h c key-sequence    

Note that you should not type the key-sequence, you have to press it the way you would for invoking the command. For example, to find the name of the command that is executed when M-/ is pressed, you cannot type letter M followed by a /, you have to press ALT and / together. This will return:

     M-/ runs the command dabbrev-expand     

in the minibuffer. There, you know the name of the command.

2.   The second step is to bind your custom key-sequence to this command. Suppose you want dabbrev-expand command to be executed when Shift and Space keys are pressed together. Do the following: open ~/.emacs (or, whichever is your Emacs init file) and add the following line to that file:

     (global-set-key (kbd "S-SPC") 'dabbrev-expand)     

3.   Close Emacs and restart. You can now use your custom key-sequence for the command you assigned to it.

If you are thinking, "okay, from your example I know that I have to use
S for Shift key and SPC for the Space key, but what do I use for some other keys, say the function keys. You can find useful information regarding this and a lot more about custom key bindings at

Saturday, December 25, 2010

Changing commit message of a revision in Mercurial

About 2-3 months back I started using Mercurial for version control. If you have not heard of Mercurial before, this post will not be of any interest to you. Just know that Mercurial is great and I strongly recommend it if in your work you feel the need to keep different versions of a file at different points of time, and the way you do this is by saving the file under different names. See here for more information on Mercurial. For the rest, read below.

As I started with Mercurial, from the very beginning I had a feeling that one day I would make a mistake in recording the message that accompanies a commit. It seemed very likely that I would forget to make note of some important change I had made to a file. Of course, there was also the possibility of typos. This is what happened a couple of days back; I committed a revision with an incomplete and partly incorrect message. I had intended to do something, changed my mind while I was modifying the file, but wrote a message which reflected my original intentions and hit enter. What made the problem worse is that I did not realize this until I had made a couple of subsequent commits. I searched on Google to find how I could fix my mistake. I found a couple of ways, but they seemed a little confusing, especially given my lack of experience with version control systems. Here I explain the method that I felt was the easiest and most straightforward to use. This involves using the histedit extension for Mercurial. The extension can do more than just change a commit message, but I will focus only on this functionality, hoping that this will make the process clearer to follow.
  1. Install histedit.
    If you have already installed histedit, you can skip this step.
    1. Download histedit.
      • Open the terminal and navigate to the directory where you want to save histedit. For
        example, I have saved it in ~/Dropbox/Mercurial_Extensions.
      • At the terminal prompt type:
        hg clone 
        This will download the histedit extension in your present working directory.
    2. Tell your Mercurial installation about the location of this extension.
      • Open .hgrc file.
        This file is usually located in your HOME directory. If so, navigate to your home folder by typing cd ~ at the terminal prompt and pressing enter.
      • Then type open .hgrc and press enter.
    3. Add the following lines in the .hgrc file.
      histedit = ~/Dropbox/Mercurial_add_ons/histedit/
      Save the file.
      Note that you may have to modify ~/Dropbox/Mercurial_add_ons/
      part of the path to point to the directory where you downloaded
      and saved the histedit extension.
  2. Navigate to the directory where the hg repository whose history you want to change is located.
  3. Ensure that none of the files that Mercurial is tracking have been modified since the last commit. If there are modified files you have to commit the changes.
  4. At the terminal prompt type hg log and press enter. Note the changeset information of the revision whose commit message you want to modify. You will need this information in the next few steps.
  5. Suppose you want to change the commit message of
    changeset:  5:48f58ff8250e
    At the terminal prompt, either type:
    hg histedit -r 5
    and press enter or, type
    hg histedit 48f58ff8250e
    and press enter.

    This will open a file with a list of changesets in a text editor. The first changeset is the one that you specified. The list will look something like the one shown in the following image.

  6. In the first column of this file, replace the word pick with the word edit in the row corresponding to the changeset whose commit message you want to edit. Do not touch the other rows. Save the file at the default location and under the default filename suggested by your editor.
  7. At the terminal prompt type hg histedit --continue and press enter. A file will open in a text editor with your old commit message. Make any changes to this message and save the file (again, use the suggested file name and location). The old message in the revision history will be replaced by your corrected message.

Go here for more information on histedit.

Tuesday, December 21, 2010

Emacs, what can you do with it?

Recently, when searching the web for a good python IDE, I came across PyCharm and a review of it by none other than the great Guido Van Rossum (see here).  From his review I learnt that he is a big fan of Emacs.  I had heard of Emacs before and I knew that it had been around for ages (Emacs development began in 1970s), but I had never given it a serious try.  Around this time, I was also looking for a good text editor.  Motivated by the knowledge that Guido was so happy with Emacs, I did a search on Google to find out whether Emacs was still worth learning. I felt this was necessary given the plethora of new editors that have been written since Emacs first came into being. Given that Emacs is so old, I thought there must be something better out there.

I found many rave reviews about emacs.  People talked about all the different things you can do with it.  Many said that it is not a text editor, it is more of an IDE. Some likened it to an operating system.  I read that Emacs has:

  1. powerful text editing and navigation commands and the ability to access all these commands with the keyboard
  2. ability to issue shell commands from within Emacs
  3. ability to perform numerous directory and file operations from within Emacs such as renaming and removing files, creating and deleting directories, browsing them etc.
  4. content sensitive editing modes for many programming languages and for TeX, LaTeX and ConTeXt.
  5. ability to function (with the help of modes such as the powerful org-mode) as a Planner, Calendar and Task manager.  (I had also been looking for a good Calendar and Task manager and most that I came across were not satisfactory for one reason or another).
  6. ability to access email and newsgroups.
    You can even play games in Emacs... I have not tried that. I was impressed after reading these things and decided to give Emacs a serious try. While I have used it for only a few weeks now,  I am quite impressed with the things that I have tried so far and am using it for increasing number of my tasks.

    If you also want to try Emacs and you use Mac OS X, the following are two possible options for downloading it:
    • GNU Emacs for Max OS X - These are vanilla builds that do not contain additional packages or patches not contained in the official release of GNU Emacs
    • Emacs for OS X modified - This is also based on GNU Emacs but come with AUCTeX and ESS. AUCTeX is useful for those who use TeX or its relatives for writing. From the ESS (Emacs Speaks Statistics) website, ESS is designed to support editing of scripts and interaction with various statistical analysis programs such as R, S-Plus, SAS, Stata and JAGS. 
    I will post here as I learn how to do various cool things in Emacs that are not possible in most other text editors.

      My First Post

      This is my first post and I am excited! I thought of blogging once before, set up a blog at and then gave up without a single post on it. Why did I give up? I think I tried to customize the theme and found that small customizations required me to shell out money, not a little but a lot; and I did not feel like spending that kind of money on a blog whose future was uncertain. (Possibly I was misinformed, so do not consider this as a definitive statement about the features of So, the blog did not go anywhere. This time around, I have decided I will worry about customizing the theme (at least beyond what is free) later. I want to get in some posts first.

      What do I plan to post here?
      I am constantly looking for better tools to accomplish my tasks. In my case these mostly happen to be software tools. In fact, sometimes the search for these tools and the time spent learning them delays the completion of the tasks. But I enjoy learning new things, so that is okay and in the long-run I think I come out ahead. 

      This blog is a place for me to write notes on the interesting things that I have learnt. Why a blog? Why don't I use a word processing software on my desktop. There are a few reasons. One, I like how I can make notes on unrelated things in the same place and neatly categorize them using tags. It appears that searching the notes will be easier here when I need them later. Two, they will be in cloud and I can access them from anywhere. Three, in learning about new things, I have benefited tremendously from other people's blogs. I hope that I can be similarly helpful.  Here I will collect knowledge that I feel like sharing. Hence, the name Knowledge Stockpile.