Sorting words (not lines) in VIM

后端 未结 6 507
长情又很酷
长情又很酷 2020-12-13 02:44

The built-in VIM :sort command sorts lines of text. I want to sort words in a single line, e.g. transform the line

b a d c e f

6条回答
  •  时光取名叫无心
    2020-12-13 03:25

    Here's the equivalent in pure vimscript:

     :call setline('.',join(sort(split(getline('.'),' ')),' '))
    

    It's no shorter or simpler, but if this is something you do often, you can run it across a range of lines:

     :%call setline('.',join(sort(split(getline('.'),' ')),' '))
    

    Or make a command

     :command -nargs=0 -range SortLine ,call setline('.',join(sort(split(getline('.'),' ')),' '))
    

    Which you can use with

    :SortLine
    :'<,'>SortLine
    :%SortLine
    

    etc etc

提交回复
热议问题