Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would one implement bash-like tab completion?

I'm trying to determine how the system prints characters to standard input -- that is, how it prints characters which the user can delete and which are considered input if the user hits "Enter."

I happen to be using C, but I would be very surprised if the solution were language-dependent.

Thanks for any insights! : D

like image 776
zslayton Avatar asked Oct 07 '09 17:10

zslayton


People also ask

How do you implement tab completion?

If you want completion within your program, you normally have to find matches starting with a particular string. This might be done by populating all possible values in a std::map, then using lower_bound and/or upper_bound to find the currently matching completions.

How does tab completion work bash?

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence. [1] If multiple completions are possible, then [Tab] lists them all. Let's see how it works. Tab completion also works for variables and path names.

Does bash have tab completion?

Bash completion is a functionality through which Bash helps users type their commands more quickly and easily. It does this by presenting possible options when users press the Tab key while typing a command.


1 Answers

As iny says, bash uses readline for its input. The source is available here, and there's a file called complete.c.

To answer your question, I don't think they're actually printed to standard input. Readline contains some kind of buffer for the contents of the line the user is editing, and completion prints into this. When the user presses enter, the contents of the buffer are sent to whatever program wanted to read a line, and in the case of bash, passed along into standard input. (Readline doesn't do this - other programs which use readline might simply store the value into a string for later use.)

like image 91
Cascabel Avatar answered Oct 23 '22 00:10

Cascabel