I'm using r a lot (e.g. for replacing " with ' when refactoring code). Is there an easy way to do something like this:

For text: values = ["a", "b", "c"].
Place cursor on first ", press r' to change to values = ['a", "b", "c"].
Now we want to repeat that five times.
For example by pressing 5<Ctrl>r.
To repeat previous r command five times for next occurences of '.

Would this be possible? And if so, how?

I know I can do <Shift>v:s/"/'/<Enter>. But this seems inefficient.

Please help making my life easier. ;)

  • 3
    Well, you could do :s:':":g Dec 23, 2015 at 15:34
  • <Shift>v:s doesn't make any sense. (It does the same as simply :s.) I wonder if you thought that specifying a range will make the :substiute work on all matches instead of just the first one (per line) – that is not the case. Instead, you need to :set gd° or append the /g global modifier to your :s commands. ° Thanks @romainl!! :D (Now I have to adapt all my :s mappings – thanks A LOT!… ;) ) Dec 29, 2016 at 17:20

1 Answer 1


Well, <Shift>v:s/"/'/<Enter> is certainly less efficient than :s/"/'<CR>.

Here are various ways to perform the same task:

f"           jump to next "
r'           replace it with a '
;.           repeat jump then repeat replace
;.           repeat jump then repeat replace
;.           repeat jump then repeat replace
;.           repeat jump then repeat replace
;.           repeat jump then repeat replace

/"<CR>       search for next "
cgn'<Esc>    change it to ' (Note: the gn command was introduced in Vim 7.3.610)
.....        repeat change 5 times

qq           start recording in register q
f"           jump to next "
r'           replace it with '
q            stop recording
5@q          replay recording 5 times

None of which being as efficient as :s/"/'<CR> which can be made even better with a small mapping:

nnoremap <key>      :s/
nnoremap <otherkey> :%s/


  • 2
    How come you don't need to use the g flag?
    – Hovercouch
    Dec 23, 2015 at 19:32
  • 6
    Because of :help 'gdefault'.
    – romainl
    Dec 23, 2015 at 22:30
  • 4
    Life changed forever
    – Hovercouch
    Dec 23, 2015 at 22:34
  • 1
    @romainl It seems that you mistyped 5@q for 5aq. Could you explain cgn'<Esc>? I cannot figure out what it means.
    – Feng Yu
    Dec 25, 2015 at 1:01
  • 2
    @FengYu, I fixed my answer, thanks. cgn means "change the next/current match of the last search", here the last search is " so the cursor is moved to the next " which is changed into a '. Since c puts us into insert mode we need to leave it with <Esc>. So that's cgn in normal mode, then '<Esc> in normal mode.
    – romainl
    Dec 25, 2015 at 10:59

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.