# [QUERY] How do you sort completions?

8 messages from 2013-05-24 to 2013-05-28. Participants: Ramkumar Ramachandra, Felipe Contreras, SZEDER Gábor, Antoine Pelisse, Marc Khouzam.
Thread: https://gitlist.dev/t/33918

## Ramkumar Ramachandra, 2013-05-24 11:33

Subject: [QUERY] How do you sort completions?
Message-ID: <CALkWK0k=nVHOBe5SgRH9xQK1+yAYBzbNMqsWxhYPo0MTZPp63g@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0k%3DnVHOBe5SgRH9xQK1%2ByAYBzbNMqsWxhYPo0MTZPp63g%40mail.gmail.com

```
Hi,

I'm not able to sort completions for some weird reason.  No matter
what I order I insert stuff into COMPREPLY, bash seems to auto-sort
them lexically.  I tried to pass --sort='-committerdate' to git
for-each-ref so I can get a sensible 'git checkout <TAB>' reply, and
I'm very annoyed that it doesn't work.

I thought it was bash's complete builtin doing the implicit sorting,
so I read the manual with no success.  Is readline doing it?  If so,
why am I not getting the ordering even in zsh (which doesn't use
readline)?

What am I missing?

Ram

```

## Felipe Contreras, 2013-05-24 13:11

Subject: Re: [QUERY] How do you sort completions?
Message-ID: <CAMP44s3_BBPu0WSzYyaVLbdg0dBuaV7MFgy1SMuK1C8P2vKbYg@mail.gmail.com>
URL: https://gitlist.dev/e/CAMP44s3_BBPu0WSzYyaVLbdg0dBuaV7MFgy1SMuK1C8P2vKbYg%40mail.gmail.com
In-Reply-To: <CALkWK0k=nVHOBe5SgRH9xQK1+yAYBzbNMqsWxhYPo0MTZPp63g@mail.gmail.com>

```
On Fri, May 24, 2013 at 6:33 AM, Ramkumar Ramachandra
<artagnon@gmail.com> wrote:
> Hi,
>
> I'm not able to sort completions for some weird reason.  No matter
> what I order I insert stuff into COMPREPLY, bash seems to auto-sort
> them lexically.  I tried to pass --sort='-committerdate' to git
> for-each-ref so I can get a sensible 'git checkout <TAB>' reply, and
> I'm very annoyed that it doesn't work.
>
> I thought it was bash's complete builtin doing the implicit sorting,
> so I read the manual with no success.  Is readline doing it?  If so,
> why am I not getting the ordering even in zsh (which doesn't use
> readline)?
>
> What am I missing?

AFAIK bash needs the completions sorted, so we added some | sort |
uniq. I suppose zsh doesn't need that, but have you tried by calling
compadd directly?

-- 
Felipe Contreras

```

## SZEDER Gábor, 2013-05-24 16:12

Subject: Re: [QUERY] How do you sort completions?
Message-ID: <20130524161243.GB2104@goldbirke>
URL: https://gitlist.dev/e/20130524161243.GB2104%40goldbirke
In-Reply-To: <CALkWK0k=nVHOBe5SgRH9xQK1+yAYBzbNMqsWxhYPo0MTZPp63g@mail.gmail.com>

```
Hi,


On Fri, May 24, 2013 at 05:03:27PM +0530, Ramkumar Ramachandra wrote:
> I'm not able to sort completions for some weird reason.  No matter
> what I order I insert stuff into COMPREPLY, bash seems to auto-sort
> them lexically.  I tried to pass --sort='-committerdate' to git
> for-each-ref so I can get a sensible 'git checkout <TAB>' reply, and
> I'm very annoyed that it doesn't work.
> 
> I thought it was bash's complete builtin doing the implicit sorting,
> so I read the manual with no success.  Is readline doing it?  If so,
> why am I not getting the ordering even in zsh (which doesn't use
> readline)?

The order of elements in COMPREPLY doesn't matter, it will be sorted
anyway:

$ _foo () { COMPREPLY=("foo" "bar" "baz") ; }
$ complete -F _foo foo
$ foo <TAB>
bar  baz  foo

I don't know who does the sorting (Bash or readline), and I don't know
any way to disable it.


Gábor

```

## Ramkumar Ramachandra, 2013-05-24 16:18

Subject: Re: [QUERY] How do you sort completions?
Message-ID: <CALkWK0n8iuwNHfxV4AHUV+721pqLUOPhM6h+e0_opmkVuOV_4A@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0n8iuwNHfxV4AHUV%2B721pqLUOPhM6h%2Be0_opmkVuOV_4A%40mail.gmail.com
In-Reply-To: <20130524161243.GB2104@goldbirke>

