61

Assume I have a text file with content

1
123
12
12345

If I want to add an 'a' in the beginning of each line I can simply use string-rectangle (C-x r t), but what if I want to append an 'a' to the end of each line, after which the file should become

1a
123a
12a
12345a

Thanks.

ZelluX
  • 69,107
  • 19
  • 71
  • 104

3 Answers3

76

You could use replace-regexp for this purpose, with the $ regexp metacharacter that matches end-of-line. Go to the start of the buffer, and then do M-x replace-regexp, and answer $ and (your text) to the two prompts.

Or, in emacs-speak, for your specific example of adding a:

M-< M-x replace-regexp RET $ RET a RET

nelhage
  • 2,734
  • 19
  • 14
  • tbh, "keyboard macro" was my first thought, too, until I stopped and thought "there's got to be a simpler way". – nelhage Feb 02 '11 at 04:50
  • 4
    Or `C-M-% $ RET a RET` to do the same thing more selectively with `query-replace-regexp` – phils Feb 02 '11 at 04:50
  • 2
    @phils Your suggestion for some reason doesn't work. That is obvious way - but doesn't work. Try it yourself. – Angus Comber May 13 '13 at 17:51
  • 1
    user619818: it works just fine, and I have used this myself on a great many occasions. I suspect you are mis-typing the key combination `C-M-%` which on US keyboards is `Ctrl` + `Alt` + `Shift` + `5` (or use `M-x query-replace-regexp`) – phils May 13 '13 at 22:32
  • And let's not forget that adding whatever at the start of each line is essentially the same, except using ^ instead of $, like so: `M-< M-x replace-regexp RET ^ RET Z RET` – Ben Aug 08 '14 at 08:10
  • How do we type RET in mac :( – Akshay Hazari Jun 27 '18 at 05:04
  • @AkshayHazari `RET` is emacs speak for the Enter key ("Return") – mogsie Jun 27 '18 at 09:08
  • @mogsie I know , just unable to type Enter key in string search and replace-string in OSX . In the terminal its ctrl and enter , but in emacs it doesn't work unlike when I had linux. – Akshay Hazari Jun 27 '18 at 14:02
25

Emacs keyboard macros are your friend.

C-x ( C-e a C-n C-x )

Which just sets up the keyboard macro by: starting the keyboard macro (C-x (), go to the end of the line (C-e), insert an a, go to the next line (C-n), and then end the macro recording (C-x )).

Now you can either execute it (C-x e), and keep pressing e for each line you want to have it run on, or you can run it on a region with C-x C-k r.

If you do this a lot, you can save the macro, or you can write a function. This would be one such function:

(defun add-string-to-end-of-lines-in-region (str b e)
  "prompt for string, add it to end of lines in the region"
  (interactive "sWhat shall we append? \nr")
  (goto-char e)
  (forward-line -1)
  (while (> (point) b)
    (end-of-line)
    (insert str)
    (forward-line -1)))
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 4
    I usually create such a macro by typing `F3 End a Down F4` followed by series of `F4`. I tend to prefer normal arrow keys and Home/End over the Emacsisms. – Rene Saarsoo Feb 02 '11 at 13:20
  • 1
    One additional useful piece of information is that of course hitting `e` a boatload of times is that you can combine it with `C-u` to repeat the task many times: `C-u` `20` `C-x` `e` to get 20 repeats of the macro – mogsie Jun 27 '18 at 09:12
0

Another option is install the multiple-cursors package: https://github.com/magnars/multiple-cursors.el

Then you can mark (select) all of those lines and run mc/edit-ends-of-lines which will create a cursor at the end of each of those marked lines. Next type your text and it'll be added at each cursor.

Jason Axelson
  • 4,485
  • 4
  • 48
  • 56