From 52c3c0b5e7efc9db5fe5dc91344873a56a93544a Mon Sep 17 00:00:00 2001 From: home Date: Mon, 23 Oct 2023 02:27:45 +0300 Subject: [PATCH] bash : simplify autocomplete. --- document/linux/config/bash/module/autocomplete.sh | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/document/linux/config/bash/module/autocomplete.sh b/document/linux/config/bash/module/autocomplete.sh index e6cad74..1d4beed 100644 --- a/document/linux/config/bash/module/autocomplete.sh +++ b/document/linux/config/bash/module/autocomplete.sh @@ -1,13 +1,8 @@ # bash autocomplete. -# usage: complete -F _foo foo +# usage: _foo () { autocomplete "{foo,bar}" } ; complete -F _foo foo # there are also options like -o nospace. see man for more info. +# from man: The shell function function is executed in the current shell environment. When the function is executed, the first argument ($1) is the name of the command whose arguments are being completed, the second argument ($2) is the word being completed, and the third argument ($3) is the word preceding the word being completed on the current command line. When it finishes, the possible completions are retrieved from the value of the COMPREPLY array variable. autocomplete() { - local commands="$@" - local cur prev - - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) ) - return 0 + COMPREPLY=( $(compgen -W "${@}") ) }