narrowing buffer contents

'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 shownnamebinding
region (selection)narrow-to-regionC-x n n
current pagenarrow-to-pageC-x n p
functionnarrow-to-defunC-x n d
everythingwidenC-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 disables narrow-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 shownnamebinding
subtreeorg-narrow-to-subtreeC-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.


Viola Pastor said...

Narrowing is such a helpful feature. Thanks for the good writeup. Would setnu be a helpful workaround for your line numbering complaint?

Scott Turner said...

You might want to explain why you would want to use narrowing.

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.

Dave Sailer said...

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.

djcb said...

@Bratsche-freude: with 'linum' i'm still seeing narrowed-area-relative number rather than buffer-relative. Is that different with setnu?

@Scott Turner: I'm using it mostly for org-mode narrowing, and sometimes to have 'global' functions apply only to a part of the buffer. But that's in the article already :)

@Dave Sailer: do you have some examples of what SPF could do with narrowing but emacs can't? (just curious)

Merlin said...

'narrow-to-region is likely disabled by default because someone might accidentally type 'n' twice rapidly. This is much less likely with a two-key combination.

djcb said...

@Eric H. R. Alchemist: ah yes, that makes sense.

Chris. said...

If I remember correctly, SPF would allow sorting the narrowed data. You could also unnarrow lines using search. These wer very helpful when editing data files.

Alex said...

something like that would give you global linum numbers (with optional relativ numbers) while narrowed:



djcb said...

@Alex: ah, that's nice, thanks!

Viola Pastor said...

@djcb, sorry for the long delay. You said: "with 'linum' i'm still seeing narrowed-area-relative number rather than buffer-relative. Is that different with setnu?"

It sure is. Works right, fresh out of the box.

The Cook said...

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.

Drew said...

See also wide-n.el. It lets you have a ring of buffer restrictions (narrowings), which you can cycle among using `C-x n x x x...'. Each time you hit `x' a different narrowing is made current.

`C-x n x' with a prefix arg:

* `C-u' widens buffer completely

* `C-0' widens completely and empties the list of narrowings

* `C-N' (numeric N != 0) takes you to the Nth previous narrowing - IOW, it widens abs(N) times. Negative N also pops the ring entries back through the (-)Nth one.

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.






Draxil said...

Even though this blog has been defunct for some time I am still finding gems in here :)