Posts Tagged ‘Emacs’

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.

Emacs: linux-c-mode and Line numbers

June 25, 2009

Tired of the slow ssh access to one of the servers located outside India, I finally figured it was time to start playing with emacs to reduce the number of keystrokes needed.

First on, I wanted the linux-c-mode enabled *by default* whenever I open a file whose extension is ".c" (Yeah Yeah, I know extensions are meaningless for Linux.. but emacs is intelligent 🙂 ). For all the other type of files, linux-c-mode should be OFF, because it will mess-up their indentation.
Second, line numbers should be displayed on the left-hand-side panel of each file I open. Then to go a particular line I just "M-g g". Simple.
Third, All this should be accessible to my user account as well as root user.

So, I edit the ~/.emacs file (create if not already present) like this (for the first goal):

(defun linux-c-mode ()
"C mode with adjusted defaults for use with the Linux kernel."
(c-set-style "K&R")
(setq tab-width 8)
(setq indent-tabs-mode t)
(setq c-basic-offset 8))

(add-to-list 'auto-mode-alist '("\.c$" . linux-c-mode))

The first chunk defines the linux-c-mode (from Documentation/CodingStyle) and the last line enables it by default ONLY for *.c files. Simple!

A quick-tips Emacs post

May 22, 2009

In this post, I’m basically going to just cut-paste code from my .emacs file, customizations that I feel have been most useful to me:

1) Cscope customizations

;for cscope
(load-file "~/.emacs.d/xcscope.el")
(require 'xcscope)
(setq cscope-do-not-update-database t)
(define-key global-map [(control f3)] 'cscope-set-initial-directory)
(define-key global-map [(control f4)] 'cscope-find-this-file)
(define-key global-map [(control f5)] 'cscope-find-this-symbol)
(define-key global-map [(control f6)] 'cscope-find-global-definition)
(define-key global-map [(control f7)] 'cscope-find-this-text-string)
(define-key global-map [(control f8)] 'cscope-pop-mark)
(define-key global-map [(control f9)] 'cscope-find-functions-calling-this-function)
(define-key global-map [(control f10)] 'cscope-find-called-functions)
(define-key global-map [(control f11)] 'cscope-display-buffer)
;cscope settings end here

An earlier tutorial I wrote for the cscope-newbie.

2) Auto-completion with Smart Tab

Emacs Tip of the Day # Collapsing Functions #

January 5, 2009

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!

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 🙂