With narrowing, the rest of a buffer is made invisible, as if it weren’t
there. This is an advantage if, for example, you want to replace a word
in one part of a buffer but not in another: you narrow to the part you want
and the replacement is carried out only in that section, not in the rest
of the buffer. Searches will only work within a narrowed region, not
outside of one, so if you are fixing a part of a document, you can keep
yourself from accidentally finding parts you do not need to fix by
narrowing just to the region you want.
(The key binding for narrow-to-region
is C-x n n.)
However, narrowing does make the rest of the buffer invisible, which
can scare people who inadvertently invoke narrowing and think they
have deleted a part of their file. Moreover, the undo
command
(which is usually bound to C-x u) does not turn off narrowing
(nor should it), so people can become quite desperate if they do not
know that they can return the rest of a buffer to visibility with the
widen
command.
(The key binding for widen
is C-x n w.)
Narrowing is just as useful to the Lisp interpreter as to a human.
Often, an Emacs Lisp function is designed to work on just part of a
buffer; or conversely, an Emacs Lisp function needs to work on all of a
buffer that has been narrowed. The what-line
function, for
example, removes the narrowing from a buffer, if it has any narrowing
and when it has finished its job, restores the narrowing to what it was.
On the other hand, the count-lines
function
uses narrowing to restrict itself to just that portion
of the buffer in which it is interested and then restores the previous
situation.