Vim repeat adds something at the end of a word

In vim, I often find that I want to add a suffix to an identifier in my source code, and then repeat it by other identifiers using ".".

i.e. to convert:

foo bar baz faz 

in

 foo_old bar_old baz_old faz_old 

I would like to be able to:

 ea_old<ESC>www 

instead:

 ea_old<ESC>wea_old<ESC>wea_old<ESC>wea_old<ESC> 

In other words, I want to add text to the end of the word so that it appears as a repeating command in the story. Does anyone know how to do this?

I can do:

 nmap <Ca> ea 

to create a slightly more convenient way of adding at the end of a word, but it only repeats "a". Ideally, I want to be able to repeat the entire sequence of "eaarbitrarytext".

I have the repeat.vim plugin installed and a bit redesigned, but I really don't know what I'm doing in vimscript.

Clarification of the requirement: I want you to be able to jump using arbitrary movement commands until my cursor is somewhere on the identifier, and then click ".". to repeat adding a suffix. The above example is for a special case.

+4
source share
3 answers

e a _old ESC e . e . e . should work for you.

Another possible solution would be to use the c flag in the find and replace command:

 :.s/\<[[:alnum:]]\+\>/&_old/gc 

Then you only need to press y to confirm each change. It would be faster if you had many replacements and you had to confirm each of them manually. If, on the other hand, you want to add _old to every word in the line, you can remove c :

 :.s/\<[[:alnum:]]\+\>/&_old/g 
+7
source

I assume that the OP has adopted a very simple example to illustrate a more general problem, which can be reformulated as "I would very easily repeat an arbitrarily large sequence of commands."

And for this there is a q command. Choose your favorite register to write, say "q", then:

q q - starts recording

(follow any complex set of actions here ...)

q - stop recording

@ q - record playback

And since I myself often use this when programming, I ended up comparing the actions above with F2 , F3 and F4, respectively, on my keyboard. This allows you to repeat your set of actions in 1 key stroke. In .vimrc:

 nmap <F2> qq nmap <F3> q nmap <F4> @q 
+4
source

In this particular case, you can use the search and replace: s/ /_old /g .

+1
source

Source: https://habr.com/ru/post/1413335/


All Articles