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 .

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)

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

  • I recommend adding a <silent> to the nnoremap so the :call doesn't flash the command-line... – filbranden May 22 '20 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 '20 at 14:56

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.