2

I am attempting to use bash-completion for URLs of the form "proto://host". Without the ":", I see expected behavior, with the ":" present, the URL comes out "faulty".

Bash completion has a function for hostnames, _known_hosts_real. It has a parameter -p PREFIX that I want to use.

How to reproduce "wrong" behavior:

You need at least one "known" host on your system:

E.g. echo "gdns.google 8.8.8.8" >> /etc/hosts

Run these commands for functioning example:

$ unset COMPREPLY 
$ _known_hosts_real -p 'http//' -- "gdn"
$ echo $COMPREPLY 
http//gdns.google

Non-working example:

$ unset COMPREPLY 
$ _known_hosts_real -p 'http://' -- "gdn"
$ echo $COMPREPLY 
//gdns.google

Note the only difference is the added colon in "http://". The problem here is the prefix has lost the protocol specifier and the colon. Is this expected behavior?

1

It is expected behaviour, as _known_hosts_real calls __ltrim_colon_completions, which removes prefixes terminated by colons. _known_hosts_real is intended for use with the ssh known hosts file, so you should probably define your own function on the basis of compgen -A hostname.

| improve this answer | |
  • thanks for tracing that. In fact, I want the known ssh hosts to be included. So instead of writing something entirely new with basically the same function, I decided to add a patch to _known_hosts_real to my .bashrc, which adds options "-S" (suffix), and "-t" (dont trim). – Alex Stragies Jul 7 '16 at 17:15
1

@Michael_Veers pointed out, that this is expected behavior (+1), so I should write my own function.

But instead I wrote a patch for the standard _known_hosts_real into my bashrc, that enables additional options:

  • -t Do not trim to left colon
  • -S <suffix> Add custom suffix

In .bashrc:

 . <(type _known_hosts_real | sed -e '
 1d;s/acF:p:/actS:F:p:/;
 s/^.*c)/\nS)\nsuffix=$OPTARG\n;;\nt)\nnotrim=1\n;;\nc)/;
 s/\(__lt\)/[ $notrim ]||\1/')

Now this works as expected:

$ unset COMPREPLY
$ _known_hosts_real -p "scp://" -S '/' -t -a gdn
$ echo $COMPREPLY
scp://gdns.google/
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.