:sort in Vim 7

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

:sort in Vim 7

Bram Moolenaar

In the current snapshot the :sort command is available.  That means
sorting no longer depends on an external command.  This is the help for
it:

                                                        *:sor* *:sort*
:[range]sor[t][!] [i] [u] [/{pattern}/]
                        Sort lines in [range].

                        With [!] the order is reversed.

                        With [i] case is ignored.

                        With [u] only keep the first of a sequence of
                        identical lines (ignoring case when [i] is used).

                        When /{pattern}/ is specified the text matched with
                        {pattern} is skipped, so that you sort on what comes
                        after the match.  For lines without a match sorting
                        starts in the first column (e.g., for empty lines).
                        Instead of the slash any non-letter can be used.
                        For example, to sort on the second comma-separated
                        field: >
                                :sort /[^,]*,/
< To sort on the text at virtual column 10 (thus
                        ignoring the difference between tabs and spaces): >
                                :sort /.*\%10v/


Hopefully that satisfies most users.

--
DENNIS: You can't expect to wield supreme executive power just 'cause some
        watery tart threw a sword at you!
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

Alexei Alexandrov
Hi Bram Moolenaar, you wrote:

>
> Hopefully that satisfies most users.
>

What about sorting in numeric order? I often use 'sort -n' external command and it would be nice for Vim to support this as well.

--
Alexei Alexandrov
Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

Bram Moolenaar

Alexei Alexandrov wrote:

> What about sorting in numeric order? I often use 'sort -n' external
> command and it would be nice for Vim to support this as well.

You mean that "11"  sorts after "9"?  That should be quite simple to do.
But how about octal?  And hex?  And where to sort an empty line or a
line that doesn't start with a digit (skip white space)?  Hmm, perhaps
it's not so simple after all...

--
Eye have a spelling checker, it came with my PC;
It plainly marks four my revue mistakes I cannot sea.
I've run this poem threw it, I'm sure your please to no,
It's letter perfect in it's weigh, my checker tolled me sew!

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

Mikołaj Machowski
In reply to this post by Bram Moolenaar
Dnia wtorek 24 maj 2005 19:46, Bram Moolenaar napisa?:
>
> :[range]sor[t][!] [i] [u] [/{pattern}/]

What about :g/:v commands as range providers?

Just tested and looks like Vim is ignoring them and treats sort as global
command. Could be interesting to sort lines in this way:
1
c
2
a
3
d
:g/\D/sort
1
a
2
c
3
d
At the moment it returns:
1
2
3
a
c
d

If you don't want to implement this at least this behaviour should be
mentioned in docs (IMO).

m.
--
LaTeX + Vim = http://vim-latex.sourceforge.net/
Vim-list(s) Users Map: (last change 15 May)
 http://skawina.eu.org/mikolaj/vimlist
CLEWN - http://clewn.sf.net

Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

Marian Csontos
In reply to this post by Bram Moolenaar
On Tue, 24 May 2005 22:52:55 +0200, Bram Moolenaar <[hidden email]>  
wrote:

>
> Alexei Alexandrov wrote:
>
>> What about sorting in numeric order? I often use 'sort -n' external
>> command and it would be nice for Vim to support this as well.
>
> You mean that "11"  sorts after "9"?  That should be quite simple to do.
> But how about octal?  And hex?  And where to sort an empty line or a
> line that doesn't start with a digit (skip white space)?  Hmm, perhaps
> it's not so simple after all...
>

may be sorting on substitution could solve this. And sorting would be much  
more powerful:
        sort/\(\d\d\).\(\d\d\).\(\d\d\)/\3\2\1/
could sort files with date in DD.MM.YY format too.

it seems this could be relatively simply achieved by set of commands:
        g/^\(\d\d\).\(\d\d\).\(\d\d\)/s/^\(\d\d\).\(\d\d\).\(\d\d\)/\3\2\1 \0/ |  
sort | s/^\d\+ //

-- Marian


________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com
Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

Marian Csontos
On Wed, 25 May 2005 10:23:43 +0200, Alexei Alexandrov  
<[hidden email]> wrote:

> On 5/25/05, Marian Csontos <[hidden email]> wrote:
>> may be sorting on substitution could solve this. And sorting would be  
>> much
>> more powerful:
>>         sort/\(\d\d\).\(\d\d\).\(\d\d\)/\3\2\1/
>> could sort files with date in DD.MM.YY format too.
>>
>
> Yes, it will work for dates but I don't see a simple regexp that would
> make it working for numbers.
>

>> it seems this could be relatively simply achieved by set of commands:
>>         g/^\(\d\d\).\(\d\d\).\(\d\d\)/s/^\(\d\d\).\(\d\d\).\(\d\d\)/\3\2\1  
>> \0/ |
>> sort | s/^\d\+ //
>>
> Hmm... I think I'll stick with !sort -n command...
>

hmmmmmmm:
        :sort/\d\+/\=("0000000000000000".(submatch(0)+0))[-16:]." ".submatch(0)/
