I have a file like this:

1 = "go 1"
2 = "go 2"
3 = "go 3"

I want duplicate the last line and increase both numbers. The needed amount of lines is very high (approximately 90), so I want to automate a little.

The result should look like that:

1 = "go 1"
2 = "go 2"
3 = "go 3"
4 = "go 4"
5 = "go 5"
6 = "go 6"


88 = "go 88"
89 = "go 89"
90 = "go 90"

How can I achieve this?

  • 1
    It is not clear what you want. The current question looks like you could just append a line 91 = "go 91". Maybe, you can add a small example for 5 lines with the text before and after the edit. – Tobias Jun 1 '16 at 16:17
  • Okay, now I got it. The original text has only 3 lines and you want to duplicate the last line 87 times such that the final text has 90 lines. – Tobias Jun 1 '16 at 16:40
  • 1
    You could do M-: (loop for i from 4 to 90 do (insert (format " %d = \"go %d\"\n" i i))) behind the line ` 3 = "go 3"`. – Tobias Jun 1 '16 at 16:47
  • Check out the solution using tiny package here. – Kaushal Modi Jun 1 '16 at 17:24
  • 1
    loop is an alias for cl-loop from the cl-macs-package. You get it if you load cl-lib. I always do that since I am using elisp rather often. – Tobias Jun 1 '16 at 19:58

Another option is to use the tiny package (also available from Melpa).

m1\n90|%d = "go %d"

Simply type the above and do M-x tiny-expand.

The below linked reference to another emacs.SE Q&A provides details on how to use this tiny syntax.

enter image description here

Reference: Solution to Enumerating with multiple cursors?

  • Your code expands in my setup to (mapconcat (lambda(x) (let ((lst (list x))) (format "%d = \"go %d" (nth 0 lst) x)))(number-sequence 1 90) "\n")" using github.com/abo-abo/tiny#setup – toogley Jun 3 '16 at 13:44
  • 1
    @toogley What version of emacs are you on? (this worked fine for me on 24.5 and works fine on the latest emacs-25 pretest too). Does the same thing happen if you call tiny-expand in an emacs -Q session (with just the tiny.el loaded)? Is that only thing that happens is that string gets pasted? You get no errors? In any case, please report back if you get backtrace after doing M-x toggle-debug-on-error, and then M-x tiny-expand with the point at the end of m1\n90|%d = "go %d". Also it would be helpful to post this as an issue on the package github. – Kaushal Modi Jun 3 '16 at 14:20
  • The error was caused by the position of my cursor. After typing/inserting the above, my cusor was targeting the quote character instead of a space afterwards. Would you mind adding that condition to your post, just for clarification. Thanks :) – toogley Jun 14 '16 at 14:19
  • @toogley Sorry, I am not sure what you mean by that. After you type m1\n90|%d = "go %d", your cursor will be directly after the last double quote there. And then you do M-x tiny-expand. That works fine as you see in the gif. – Kaushal Modi Jun 14 '16 at 15:41
  • Ah sry, I forgot to note: I'm using the evil package. After leaving the insert mode, the cursor is placed upon the last character. – toogley Jun 15 '16 at 11:15

You can use keyboard macros. Try this:

<f3>                    ;; (start macro)

<f3>                    ;; (insert counter)
SPC                     ;; self-insert-command
=                       ;; self-insert-command
SPC                     ;; self-insert-command
"go                     ;; self-insert-command * 3
SPC                     ;; self-insert-command
C-u C-x C-k C-a         ;; (reset counter to previous value)
<f3>                    ;; (insert counter)
"                       ;; self-insert-command
C-e                     ;; move-end-of-line
RET                     ;; newline

<f4>                    ;; (end macro)
C-u 99 <f4>             ;; (repeat macro 99 times)

This will create lines starting with 0 = "go 0" all the way through 99 = "go 99". If you wanted to start at a different number, you could press C-u 42 C-x C-k C-c before recording the macro to start the counter at 42 instead of 0.

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.