```
SZEDER Gábor wrote:
> I don't know who does the sorting (Bash or readline), and I don't know
> any way to disable it.

Damn; so it's impossible to have a custom-sorted completion list in
bash.  Any idea about zsh?  I know that there are completion groups,
but I'd really like custom sorting.

```

## Antoine Pelisse, 2013-05-24 16:22

Subject: Re: [QUERY] How do you sort completions?
Message-ID: <CALWbr2xxND4xz49jmwXUTTr1f=Z3nmHCTQd+bBcK_Etv5oB0Fg@mail.gmail.com>
URL: https://gitlist.dev/e/CALWbr2xxND4xz49jmwXUTTr1f%3DZ3nmHCTQd%2BbBcK_Etv5oB0Fg%40mail.gmail.com
In-Reply-To: <CALkWK0n8iuwNHfxV4AHUV+721pqLUOPhM6h+e0_opmkVuOV_4A@mail.gmail.com>

```
On Fri, May 24, 2013 at 6:18 PM, Ramkumar Ramachandra
<artagnon@gmail.com> wrote:
> Damn; so it's impossible to have a custom-sorted completion list in
> bash.  Any idea about zsh?  I know that there are completion groups,
> but I'd really like custom sorting.

I think sorting is required for faster look-up, most likely with
dichotomic search. Otherwise it would have to search the whole list
each time.

```

## Marc Khouzam, 2013-05-24 18:20

Subject: RE: [QUERY] How do you sort completions?
Message-ID: <E59706EF8DB1D147B15BECA3322E4BDC1BF6DA0E@eusaamb103.ericsson.se>
URL: https://gitlist.dev/e/E59706EF8DB1D147B15BECA3322E4BDC1BF6DA0E%40eusaamb103.ericsson.se
In-Reply-To: <CALkWK0n8iuwNHfxV4AHUV+721pqLUOPhM6h+e0_opmkVuOV_4A@mail.gmail.com>

```

> -----Original Message-----
> From: git-owner@vger.kernel.org 
> [mailto:git-owner@vger.kernel.org] On Behalf Of Ramkumar Ramachandra
> Sent: Friday, May 24, 2013 12:19 PM
> To: SZEDER Gábor
> Cc: Git List; Felipe Contreras
> Subject: Re: [QUERY] How do you sort completions?
> 
> SZEDER Gábor wrote:
> > I don't know who does the sorting (Bash or readline), and I 
> don't know
> > any way to disable it.
> 
> Damn; so it's impossible to have a custom-sorted completion list in
> bash.  Any idea about zsh?  I know that there are completion groups,
> but I'd really like custom sorting.

You could do that with tcsh :)
```

## Ramkumar Ramachandra, 2013-05-24 18:26

Subject: Re: [QUERY] How do you sort completions?
Message-ID: <CALkWK0k=yGmAvOsGO+4hS6bU=Y4mJoCq6QqVoSNajBYHaPxGng@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0k%3DyGmAvOsGO%2B4hS6bU%3DY4mJoCq6QqVoSNajBYHaPxGng%40mail.gmail.com
In-Reply-To: <CALWbr2xxND4xz49jmwXUTTr1f=Z3nmHCTQd+bBcK_Etv5oB0Fg@mail.gmail.com>

```
Antoine Pelisse wrote:
> I think sorting is required for faster look-up, most likely with
> dichotomic search. Otherwise it would have to search the whole list
> each time.

Oh, it's probably keeping just one copy of the completions in memory
(which has to be sorted for search); this is also used to display.
Makes sense, although I would have expected the shell to keep two
copies: a sorted one for searching through, and another preserving the
original order for displaying.  I suppose they didn't think of it.

```

## SZEDER Gábor, 2013-05-28 10:23

Subject: Re: [QUERY] How do you sort completions?
Message-ID: <20130528102303.GC2192@goldbirke>
URL: https://gitlist.dev/e/20130528102303.GC2192%40goldbirke
In-Reply-To: <CAMP44s3_BBPu0WSzYyaVLbdg0dBuaV7MFgy1SMuK1C8P2vKbYg@mail.gmail.com>

```
Hi,

On Fri, May 24, 2013 at 08:11:00AM -0500, Felipe Contreras wrote:
> AFAIK bash needs the completions sorted, so we added some | sort |
> uniq.

Actually, it seems to sort and remove duplicates automatically:

$ _foo () { COMPREPLY=("bar" "foo" "bar" "baz") ; }
$ complete -F _foo foo
$ foo <TAB>
bar  baz  foo  

So that | sort | uniq might not be necessary after all, and by
removing them we could spare two fork()+exec()s.

I'm not sure all supported Bash versions behave this way, though.


Best,
Gábor

```
