【VIM】VIM Sorting Text

   |   4 minute read   |   Using 689 words

【VIM】VIM sort function

​ I was clean up my vimrc file

​ and I see a bunch of plugins in my vimrc, and I want to make all the lins in a alphabet

​ so, there is a way can do that

​ here is what i have done

  1. select the first letter in visual block

  2. use command sort iu which stands for sort it ignore cases and unique

    then , it worked for me

And the following are the vim doc of sorting, you can also check it in your vim by using the command :h sort

7. Sorting text                                         sorting

  Vim has a sorting function and a sorting command.  The sorting function can be
  found here: sort(), uniq().

                                                          :sor :sort
  :[range]sor[t][!] [b][f][i][l][n][o][r][u][x] [/{pattern}/]
                          Sort lines in [range].  When no range is given all
                          lines are sorted.

                          With [!] the order is reversed.

                          With [i] case is ignored.

                          With [l] sort uses the current collation locale.
                          Implementation details: strcoll() is used to compare
                          strings. See :language to check or set the collation
                          locale. Example:
                                  :language collate en_US.UTF-8
                                  :%sort l
                          v:collate can also used to check the current locale.
                          Sorting using the locale typically ignores case.
                          This does not work properly on Mac.

                          Options [n][f][x][o][b] are mutually exclusive.

                          With [n] sorting is done on the first decimal number
                          in the line (after or inside a {pattern} match).
                          One leading '-' is included in the number.

                          With [f] sorting is done on the Float in the line.
                          The value of Float is determined similar to passing
                          the text (after or inside a {pattern} match) to
                          str2float() function. This option is available only
                          if Vim was compiled with Floating point support.

                          With [x] sorting is done on the first hexadecimal
                          number in the line (after or inside a {pattern}
                          match).  A leading "0x" or "0X" is ignored.
                          One leading '-' is included in the number.

                          With [o] sorting is done on the first octal number in
                          the line (after or inside a {pattern} match).

                          With [b] sorting is done on the first binary number in
                          the line (after or inside a {pattern} match).

                          With [u] (u stands for unique) only keep the first of
                          a sequence of identical lines (ignoring case when [i]
                          is used).  Without this flag, a sequence of identical
                          lines will be kept in their original order.
                          Note that leading and trailing white space may cause
                          lines to be different.

                          When /{pattern}/ is specified and there is no [r] flag
                          the text matched with {pattern} is skipped, so that
                          you sort on what comes after the match.
                          'ignorecase' applies to the pattern, but 'smartcase'
                          is not used.
                          Instead of the slash any non-letter can be used.
                          For example, to sort on the second comma-separated
                          field:
                                  :sort /[^,]*,/
                          To sort on the text at virtual column 10 (thus
                          ignoring the difference between tabs and spaces):
                                  :sort /.*\%10v/
                          To sort on the first number in the line, no matter
                          what is in front of it:
                                  :sort /.\{-}\ze\d/
                          (Explanation: ".\{-}" matches any text, "\ze" sets the
                          end of the match and \d matches a digit.)
                          With [r] sorting is done on the matching {pattern}
                          instead of skipping past it as described above.
                          For example, to sort on only the first three letters
                          of each line:
                                  :sort /\a\a\a/ r

                          If a {pattern} is used, any lines which don't have a
                          match for {pattern} are kept in their current order,
                          but separate from the lines which do match {pattern}.
                          If you sorted in reverse, they will be in reverse
                          order after the sorted lines, otherwise they will be
                          in their original order, right before the sorted
                          lines.

                          If {pattern} is empty (e.g. // is specified), the
                          last search pattern is used.  This allows trying out
                          a pattern first.

  Note that using :sort with :global doesn't sort the matching lines, it's
  quite useless.

  :sort does not use the current locale unless the l flag is used.
  Vim does do a "stable" sort.

  The sorting can be interrupted, but if you interrupt it too late in the
  process you may end up with duplicated lines.  This also depends on the system
  library function used.

   vim:tw=78:ts=8:noet:ft=help:norl:



© 2025 by clayliu. All Rights Reserved.