3

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

:s/.*/\=list2str(reverse(str2list(getline('.'))))"

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.

1
  • 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

5

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

Then you can do:

:2,5!rev  

Where 2,5 is range.

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

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')))

prints

b́a

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.