Is it possible to start insertion in the beginning of a word when standing in the middle of it with a single keystroke? Currently I am using bi or Bi, but it does not allow me to use . to repeat the same action.

I really love A and I which operate with a line. I am looking for alternative commands which operate with w and W. I was able to find a replacement for ea and Ea, see How to append to the end of word with one key stroke? However, its not so easy for bi and Bi.

Let's say we have these lines:

random_name: SomeKindOfType = "foo"
long_name: AnotherType = "bar"
short_name: ThirdType = "spam"

And I want to add a prefix for all the types. When standing in the middle of SomeKindOfType I would like to press a single keystroke, write My prefix and then repeat the same insertion two times with j. to get this output:

random_name: MySomeKindOfType = "foo"
long_name: MyAnotherType = "bar"
short_name: MyThirdType = "spam"

I have unsuccessfuly tried defining my own command with repeat.vim plugin; it seems not to work with insertion commands such as i, a, c.

1 Answer 1


Re-hashing what's available in the linked question:

  • ciwMy<C-r>-<esc>j. (harder to map, I suppose; maybe something with a self-deleting autocommand)
  • qqbiMy<esc>qj@q
  • /\<\w+Type<CR>iMy<esc>n.
  • :[range]normal! 0f:wiMy
  • "maybe something with a self-deleting autocommand" -- For the mapping to be repeatable with ., the self-deleting autocommand would have to be set from within the single c operation, which it could by using <c-r>= to call a function that does so, but that expression wouldn't be re-evaluated by ., so repeats wouldn't work. Maybe a permanent autocommand.
    – JoL
    Commented Jan 12, 2022 at 19:10
  • @JoL the autocommand I’m thinking of was basically InsertLeave normal! "-p (Might need an execute to avoid that double-quote showing as a comment). That’s not going to be good for a permanent thing. OTOH with tpope’s repeat plugin, I think making it repeatable is far easier, but possibly not perfect.
    – D. Ben Knoble
    Commented Jan 12, 2022 at 19:51
  • Yes, I understood, but to set that up you'd need something like nmap <leader>i cb<c-r>=execute('au InsertLeave * ++once normal "-p')<cr>. However, while that would work when invoking with \i, it wouldn't when trying to repeat it with .. It would just put the prefix you insert without restoring the part that was deleted, because vim seems to save the result of the = expression instead of re-evaluating for each call of ., so you only get the autocommand set-up on the first time.
    – JoL
    Commented Jan 12, 2022 at 20:04
  • For the permanent autocommand, I was wondering if it were possible to know if we just left insert mode of precisely the one started by one particular nmap, to only act on those. Also, yes, it may be easier to modify tpope's script. For all I know, the decision to require appending instead of prepending the function call may have been arbitrary, and the switch could be relatively simple.
    – JoL
    Commented Jan 12, 2022 at 20:14
  • @JoL not in general, since mappings are expanded a bit like as if we had used feedkeys. But you could set a magic buffer-local var, check it, and unset it… I wasn’t suggesting modifying repeat, but rather hooking into it.
    – D. Ben Knoble
    Commented Jan 12, 2022 at 21:19

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.