bash 4.4 with 'complete'
jq - for json parsing
https://stedolan.github.io/jq/download/
bash 4.4 is a bit annoying for macos users. You'll need to upgrade the default bash (v3+) with a version from the early 90s.
install brew - https://brew.sh/
brew upgrade bash
brew install bash-completion
Ubuntu Et Al - you are set to go.
- clone this repo
- tab-completion-sample-functions.sh contains examples for adding tab completion to an existing program - kt (https://github.com/fgeller/kt) AND a bash function tabTester
- the json config for both apps is at the top of the file. I inlined the config in the shell script for completeness. It config consists of a "key" (aka the function/program name for completion) and the program arguments and the function or static data the completion is mapped to. The key is case sensitive. It must match exactly.
- After getting your head around how to configure completion, it's time to create your own script and add a config for each program/fn you want tab completion for. The configuration can be in the same json config. It is not necessary to sep. configs the configurations. It's just JSON, comman sep. the commands and it'll work.
- with the json written, it's time to code the bash functions that provide the completion data. There are two ways to provide completion results. You can define a static function (aka the data will cached for the duration fo the shell session) and(!) as dynamic data where the function is called very time you press____. The output of the functions that provide tab completion is very simple - each option returned is separated on it's own line in the output (carriage return delimited ( '\n' ). Checkout _tab-completion-sample-functions.sh for a complete example with additional documentation.
- With the json config and functions providing tab completion written, source them (and tab-completion-lib.sh) in your .bashrc etc.
For an example of dynamic generated data, check out the tabTester function json config in _tab-completion-sample-functions.sh. The option --random is bound to the shell function _sillyRandomFn. You can test this by sourcing the file and typing
tabTester --random
This will print two random strings to match upon.
tab-completion-sample-functions.sh also contains many examples of both functions that provide static data and inlined completion results. I recommend checking out kt's -offsets config for an example of inlined tab completion data. The values are placed directly in the data property, sep. by \n
source tab-completion-sample-functions.sh
To make functions available to your existing shell, you source a file NOT execute it
yay(?)
grouping your completion configs by task and not individual function makes a lot of sense. Do that.
yes I know this stinks. Without a better in memory caching solution this is how it is. use an fn function instead (see --random in tabTester) and do your own cachine.
ENJOY! or don't I don't care...