tags:

views:

528

answers:

3

Using vim 6.0.

say I'm editing this file

sdfsdg
dfgdfg

34     12
2      4
45     1
34     5

and I want to sort the second column

Thanks

+3  A: 

If you have descent 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
Pavel Shved
an example of this please :'<,'>!sort -n -k 2 ?
vehomzzz
I also get a an error: E20: Mark is not set
vehomzzz
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.
Jefromi
+4  A: 

sort all lines on second column N

:sort /.*\%2v/

reference:
http://rayninfo.co.uk/vimtips.html

mschmidt42
Wow, vim has internal sorting procedure! How huge is that bastard?!
Pavel Shved
No sort command in vim 6.0 :(
vehomzzz
Any reason you can't upgrade? vim.org does say 7.2 is the latest stable version.
Mark Rushakoff
yes, company's bureaucracy. I wish I could though
vehomzzz
+2  A: 

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.

Maxim Kim