In order to invert all characters on each line of a range I could think of the following


where I can put a [range] if I want.

Is there any better way to do this? I mean, quicker to write/more performant/more versatile.

  • Write a function that accept a range and do what you want and then define a visual mode mapping for that?
    – jdhao
    Commented Nov 28, 2020 at 9:51

2 Answers 2


If you're on Linux, you probably have rev installed.

Then you can do:


Where 2,5 is range.

  • Even if it's not technically Vim, it's the perfect solution, I think.
    – Enlico
    Commented Dec 3, 2020 at 17:16

A slight improvement

:[range]s/.*/\=join(reverse(split(submatch(0), '\zs')), '')/

The use of submatch allows this to to be more flexible, e.g.,

:[range]s/hello: \zs.*/\=join(reverse(split(submatch(0), '\zs')), '')/

We use split/join because the str2list approach will fail with combining characters, for example;

echo list2str(reverse(str2list('áb')))



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.