Reverse a word in Vim

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

    I don't have Python supported on my VIM, but it looks like it would be pretty simple to do it with Python. This article seems like a good explanation of how to use Python in VIM and I'm guessing you'd do something like this:

    :python 'word'[::-1]
    

    The article indicates that the result will appear in the status bar, which would be non-optimal if you were trying to replace the string in a document, but if you just want to check that your girlfriend is properly reversing strings in her head, this should be fine.

    0 讨论(0)
  • 2021-01-17 11:53

    if your version of VIM supports it you can do vw\is or viw\is (put your cursor at the first letter of the word before typing the command)... but I have had a lot of compatibility issues with that. Not sure what has to be compiled in or turned on but this only works sometimes.

    EDIT:

    \is is:

    :<C-U>let old_reg_a=@a<CR>
    \ :let old_reg=@"<CR>
    \ gv"ay :let @a=substitute(@a, '.\(.*\)\@=', '\=@a[strlen(submatch(1))]', 'g')<CR> 
    \ gvc<C-R>a<Esc> :let @a=old_reg_a<CR> 
    \ :let @"=old_reg<CR>
    

    Didn't remember where it came from but a google search come this article on vim.wikia.com. Which shows the same thing so I guess that's it.

    0 讨论(0)
  • 2021-01-17 11:59

    This Tip might help: http://vim.wikia.com/wiki/Reverse_letters

    0 讨论(0)
  • 2021-01-17 11:59

    Well you could use python itself to reverse the line through the filter command. Say the text you had written was:

    Python
    

    You could reverse it by issuing.

    :1 ! python -c "print raw_input()[::-1]"
    

    And your text will be replaced to become:

    nohtyP
    

    The "1" in the command tells vi to send line 1 to the python statement which we are executing: "print raw_input()[::-1]". So if you wanted some other line reversed, you would send that line number as argument. The python statement then reverses the line of input.

    0 讨论(0)
提交回复
热议问题