Archives: July 21st, 2008

New Vim tricks

Monday, July 21st, 2008

I’ve been using vim as my main text editor for over 5 years now and because I’m now dependent on it and using it every day at work I’ve decided to challenge myself to learn a new trick every week. Last week’s trick was sorting lines.

Sorting Lines

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

By placing the cursor on the first line you wish to select then pressing Shift+V and moving the cursor the last line to select you select the range of lines between the two and you can do any of your normal ex commands on just the selected text.

To sort the selected lines type :sort, this runs the sort command and replaces the selection with the result. You 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.