13

I've noticed vim can be pretty intuitive, but I'm having trouble finding a way to repeat the second-to-last command (or third-to-last, fourth-to-last, etc.). By typing . in normal mode it will repeat the last command. Pressing 3. will repeat the last command three times.

Let's say I type oHello<Esc> then A, World!<Esc>. I now get

Hello, World!

on the screen. Now, say I want to get

Hello, World!
Hello
Hello
Hello

without typing Hello. Does vim store the history beyond one command, so I could type something like 3,2. (i.e. repeat the second-to-last command three times)?

  • 2
    Remember you can record anything as a macro (qaoHello<esc>q then @a to repeat Hello whenever you want.) There's also @@ to repeat the last used macro. – James McLaughlin Apr 19 '12 at 18:17
  • 2
    I'd assume you could also do 3@a to repeat the macro multiple times – redbmk Apr 19 '12 at 18:34
8

No, the . command in vim does not have a history, nor is there any sort of repeat history for normal mode commands that I am aware of. For the fewest number of keystrokes, I'd recommend 4oHello<Esc>3jA, World<Esc>. If you've already done oHello<Esc>A, World<Esc>, you could follow that with uyy<Ctrl-r>3p.

3

There's this plugin called RepeatLast.vim which does what you wantbut with a cost: your vim is always in recording macro mode. You can install it with package manager you're using like any other vim packages or you can simply clone and drop it in ~/.vim/plugin if you're using vim or ~/.config/nvim/plguin if you're using neovim. To repeat second to last action you should type 2\. in normal mode and to repeat th to last action you should type <n>\. in normal mode.

http://www.vim.org/scripts/script.php?script_id=4381 https://github.com/vim-scripts/RepeatLast.vim

  • 2
    @Sassan, could you perhaps edit your answer and quickly explain how to install this plugin, and how exactly it can be used to solve the OP's problem? – terdon May 22 '17 at 10:01
  • 2
    @terdon if someone reads my answer and doesn't know how to use a package manager it's really better for him to get curious and go spend the time and learn about package managers in vim. Same for the one who reads it and doesn't know about getting information from docs/README.md for vim plugins. – Sassan May 22 '17 at 11:10
  • That's not the point. An answer that is just a link isn't considered an answer. Answers should be self-contained and offer all the information necessary to apply the proposed solution. Thanks for the edit! – terdon May 22 '17 at 11:12
  • 1
    Actually I agree, this plugin is too hackish to be used by people who don't know how to install it :) – Boris Churzin Dec 27 '17 at 9:42

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.