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?
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?
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
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.
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.
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.
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.
If you have cua-mode
enabled, then:
I said
as text to be insertedI prefer the cua-mode
way of working, compared to the Emacs standard rectangle where the equivalent would be:
I said
RET to insert textThe 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
.