Reverse a word in Vim

前端 未结 10 1465
南方客
南方客 2021-01-17 11:16

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

word => drow

<
10条回答
  •  心在旅途
    2021-01-17 11:33

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

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

    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.

提交回复
热议问题