Sunday, March 22, 2009

Narrowing down to a region in Emacs

Emacs is awesome! What a piece of software! Recently, I discovered a very useful feature in Emacs.

Let's say, you are writing some documentation in text-mode. Inside it, you need to write a piece of C code with proper indentation. You can switch to c-mode, but then the rest of your text starts looking weird with colors and all, and this is very distracting (to me at least). But Emacs, as usual, has a solution to this problem.

Select a few blank lines (where you want to insert your code) within the text, and give the command M-x narrow-to-region (the keyboard shortcut for this is C-x n n). You will find yourself in a buffer with only the region you selected. Then you can enter c-mode and write the code. When done, you can go back to your original full view by using the command M-x widen (or C-x n w) and switch back to whatever mode you were in (text-mode in this case).

I find this pretty useful.
Post a Comment