CmdAutoComp is a tool for simplifying the implementation of auto-completion of commands. You can watch the demo video first to check if it meets your needs. It relies on the complete command on the Linux shell and a command tree file (with the suffix .comp) designed by me. After installing this tool, when you want to auto-complete a command, you just need to make a .comp file like example.
You can refer to example to try it out.
- Append the contents of the
bashrcto the.bashrcin your home directory. And replacepath/to/shell-cmd-comp.shwith the path where you putshell-cmd-comp.sh. - Run
source ~/.bashrc. This will create a directory named.shellCmdComp.dand export a environment variableSHELL_CMD_COMP_DIR. - In this step, you can create a file with the
.compsuffix and put it in the~/shellCmdComp.ddirectory. This file is the key to auto-complete commands. Refer to .comp File to get the details. - Run
complete -F _shell_cmd_complete<cmd-name>. - Test your command.
Sub-commands are organized into a tree in the .comp file. The coding rules for the .comp file and the tree are as follows.
- The file name before the suffix must be the same as your main command.
- The first line is reserved. You can write anything or nothing on this line.
- Subsequent lines are used to enumerate sub-commands. These sub-commands are organized in a tree. Each child command is indented one tab back from the parent command.
- The first-level sub-command should be indented one tab from the beginning of the line
- Each level of command nodes should start with a dedicated line that determines how to handle the command nodes at the current level. Only the following symbols can be used for dedicated lines.
&All sub-commands of the current level can be used together.@Only one sub-command of the current level can be used.*Current sub-command supports any input parameter.
There is a command test which supports the following sub-commands.
--option1 param11--option1 param12--option1 param12 <anything>--option1 param13 param131--option1 param13 param132--option2 param21--option2 param22--option3 param31--option3 param32--option1,--option2and--option3can be used together.param11,param12andparam13under--option1can be used together.param21andparam22under--option2can be used together.
Then the name of the .comp file must be test.comp. The contents of this file are as follows.
Anything
&
--option1
&
param11
param12
*
param13
@
param131
param132
--option2
&
param21
param22
--option3
@
param31
param32
The command log is shown below.
source@debian:cmdAutoComp$ ls
1 bashrc LICENSE README.md shell-cmd-comp.sh test test.comp
source@debian:cmdAutoComp$ pwd
/home/source/opensource/mine/cmdAutoComp
source@debian:cmdAutoComp$ vim ~/.bashrc
source@debian:cmdAutoComp$ tail -1 ~/.bashrc
source /home/source/opensource/mine/cmdAutoComp/shell-cmd-comp.sh
source@debian:cmdAutoComp$ source ~/.bashrc
source@debian:cmdAutoComp$ ls ~/.shellCmdComp.d
source@debian:cmdAutoComp$ vim ~/.shellCmdComp.d/test.comp
source@debian:cmdAutoComp$ ls ~/.shellCmdComp.d
test.comp
source@debian:cmdAutoComp$ cat ~/.shellCmdComp.d/test.comp
Anything
*
--option1
*
param11
param12
param13
@
param131
param132
--option2
*
param21
param22
--option3
@
param31
param3
source@debian:cmdAutoComp$ cat test
#!/bin/bash
source@debian:cmdAutoComp$ chmod +x test
source@debian:cmdAutoComp$ complete -F _shell_cmd_complete ./test
source@debian:cmdAutoComp$ test
1 .git/ README.md test
bashrc LICENSE shell-cmd-comp.sh
source@debian:cmdAutoComp$ ./test --option
--option1 --option2 --option3