Reverse a word in Vim

前端 未结 10 1467
南方客
南方客 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:44

    I realize I'm a little late to the game, but I thought I'd just add what I think is the simplest method.

    It's two things:

    • Vim's expression register
    • pyeval (py3eval on recent vim releases) function

    So to reverse a word you would do the following:

    • "ayiw yank word into register a
    • =py3eval('"".join(reversed(str(' . @a ')))') use vim's = (expression) register to call the py3eval function which evaluates python code (duh) and returns the result, which is then fed via the expression register into our document.

    For more info on the expression register see https://www.brianstorti.com/vim-registers/#the-expression-and-the-search-registers

提交回复
热议问题