34

I have the code below:

Hello
There
I am some code

And want to add code in front of it, like:

I said Hello
I said There
I said I am some code

So how would I add a prefix to each line?

46

You can add a prefix with M-x string-rectangle, which is bound to C-x r t. This is part of a series of commands that operates on rectangular selections; this one inserts text at every line in the rectangle.

Start by setting the mark at the beginning of the first line, and move your cursor to the first character of the last line you want to prefix:

*Hello
 There
▮I am some code

Then use C-x r t, enter your prefix (I said) and press RET. This adds the text to each line in the rectangle:

I said Hello
I said There
I said I am some code

If you don't line up your cursor on the same column as your mark, it will overwrite that part of the rectangle:

*Hello
 There
 I am▮ some code

with the same command results in:

I said o
I said e
I said  some code
  • 3
    you are the second person to earn good answer badge :) – nicael Oct 6 '14 at 14:53
  • An alternative is to use M-x string-insert-rectangle. It's very similar, but string-insert-rectangle inserts at the beginning of each line of the selection, whereas string-rectangle replaces, so with the latter you have to be more precise about your selection if you don't want to overwrite anything. – Jeff Clites Aug 5 '17 at 14:42
32

You could use multiple-cursors-mode, to put a cursor on every line. Then, any text you type will be inserted on each line.

You would start out with the point at the beginning of the piece of code:

▮Hello
 There
 I am some code

Then hit C-> twice. (This is the suggested key binding; you have to set it up yourself.) This creates two extra cursors on the following two lines:

▮Hello
▮There
▮I am some code

Then type I said. The text will be added on each line:

I said ▮Hello
I said ▮There
I said ▮I am some code

Finally hit RET to exit multiple cursors mode and leave point at the last cursor:

I said Hello
I said There
I said ▮I am some code

Here is a video that shows how it works.

27

Personally, I prefer replace-regexp (replace "^" with "I said ").

12

Another option is to use macros, which can handle more tasks than rectangular selections or multiple cursors, even if it's a little clunkier for this specific case.

Position the cursor at the beginning of the first line and hit F3 to start recording, insert the text, move the cursor to the beginning of the next line and hit F4 to stop recording. Now hit F4 again to repeat the macro, C-2 F4 to repeat it twice, or C-0 F4 to repeat it until an error (such as running out of lines) is encountered.

9

You could install evil and do it in any number of vi-like ways - I prefer visual block selection using Ctrl + V to mark each line and then Shift + I to insert and then type the text you want to insert and finally hit ESC to exit insert mode and the text will be prepended to each line. This is very similar to emacs rectangle selections but a few less keystrokes.

7

This is very similar to artagnon's answer, but replace-regexp is not bound to any key.

So, I actually use query-replace-regexp which is bound by default to C-M-% and then replace ^ with the given prefix I said and then type ! to replace all without prompting anymore.

4

If you have cua-mode enabled, then:

  • C-a to move to beginning of line,
  • C-RET to start rectangle,
  • (down key) twice to move to third line,
  • I said as text to be inserted
  • C-RET to end rectangle

I prefer the cua-mode way of working, compared to the Emacs standard rectangle where the equivalent would be:

  • C-a to move to beginning of line,
  • C-SPC to start rectangle,
  • (down key) twice to move to third line,
  • C-x r t I said RET to insert text

The cua-mode saves two key presses compared to standard Emacs rectangle.

But for me, the additional advantage of cua-mode is that I can append text to the rectangle (I personally to do not know how to append text with standard Emacs rectangle). With cua-mode, it is one RET key press only, inputed before the I said.

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.