The Problem
In this post we will address the problem of collecting text from scattered locations in one or more buffers and placing it in a specific location. For example: suppose that you want to copy some C function prototypes from a .c file into a header file as illustrated below.
What is an efficient way to accomplish this? There are two solutions that come to mind right away.
Solution 1: Use Text Registers
- Mark text in the source buffer.
- Append text to some register (we'll use "A" here).
- Repeat steps 1 and 2 until you have collected all of the text that you need to move.
- Switch to the destination buffer.
- Move to the target location.
- Copy register "A" to destination buffer with
C-x r i A
Steps 1 and 2: Mark text in the source buffer, append with
M-x append-to-register
Steps 4, 5, and 6: Switch to the destination buffer, move to the target location and insert register "A".
Unfortunately
append-to-register
places all of the copied text onto a
single line so some editing must be done.Solution 2: Use the Global Mark
Emacs' CUA mode provides a global mark which offers a more efficient solution than text registers. When the global mark is enabled, any keystrokes that you type will be sent to the target location and any text that you place in the kill ring will also be sent to the target location. So the sequence of steps is now:
- Move to target location and enable the global mark
S-C-SPC
. - Copy the text you wish to send to the target location to the kill ring.
- Press ";" then
RET
to place a semicolon at the the end of the target text. PressingRET
causes any other text copied to the kill ring to be placed on a separate line. This saves us the editing step required when we useappend-to-register
.
.emacs
file:
(setq-default cua-enable-cua-keys nil)
(cua-mode t)
This will enable CUA features such as global mark/copy and enhanced rectangle editing but not enable CUA keys such as C-c for copy, C-v for paste etc.Let's illustrate the above steps with some screenshots.
Step 1: Move to target location and enable the global mark
S-C-SPC
.Step 2: Copy the text you wish to send to the target location to the kill ring. The text will instantly appear at the target location.
Step 3: Press ";" then
RET
to place a semicolon at the the end of the target text.
Pressing RET
causes any other text copied to the kill ring to be placed on a separate line.
This saves us the editing step required when we use append-to-register
.
No comments:
Post a Comment