Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I define a custom macro in emacs in my .emacs file. Is there a way to ensure that it works only on the region I marked in a buffer before executing my command, without going into details of the macro definiton?

share|improve this question
up vote 4 down vote accepted

I don't think this is possible at the level of macros, it's going to depend on the commands inside the macro. That being said, macros have gotten more flexible since I learned Emacs, so it's possible that I'm missing something.

Emacs offers another possibility to work on part of a file: narrowing. Select the region you want to work with, and press C-x n n (narrow-to-region) to narrow the buffer to the selected region. Now the part outside the region is hidden, and all commands will affect only the visible part. Press C-x n w (widen) to go back to working on the whole file.

A feature that's often convenient together with narrowing is indirect buffers. Run M-x clone-indirect-buffer or C-x 4 c to get a second buffer editing the same file. The two buffers have the same content, and saving either saves the file, but the buffers have separate regions, modes, and visibilities, so you can use narrowing in one and keep the whole file view in the other.

share|improve this answer
@student Yes, all the narrowing-related commands are on the C-x n prefix. – Gilles May 19 '12 at 13:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.