1

I have a mapping to "Stamp" text over the current word that cursor is on: nnoremap S "_diwP. When I do S and then try to repeat it with dot . only the last command will be repeated: P.

Is it possible in vim to alter the mapping (somehow grouping the commands?) so that . will repeat all of it? In other words, is it possible to repeat a compound command like "_diwP with . in vim?

Close to being answers:

  • I know that in this particular case the command could be rewritten with c instead of d+p.
  • Also the command could be recorded as a macro and repeated with @ but that is not as convenient as using .

1 Answer 1

2

If you have tpope's vim-repeat, you can do something like the following:

nnoremap S :call Stamp()<CR>

function Stamp() abort
  normal! "_diwP
  silent! call repeat#set("S", -1)
endfunction

You can do it without the function, but this is a bit clearer in my opinion.

2
  • I recommend adding a <silent> to the nnoremap so the :call doesn't flash the command-line...
    – filbranden
    May 22, 2020 at 14:51
  • 1
    You know, it's funny, I actually removed <silent> from most of my personal mappings recently; I found that I didn't really care about seeing the "flash," and it occasionally reminded me what was really going on. That said, your map, your modifiers :) @filbranden
    – D. Ben Knoble
    May 22, 2020 at 14:56

Your Answer

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

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