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?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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