2010-08-22

'Narrowing' is yet another of those many useful emacs features that took me
years to appreciate, mostly because I never really tried it. I may not be the
only one, so here's a short introduction.

Narrowing is the concept of hiding the buffer contents except for what you
are currently working on. This is useful when you don't want to be distracted,
but also because it allows you to execute commands only on the narrowed
part. You can narrow different things:

what's shown

name

binding

region (selection)

narrow-to-region

C-x n n

current page

narrow-to-page

C-x n p

function

narrow-to-defun

C-x n d

everything

widen

C-x n w

I never used narrowing for the current page, but apparently it's used by
e.g. Info-Mode to show only one page.

That last one is pretty important to remember; it's not totally obvious how
to get back to 'normal' mode where you can see everything. For this very
reason ('where the #>*$@ did my text go'), always-helpful emacs by defaults
disablesnarrow-to-region (but, for some reason, not the other ones). To
enable it, put the following in your .emacs:

(put 'narrow-to-region 'disabled nil)

Also note that the mode-line will show 'Narrow' when you're in narrow mode,
lest you forget.

When you're using org-mode there is an additional one you might want to
memorize:

what's shown

name

binding

subtree

org-narrow-to-subtree

C-x n s

I'm using that last one quite often; I have org-files where I keep meeting notes
etc., and when in a certain meeting, I only want to see the notes for that
specific meeting.

One bug? feature? of narrowing is that line-numbering is relative to the
narrowed area rather than the full buffer. I'd prefer to have the real line
numbers.

For myself, I use "narrow-to-region" primarily because buffer-wide operations only work on the narrowed region. E.g., if I want to do a replace but only on a portion of a buffer, I can narrow to the portion, do the replace and then unnarrow.

Note that when you narrow-to-region, Emacs helpful reminds you how to unnarrow in the status line.

Great. Thank you. We had something like this on the mainframe. I think it was the SPF editor (it's been a while). Emacs narrowing is much more limited, at lease what I see here, but a nice addition. And now I can sell my mainframe. Heh.

Is there a way to get major modes to cooperate with narrowing? I have files which contain some C code and some TeX code, and I'd like to (1) narrow to a chunk of the buffer and (2) switch the mode to the relevant mode for that chunk. I have some elisp code for this, but it only works sometimes. With c-mode, I conjecture that the reason for this is that c-mode pays attention to some of the unnarrowed buffer when it's doing it's highlighting, but I'm not sure.

The same narrowing is is available even if you modify its context, because markers are used. So you can seriallize the ring of narrowings, converting all its markers to positions, and save it persistently for later reuse. Library bookmark+.el does that, so you can bookmark narrowings.