Making the Most of the Region
One of the most useful abstractions Emacs has to offer is the “region.” The region refers to the characters between the “point” and the “mark.” I know, I know, more vocabulary. But these ones are simple.
The point is the place in the document at which you enter text. It’s usually shown as a little flashy box thing. When you type, the characters are inserted right before the point.
The mark is just a place in the text. There’s nothing inherently special about it, which is what makes it so useful.
Let me explain what I mean. The mark is what it’s name implies: just a marker. It’s set automatically when you do various things.
For example, when you use C-s to search,
the mark is set to the position your point was at before you started searching.
Then you can use C-x C-x,
which moves the point to the mark,
to go back to where you were once you found what you wanted.
You can also set the mark yourself.
C-Space sets the mark at the current point.
So back to the region. The region is the standard way of working with a bunch of text in Emacs. Which you need to do a lot.
Say, for example, you’re writing some code,
but you want to comment out a method for some reason.
All you have to go is move to the beginning of the method,
C-Space to set the mark there,
move to the end and do M-x comment-region.
Because the region stretches from the mark
at the beginning of the method
to the point at the end,
the whole method is commented out1.
The region is also great for deleting some text.
Rather than backspacing furiously,
all you need to do is C-Space at the front of the section,
move to the end, and C-w, which kills the region.
Easy as pie.
However, most functions don’t limit themselves to the region.
They just run through the whole buffer.
replace-string, for instance.
But what if you only want to replace it within a certain bit?
What are you to do?
Luckily, there’s a handy solution for this.
C-x n n “narrows” the buffer to the region.
It hides all the text in the buffer that’s not in the region.
Then you can run something like replace-string
and have it only touch the portion of the buffer that’s visible.
To “widen” the buffer back to its normal size,
just run C-x n w.
Note that Emacs thinks that narrow-buffer
is potentially confusing, so the first time you use it,
you’ll get a confirmation dialog before it does anything.
Just answer “yes” and Emacs’ll enable it for you.
1 I actually find this so handy, I’ve bound comment-region to C-n c.
About Me
Feed
Becoming a Blockhead



M-;is your friend.I swear I gain more knowledge from comments to these posts than I could possibly disseminate in the posts themselves.