Vim Tricks 1: Sorting July 21, 2008

I actually learnt two new tricks to be able to do this. The first is selecting lines using visual mode.

Place the cursor on the first line you wish to select. Then press Shift+V and move the cursor the last line you want to select. This selects the range of lines between the two and you can perform ex commands on the selected text.

To sort the selected lines type :sort, this runs the sort command and replaces the selection with the result. You'll notice if you try to sort lines with numbers they will be sorted alphanumerically, ie. 700 would come before 8. if you want to sort numerically use :sort n. You can also remove duplicates from a list by using :sort u, to remove duplicates and sort numerically use :sort un