object_name = "foo"
another_long_object_name = "bar"
again_different_length_word_which_needs_same_modification = "spam"
I would like to press a keystroke when standing in the middle of object_name and add a suffix _id then repeat the j. command two times to get this result:
The way I do that is ce<Ctrl-r>-_id. You can repeat that with . since it's a single normal mode operation. The steps are:
ce change to end of word, stay in insert mode
<Ctrl-r>- insert what you just deleted with ce (the contents of the - register)
- continue inserting
_id
Here's what ce<Ctrl-R>-_id<Esc>j.j. gets you:
object_name_id = "foo"
another_long_object_name_id = "bar"
again_different_length_word_which_needs_same_modification_id = "spam"
I really love A and I which operate with a line. Is there the same alternative, which operates with a word either w or W?
Unfortunately, all keys have other uses. You could :nmap operations like ea, bi, etc. to a new key sequence, or overwrite a single key's previous use. However, . would only repeat the last native normal-mode operation, rather than the whole mapping. There seems to be a repeat.vim plugin that allows repeating whole mappings with ., though. EDIT: This plugin won't work for this use-case though, because it requires appending a function call to the mapping and we can't do that because we want it to leave us in insert mode.
I and A are only shortcuts of ^i and $a because those movement and operation combinations are so common. I don't think ea, bi, Ea, and Bi are equally as common to justify getting a separate single-keystroke equivalent each when vim is already lacking keys. The only real benefit I think would be simpler repeatability, but that can be worked around like I described above with ce for ea and cE for Ea. For bi and Bi, you can typically do <Ctrl-v><vertical-movements>I, though you could also do cbfoo_<Ctrl-r>-<Esc>j.j. for bi and with cB for Bi if that's somehow better:
foo_object_name = "foo"
foo_another_long_object_name = "bar"
foo_again_different_length_word_which_needs_same_modification = "spam"
qqto start recording in theqbuffer, thenea_idto add_idat the end of the word, then optionallyj0to go at the beginning of the next line and finallyqagain to stop recording. You can then use@qto execute the macro once or5@qto run it 5 times (that's when thej0becomes useful). Or if the situation allows it (like in your example) a simple substitution command:%s/\w\+/&_id– statox Jan 10 '22 at 13:19nnoremap <leader>X a<c-g>U<c-right><c-g>U<left>– B Layer Jan 10 '22 at 13:40You might map anything onto a single keystroke but where would that actually get you?
For a trite example, how would you distinguish among "This is simple…" or "These are simplistic…"?
– Robbie Goodwin Jan 11 '22 at 22:15