DEV Community

nabbisen
nabbisen

Posted on

Vim/Neovim: Sort & Unique

Vim / Neovim enables us to manipulate text in various ways, in which, for example, we can sort lines or make them unique. Here are the examples.


Sort

Use sort command.
Besides, here is the documentation of Vim / Neovim.

Specify sort order

1. ASC

By default.



:%sort


Enter fullscreen mode Exit fullscreen mode

2. DESC

Append !.



:%sort!


Enter fullscreen mode Exit fullscreen mode

3. Numeric sort

Give n as a parameter.



:%sort n


Enter fullscreen mode Exit fullscreen mode

operation image

Fig.1 Sort order

Specify sort range

1. Whole text

Use % as usual.



:%sort


Enter fullscreen mode Exit fullscreen mode

2. Specific lines only

Add startNum,endNum to the head.
2-5 lines only, for example:



:2,5sort


Enter fullscreen mode Exit fullscreen mode

3. Visual selection

As is.



:'<, '>sort


Enter fullscreen mode Exit fullscreen mode

operation image

Fig.2 Sort range

An example of order + range (combination)

Here is a DESC + numeric sort example.



:%sort! n


Enter fullscreen mode Exit fullscreen mode

operation image

Fig.3 Complex example

Make lines unique

Use sort command as well. Give it u as a parameter.



:%sort u


Enter fullscreen mode Exit fullscreen mode

operation image

Fig.4 Unique

Top comments (0)