Is it possible to make bash auto-completion look like in Cisco IOS shell?
I mean to add short descriptions for each completion, like this:
telnet 10.10.10. (TAB Pressed)
10.10.10.10 - routerA
10.10.10.11 - routerB
where 10.10.10.10 and 10.10.10.11 are possible completions and routerA & routerB just descriptions (not to be executed).
I know that bash can complete commands with "complete -W", but is it able to print descriptions for them?
Yes, but you need a bit of bash kung foo in order to build such system. The way completion usually works is by binding normal functions to the commands you want to complete. You can find some basic examples around to better understand how completion works, and start developing your completion functions. Also, if you happen to have the
bash-completion
package installed, you could search your system for a number of other examples that currently drive completion in your shell.You could also have a look at the completion section of the official bash manual.
EDIT
I tried some experiments, and my conclusion is now that you can't do exactly what you're after: bash doesn't support help text next to
complete
results. What you can do is to add the legend for the provided completing words. This can be done either in a bash function_myfoo
to be used ascomplete -F _myfoo
, or a command viacomplete -C myfoo
, which prints out the legend before completing.The main difference is that using a function you're bound to Bash, while commands can be written in any language you choose, as long as it's able to set the required environment variables.
Here's a little example:
After some research I've found a solution. I don't know how it looks in Cisco, but I know how it works in Vyatta. The only flaw is that in this variant you have to press TAB 3 times to get a detailed help for the first time (first two times normal completion is printed). Once detailed help was shown, next TABs will toggle normal and detailed completion.
I even managed to reduce TAB pressings to only 2 using
COMP_TYPE
variable, but there is a problem that bash doesn't reprint current command line at the bottom line if some symbols were inserted after first TAB pressing, so there is a space for further research.I have a solution to this that does not require pressing TAB more than twice or echoing any extra information. The key is to check whether there is only one completion, then strip that completion down to the valid portion, usually by removing the largest matching suffix after your "comment" delimiter. To accomplish the OP's example:
This gives the exact output you're looking for, and when there is only one possible completion, the comment is stripped from it before completing.
I'd use conversion based on whether the number of candidates become one (as shown by @bonsaiviking) for simple cases and the following if I needed more flexibility in what I want to show the user.
Note: You could probably use
COMP_TYPE
to setFOR_DISPLAY
in Bash 4.x but I needed to support Bash 3.x as well.This behaves as follows:
Tab
TabTab
Inspired from https://github.com/CumulusNetworks/NetworkDocopt
The basic trick is to print help text, PS1 (expanded) and the original command, to
stderr
, and then print the completions options tostdout
.Here is the snippet to source in bash to like a completion function to
telnet
. It will call a ruby script (calledp.rb
) to generate the actual completion output.Here is an implementation of
p.rb
:Example: