11

Consider:

apple
pear

Put your cursor on the first line and type Yp, producing:

apple
apple
pear

Put your cursor on the pear line and type . to repeat the last command. This repeats only the p (rather than the combination Yp), producing:

apple
apple
pear
apple

I would like a way to tell . to repeat the last N commands. Something like 2. to repeat the combination Yp to produce:

apple
apple
pear
pear

Ofc that won't work -- it repeats the p twice. But the idea would be "repeat the sequence of the last 2 commands".

I know I can q-record command sequences and the repeat them with @@, but this solution isn't ideal, as it requires you to have been planning ahead all along, whereas in practice you often don't realize you need to repeat something until after you've done it.

Is there any way to achieve what I want?

Consolation prize: If the above is not possible, is there a way to repeat the last macro playback? Eg, if I typed @@ to playback my last macro, I'd like for the subsequent . to be equivalent to typing @@ again -- which it is not (eg, if the result of @@ depends on the line you're on)

1
  • I don't know a solution for the first part of your issue, but for the second part, you could try this mapping: nno <silent> @@ @@:sil! call repeat#set('@@', v:count1)<CR> But for it to work, you need to install this plugin: github.com/tpope/vim-repeat Commented Dec 2, 2016 at 0:01

4 Answers 4

4

Vim only stores the last executed command, and only that gets repeated by .; in my opinion, anything else would be difficult to handle.

If you feel the need to repeat the Yp combo as a single entity, you have to make it one single command. Tim Pope's repeat.vim plugin allows to do that easily for custom mappings. Of course, if you choose a short key [sequence] for the mapping, you can just quickly retype that (instead of .), and even avoid the repeat.vim integration.

BTW, my LineJuggler plugin provides a ]d mapping to duplicate the current line, and it does support repeating via repeat.vim.

2

In Yp, the only "change" to repeat is p. The only way I know to alter the behavior of . in normal mode is to add y to the cpoptions option:

set cpoptions+=y

which lets you repeat the last yank as well as the last change.

But that doesn't change anything to your problem because Yp is still two commands: a yank followed by a change.

I don't think you can get what you want without some non-trivial scripting. Did you take a look at the scripts section of vim.org?

1

I frequently use map to achieve this effect. For example:

map <F12> Yp

Then pressing F12 repeatedly will execute YpYpYp etc. If you want to move to the next line after every paste, map a key to Yp+ instead.

1

The remarkable contraption that is the RepeatLast plugin (by our very own @joeytwiddle) provides just this functionality, via the unholy method of recording everything you do as a macro.

It provides a number of new mappings, but, if I'm reading the documentation correctly, to perform the edits you describe you could use the sequence of commands (presuming you are using the default <leader> of \):

  • Yp Yank and put/paste the line apple,
  • j Move down a line so the cursor is now on the pear line,
  • \D forget the j command,
  • 2\. Repeat the two commands Y and p
1
  • Unholy, indeed! But I appreciate the ambition.
    – Jonah
    Commented Sep 11, 2017 at 16:18

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.