?

-- Marian




________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com
Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

Marian Csontos
On Wed, 25 May 2005 11:05:42 +0200, Alexei Alexandrov  
<[hidden email]> wrote:

>> hmmmmmmm:
>>         :sort/\d\+/\=("0000000000000000".(submatch(0)+0))[-16:]."  
>> ".submatch(0)/
>> ?
>>
>
> Again, !sort -n is easy to type and I use it directly by typing. Your
> expression is nice but the only way to adopt it for daily use is to
> make a mapping. I don't want another mapping - I already have
> dozens... :-)

you could define function too:

func! DecWOrig(str)
        return ("0000000000000000".(a:str+0))[-16:]." ".a:str
endf
substsort/\d\+/\=DecWOrig(submatch(0))/

or

func! Dec0WOrig()
        return ("0000000000000000".(submatch(0)+0))[-16:]." ".submatch(0)
endf
substsort/\d\+/\=Dec0WOrig()/

> And sorting numbers is rather common task.

> Anyway, I believe Bram will do it right.

However, I agree with you: n switch would be fine, only wanted to show how  
powerful substsort would be.

Sorting numbers has many questions to answer as Bram noted:
- how to handle octal, hexadecimal numbers?
- how are they written: 0xAB or ABh or...
But n switch for decimal number would be enough for lot of people.

-- Marian


________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com
Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

Bram Moolenaar
In reply to this post by Mikołaj Machowski

Mikolaj Machowski wrote:

> Dnia wtorek 24 maj 2005 19:46, Bram Moolenaar napisa?:
> >
> > :[range]sor[t][!] [i] [u] [/{pattern}/]
>
> What about :g/:v commands as range providers?
>
> Just tested and looks like Vim is ignoring them and treats sort as global
> command. Could be interesting to sort lines in this way:
> 1
> c
> 2
> a
> 3
> d
> :g/\D/sort
> 1
> a
> 2
> c
> 3
> d
> At the moment it returns:
> 1
> 2
> 3
> a
> c
> d
>
> If you don't want to implement this at least this behaviour should be
> mentioned in docs (IMO).

Would be quite complicated to implement.  The :global command doesn't
know about the command that follows, the :sort command doesn't know
where it is invoked from.

--
CRONE:  Who sent you?
ARTHUR: The Knights Who Say GNU!
CRONE:  Aaaagh!  (she looks around in rear) No!  We have no licenses here.
           "Monty Python and the Holy editor wars" PYTHON (MONTY) SOFTWARE LTD

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: :sort in Vim 7

John (Eljay) Love-Jensen
In reply to this post by Marian Csontos
Numerical sorting...

+ How to handle octal numbers? (if at all)
+ How to handle hexadecimal numbers?  (if at all)
   - 0xAB format?
   - ABh format (ala Pascal, and some assemblers)?
   - 16#AB# format (ala Ada)?
   - $AB format (ala some assemblers)?
   - X'AB' format (ala PL/I)?
   - &hAB format (ala some versions of BASIC)?
   - &#AB; format (ala HTML)?
+ How to handle arbitrary based numbers?  (if at all)
   - Lines with differing arbitrary based numbers?
+ Handling case (0xAB vs 0Xab) is a concern.
+ Handling unary '+'
+ Handling unary '-'
+ Handling numerals (digit characters) in other Unicode script pages

The simple solution, as Marian said, "But n switch for decimal number would be enough for lot of people."

Another answer, "The :%!sort -n facility is still available, for those hard to reach places."

--Eljay

Reply | Threaded
Open this post in threaded view
|

RE: :sort in Vim 7

Keith Roberts
In reply to this post by Bram Moolenaar
>-----Original Message-----
>From: Mikolaj Machowski [mailto:[hidden email]]
>Sent: Tuesday, May 24, 2005 3:33 PM
>To: [hidden email]
>Subject: Re: :sort in Vim 7
>
>Dnia wtorek 24 maj 2005 19:46, Bram Moolenaar napisał:
>>
>> :[range]sor[t][!] [i] [u] [/{pattern}/]
>
>What about :g/:v commands as range providers?
>
>Just tested and looks like Vim is ignoring them and treats
>sort as global command. Could be interesting to sort lines in this way:
>1
>c
>2
>a
>3
>d
>:g/\D/sort
>1
>a
>2
>c
>3
>d
>At the moment it returns:
>1
>2
>3
>a
>c
>d
>
>If you don't want to implement this at least this behaviour
>should be mentioned in docs (IMO).
>
>m.

What would you expect such a sort to do?  

1 - I presume non-matching lines would be left alone
2 - where would the sorted matched lines go? beginning of buffer? end of buffer?

{range} OTOH makes sense to me.  That would be equivalent to :{range}!sort ... (where {range} lines are replaced with the filtered result).  

g/pattern/ isn't a range, is it?  it's a sequence of marked lines.

-Keith