Sorting words (not lines) in VIM

后端 未结 6 513
长情又很酷
长情又很酷 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:05

    :!perl -ne '$,=" ";print sort split /\s+/'
    

    Not sure if it requires explanation, but if yes:

    perl -ne ''
    

    runs whatever is within '' for every line in input - putting the line in default variable $_.

    $,=" ";
    

    Sets list output separator to space. For example:

    => perl -e 'print 1,2,3'
    123
    
    => perl -e '$,=" ";print 1,2,3'
    1 2 3
    
    => perl -e '$,=", ";print 1,2,3'
    1, 2, 3
    

    Pretty simple.

    print sort split /\s+/
    

    Is shortened version of:

    print( sort( split( /\s+/, $_ ) ) )
    

    ($_ at the end is default variable).

    split - splits $_ to array using given regexp, sort sorts given list, print - prints it.

提交回复
热议问题