Reverse a word in Vim

前端 未结 10 1463
南方客
南方客 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.

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

    If you have rev installed (e.g. via MSys or Cygwin) then it's really not this difficult.

    Select what you want to reverse and filter (%! <cmd>) it:

    :%! rev
    

    This pipes your selection to your shell while passing it a command.

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

    If you have some time on your hands, you can bubble your way there by iteratively transposing characters (xp)...

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

    Assuming you've got perl support built in to vim, you can do this:

    command! ReverseWord call ReverseWord()
    function! ReverseWord()
    perl << EOF
        $curword = VIM::Eval('expand("<cword>")');
        $reversed = reverse($curword);
        VIM::Msg("$curword => $reversed");
        VIM::DoCommand("norm lbcw$reversed");
    EOF
    endfun
    

    And potentially bind that to a keystroke like so:

    nmap ,r :ReverseWord<CR>
    
    0 讨论(0)
  • 2021-01-17 11:44

    There is a tricky way to do this if you have Vim compiled with +rightleft. You set 'allowrevins' which let you hit Ctrl+_ in insert mode to start Reverse Insert mode. It was originally made for inserting bidirectional scripts.

    Type your desired word in Insert mode, or move your cursor to the end of an already typed word. Hit Ctrl+_ and then pick a completion (i_Ctrl-x) method which is the most likely not to return any results for your word. Ysing Ctrl+e to cancel in-place completion does not seem to work in this case.

    I.e. for an unsyntactic text file you can hit in insert mode Ctrl+x Ctrl+d which is guaranteed to fail to find any macro/function names in the current file (See :h i_CTRL-X_CTRL-D and:h complete for more information).

    And voila! Completion lookup in reverse mode makes the looked up word reverse. Notice that the cursor will move to the beginning of that word (it's reversed direction of writing, remember?)

    You should then hit Ctrl+_ again to get back to regular insert mode and keyboard layout and go on with editing.

    Tip: You can set 'complete' exclusively (for the buffer, at least) to a completion option that is guaranteed to return no result. Just go over the options in :h 'complete'. This will make the easy i_Ctrl-N / i_Ctrl-P bindings available for a handy word reversal session. You can ofcourse further automate this with a macro, a function or a binding

    Note: Setting/resetting 'paste' and 'compatible' can set/reset 'allowrevins'. See :h allowrevins.

    0 讨论(0)
  • 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
    • <C-r>=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

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