Reverse a word in Vim


How can I reverse a word in Vim? Preferably with a regex or normal-mode commands, but other methods are welcome too:

word => drow

Thanks for your help! PS: I'm in windows XP

Python is built in supported in my vim, but not Perl.

11/5/2009 1:42:29 PM

Accepted Answer

11/5/2009 1:54:25 PM

Here is another (pythonic) solution based on how this works:

:echo join(reverse(split('hello', '.\zs')), '')

If you want to replace all words in the buffer,

:%s/\(\<.\{-}\>\)/\=join(reverse(split(submatch(1), '.\zs')), '')/g

This works by first creating a list of characters in the word, which is reversed and joined back to form the word. The substitute command finds each word and then passes the word to the expressions and uses the result as replacement.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow