Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim repeat append something at end of word

Tags:

vim

In vim I frequently find myself wanting to append a suffix to an identifier in my source code, and to then repeat it on other identifiers using '.'.

i.e. to transform:

foo bar baz faz

to:

foo_old bar_old baz_old faz_old

I would like to be able to do:

ea_old<ESC>w.w.w.

instead of:

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

In other words, I want appending text to the end of a word to appear as a repeatable command in the history. Anyone know how to do this?

I can do:

nmap <C-a> ea

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

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

Clarifying the requirement: I want to be able to jump around using arbitrary movement commands until my cursor is somewhere on the identifier, and then hit "." to repeat the appending of a suffix. The above example is intended to be a special case.

like image 972
Andrew Wagner Avatar asked May 19 '12 17:05

Andrew Wagner


2 Answers

ea_oldESCe.e.e. should work for you.

Another possible solution would be to use the c flag on a search and replace command:

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

Then all you have to do is press y to confirm each replacement. This would be faster if you have a lot of replacements to do and want to confirm each one manually. If, on the other hand, you want to add _old to every word on a line, you can remove the c:

:.s/\<[[:alnum:]]\+\>/&_old/g
like image 50
Tim Pote Avatar answered Oct 27 '22 01:10

Tim Pote


My guess is that the OP took a very simple example to illustrate a more generic problem that can be re-formulated as "I would like to repeat an arbitrarily big sequence of commands very easily".

And for that, there is the q command. Pick your favourite register for recording, say "q", then:

qq -- starts recording

(do any complicated set of actions here...)

q -- stops the ercording

@q -- plays back the recording

And as I am myself using this very often when programming, I ended up mapping the actions above to F2, F3 and F4 respectively on my keyboard. This allows to repeat your set of actions in really 1 key stroke. In .vimrc:

nmap <F2> qq
nmap <F3> q
nmap <F4> @q
like image 31
Balint Avatar answered Oct 27 '22 01:10

Balint