Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort numeric and literal columns in Vim

Tags:

vim

sorting

Using Vim 6.0. Say I'm editing this file:

sdfsdg dfgdfg  34     12 2      4 45     1 34     5 

How do I sort the second column?

like image 634
vehomzzz Avatar asked Aug 30 '09 21:08

vehomzzz


People also ask

How do I sort numbers in vim?

Sorting text in Vim is easy! Select the text, then press : , type sort , then hit enter! It'll sort the whole document by default, but you can enter a range too.

How do I sort a specific column in vim?

column happens to be the first column with a decimal number, note that you could use simply :sort n , as the help says: "With [n] ... sorting is done on the first decimal number in the line..." This doesn't apply to your case, but might to somebody elses.


1 Answers

If you have decent shell available, select your numbers and run the command

:'<,'>!sort -n -k 2 

If you gonna type this in visual mode, after typing the colon, markers '<,'> will appead automatically, and you'll only have to type the rest of it.

This type of commands (:[motion]!) is called filtering. You can learn more by consulting vim's help:

:h filter 
like image 147
P Shved Avatar answered Sep 30 '22 06:09

P Shved