Sort numerically in VI editor

arunmoezhi picture arunmoezhi · Dec 28, 2012 · Viewed 19.8k times · Source

Possible Duplicate:
How to sort numeric and literal columns in Vim

I have to sort the below lines based on thread id.

Internal thread 0 bound to OS proc set {1}
Internal thread 1 bound to OS proc set {5}
Internal thread 2 bound to OS proc set {9}
Internal thread 10 bound to OS proc set {41}
Internal thread 9 bound to OS proc set {37}

When I issue :!sort -n they get sorted like this:

Internal thread 0 bound to OS proc set {1}
Internal thread 1 bound to OS proc set {5}
Internal thread 10 bound to OS proc set {41}
Internal thread 2 bound to OS proc set {9}
Internal thread 9 bound to OS proc set {37}

But I need them to be sorted like this:

Internal thread 0 bound to OS proc set {1}
Internal thread 1 bound to OS proc set {5}
Internal thread 2 bound to OS proc set {9}
Internal thread 9 bound to OS proc set {37}
Internal thread 10 bound to OS proc set {41}

Answer

Nate picture Nate · Dec 28, 2012

Just use Vim's own sort function. Visually highlight the text (or use a range) and type:

:sort n

Documentation is available here: http://vim.wikia.com/wiki/Sort_lines

Or in Vim itself: :help sort

(Edited to reflect an important point of clarification from dash-tom-bang and the reference to Vim's own help file.)