1

I had asked a similar question previously (How to copy each line 11 times, incrementing the last "1" in each line from 2-12). I wasn't sure if I should ask this follow-up question in that same thread or not, but I think it might have been confusing to do that, so I'll ask a new question instead.

I would like to take a line that looks like*:

...?start=0&end=180&...

and copy it 19 times such that each of the two numbers is incremented by 180, so it would look like:

...?start=0&end=180&...
...?start=180&end=360&...
...?start=360&end=540&...
...
...?start=3420&end=3600&...

How can I do that (preferably with a one-line command, but a macro is fine too)?

Thank you all very much.

*The ... represents other text in the line, before and after that particular segment. (Also, in case it's relevant, I'll note that the one ? character and the two & characters shown in that segment of the line are the only occurrences of those characters in the entire line.)

1 Answer 1

3

You can record a macro, carry out your instructions, close the macro, then run it.

In your case you may start with the cursor at the beginning of the line to copy.
Press qa to start recording macro 'a' (you can pick any letter, but beware these are the same registers as the copy-paste registers).
Then carry out your actions: copy the line yy, paste it p, find a sure fire way to place your cursor in the right location, maybe /?ENTER, add 180 180CTRL-A (or whatever shortcut is defined for you to add a number), move the cursor, add again, then move the cursor to the beginning of the current line (not necessary in this particular case but good practice), finally, close the recording with q.

Now run the macro once or 18 times @a / 18@a.


EDIT: alternative answer, using the answer to your similar question:

:%s/\(.*start=\)\([0-9]\+\)\(.*end=\)\([0-9]\+\)\(.*\)/\=join(map(range(1, 20), 'submatch(1) . (submatch(2) + (v:val-1) * 180) . submatch(3) . (submatch(4) + (v:val-1) * 180) . submatch(5)'), "\n")/
3
  • I used /=, Enter, then l to get the cursor to the right location; then several l's to do it the second time. I was having the problem that <C-a> was selecting all text instead of incrementing. I recalled having that problem before - I had forgotten what I had done before, but I did a bit of searching and recalled that I had followed some instructions on another board and set the + sign to be increment with nnoremap <kPlus> <C-a> and set the - sign to be decrement with nnoremap <kMinus> <C-x>), so I used + instead of <C-a>. Anyway, it worked! Thank you. Commented May 16, 2017 at 12:00
  • 1
    You are most welcome + alternative solution in the edit to my answer.
    – asoundmove
    Commented May 16, 2017 at 12:24
  • Your alternate solution - the one-line command that I was looking for - works as well. Great work! Thanks again. Commented May 17, 2017 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.