How to sort numeric and literal columns in Vim
Asked Answered
A

5

61

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?

Apery answered 30/8, 2009 at 21:51 Comment(1)
Related, if your second (or 3rd, 4th ...) 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.Crabstick
T
65

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
Tanny answered 30/8, 2009 at 21:58 Comment(5)
an example of this please :'<,'>!sort -n -k 2 ?Apery
This is a visual mode command - Pavel used V to select a range, then typed :, which automatically inserts the visual selection range '<,'>, and then typed the actual command !sort -n -k 2. You can of course specify the range in any way - see :help range if you're curious.Courtier
Wait this is brilliant. I wanted to sort using GNU sort's -h flag for human-readable numerals to show largest files at the top: :'<,'>!sort -hrDeodand
Most of these solutions (this one and most below) work. However, they all fail to leave lines in the original order when the lines have the same key (column) value. I ended up having to write a Java program to get this behaviorTrinitrobenzene
@Johnkendall FYI, sort does have that capability. In the example, it doesn't matter since there are only two columns. However, if there are more, sort -n -k 2 will sort using column two till the end of the line. Use sort -n -k 2,2 to only sort by column two. Adding more pairs of -k for additional columns. Sometimes that still fails to leave them in the stable order. In that case, use sort's -s/--stable option.Edgington
T
50

Sort all lines on second column N by using Vim sort command, e.g.

:sort /.*\%2v/ 

Reference: vimtips.txt

Thyrse answered 30/8, 2009 at 21:59 Comment(2)
Wow, vim has internal sorting procedure! How huge is that bastard?!Tanny
Any reason you can't upgrade? vim.org does say 7.2 is the latest stable version.Dayfly
C
26

For vim7 I would go for:

:sort n /.*\s/

This will sort numbers ignoring text matched by given regexp. In your case it is second column.

Cutwater answered 31/8, 2009 at 5:4 Comment(0)
P
24

Sort by 2nd column by selecting it in visual mode (e.g. Control+v), then run:

!sort

or to sort by third column

sort -k 3 

or

:sort /.*\%3v/

Alternatively select the lines you wish to sort using the Shift+V command. Then enter

!sort -k 3n

or use the below code to tell Vim to skip the first two words in every line and sort on whatever follows:

:%sort /^\S\+\s\+\S\+\s\+/ 

or i.e. sort by 8th line:

:sort /.*\%55v/

The 'virtual' specification is the absolute number of column , which treats spaces + tabs as single character (shortly, it doesn't count tabs as eight spaces),

so to sort by last column:

:%sort /\<\S\+\>$/ r
Palua answered 27/3, 2013 at 12:55 Comment(1)
This should be the answer. But shell sort! is nice as well.Ecology
B
0

If more columns were there, you may use repetition to avoid complicated pattern.
For example, this will sort the entire file by the 100th column ("column" here means the space separated column)

:%sort /^\(\S\+\s\+\)\{99}/
Baez answered 20/1, 2020 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.