Sort numerically in VI editor

23,119

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.)

Share:
23,119
arunmoezhi
Author by

arunmoezhi

i consider myself jack of all trades and master of none https://github.com/arunmoezhi SOreadytohelp

Updated on July 25, 2022

Comments

  • arunmoezhi
    arunmoezhi over 1 year

    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}
    
  • dash-tom-bang
    dash-tom-bang over 11 years
    It's worth noting that it's not the removal of the dash that's the big difference here, it's that it's using functionality that's built into Vim instead of calling an external application (note the lack of a ! as well). Documentation is also available in Vim itself; :help :sort
  • Nate
    Nate over 11 years
    I've edited my answer because I think your point is important enough to make more prominent.