A quick and powerful Emacs tip: Repeat last complex command

August 23, 2010 by

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 : eldoc mode and c-eldoc mode

May 22, 2010 by

eldoc mode as the name suggests provides documentation for Elisp files. This is a very useful and cool mode and shows the function signature in the mode-line when your cursor is on a particular function.  It also provides info on the variables. Check out the screen-shots below.

eldoc : Function signatures

eldoc : Variables description

To start eldoc mode, add the following to your .emacs file

;;Turn on documentation in elisp mode
(add-hook 'emacs-lisp-mode-hook
          '(lambda ()
	     (turn-on-eldoc-mode)))

c-eldoc mode is eldoc mode for c. It displays the arguments of C functions while one is programming.

c-eldoc

To add c-eldoc mode, get the latest c-eldoc.el, copy it in your load-path and then add then following to your .emacs file.

(add-hook 'c-mode-hook 'c-turn-on-eldoc-mode)

Emacs : PKGBUILD mode

May 5, 2010 by

Discovered an amazing major mode for Emacs a few days back to edit PKGBUILDs. Essentially based on shell mode with the following extra bindings specific to PKGBUILDs.

C-c C-a pkgbuild-tar
C-c C-b pkgbuild-makepkg
C-c C-e pkgbuild-etags
C-c RET pkgbuild-update-md5sums-line
C-c C-r pkgbuild-increase-release-tag
C-c C-u pkgbuild-browse-url

I especially like pkgbuild-update-md5sums-line – very useful.

It also has some other cool functions. Check it out here.

My fork of the same is available here.

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

April 14, 2010 by

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 by

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.