KyL
KyL

Reputation: 1077

How to implement Tab completion in Bash C source code

As a beginner to Linux, I am researching the source of Bash and very interested to Tab completion.

On my opinion, there are two possible implementation of Tab completion.

  1. Bash get char '\t' from TTY and call corresponsable completion function to do something based on the chars you have typed.

  2. Tab keystroke fire a signal(event?) to bash. Then bash handle this signal and call back to a completion handler.

I made some searching and look up relevant code from Bash source http://sources.debian.net/src/bash/4.3-7 , but I cant find any code related to handle Tab signal or get char '\t' from TTY.

Anyone know the mechanism of Tab completion and can point the location of relevant code? Thanks.

Upvotes: 2

Views: 1556

Answers (1)

chepner
chepner

Reputation: 531615

Every keystroke you type at a bash prompt is bound to a function defined by the readline library. (Even the letters--upper and lowercase--are bound to the self-insert function, which simply puts the typed character on the command line. A key that is not bound to a function simply has no effect and is ignored.) The Tab key, by default, is bound to the complete function. The effect of the complete function is to attempt completion of the text immediately prior to the current cursor position (the point, in readline parlance). From the bash man page.

complete (TAB) Attempt to perform completion on the text before point. Bash attempts completion treating the text as a variable (if the text begins with $), username (if the text begins with ~), hostname (if the text begins with @), or command (including aliases and functions) in turn. If none of these produces a match, file- name completion is attempted.

(Programmable completion, I believe, is attempted in place of filename completion when applicable.)

Upvotes: 3

Related Questions