threads / discuss / 33918

[QUERY] How do you sort completions?

Subject: [QUERY] How do you sort completions?

## tl;dr

8 messages between May 24, 2013 and May 28, 2013.

replies: 7people: 5as markdown or json

Ramkumar Ramachandra· May 24, 2013, 11:33 UTC · lore
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· May 24, 2013, 13:11 UTC · re: Ramkumar Ramachandra · lore

Re: [QUERY] How do you sort completions?

On Fri, May 24, 2013 at 6:33 AM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:

Show 14 quoted lines
> 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· May 28, 2013, 10:23 UTC · re: Felipe Contreras · lore

Re: [QUERY] How do you sort completions?

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

SZEDER Gábor· May 24, 2013, 16:12 UTC · re: Ramkumar Ramachandra · lore

Re: [QUERY] How do you sort completions?

Hi,
On Fri, May 24, 2013 at 05:03:27PM +0530, Ramkumar Ramachandra wrote:
Show 10 quoted lines
> 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· May 24, 2013, 16:18 UTC · re: SZEDER Gábor · lore

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.

Antoine Pelisse· May 24, 2013, 16:22 UTC · re: Ramkumar Ramachandra · lore

Re: [QUERY] How do you sort completions?

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.

Ramkumar Ramachandra· May 24, 2013, 18:26 UTC · re: Antoine Pelisse · lore

Re: [QUERY] How do you sort completions?

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.

Marc Khouzam· May 24, 2013, 18:20 UTC · re: Ramkumar Ramachandra · lore

RE: [QUERY] How do you sort completions?

Show 16 quoted lines
> -----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 :)

← back to recent threads