This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/autocomplete.sh

68 lines
1.4 KiB
Bash
Raw Normal View History

# bash autocomplete.
2023-10-30 03:49:10 +03:00
# usage: _foo() { _autocomplete "{foo,bar}" } ; complete -F _foo foo
# there are also options like -o nospace. see man for more info.
2023-10-23 03:31:00 +03:00
_autocomplete()
2023-10-23 00:24:36 +03:00
{
local IFS=$'\n'
2023-10-30 03:49:10 +03:00
local commands="${@}"
2023-10-23 03:31:00 +03:00
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
2023-10-23 03:31:00 +03:00
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
}
# autocomplete only first argument.
_autocomplete_first()
{
local IFS=$'\n'
2023-10-30 03:49:10 +03:00
local commands="${@}"
2023-10-23 03:31:00 +03:00
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
2023-10-23 03:31:00 +03:00
if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
fi
}
# Autocomplete by grepping file names.
2023-10-30 14:22:24 +03:00
_autocomplete_grep()
{
local IFS=$'\n'
2023-10-30 14:22:24 +03:00
COMPREPLY=()
local pattern="${1}"
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
COMPREPLY=( $(compgen -W "$(ls | grep -E ${pattern})" -- ${cur}) )
return 0
}
2023-10-23 03:31:00 +03:00
# autocomplete nested program.
_autocomplete_nested()
{
# local IFS=$'\n'
2023-10-23 03:31:00 +03:00
local cur prev words cword split i
_init_completion -s || return
for ((i = 1; i <= cword; i++)); do
if [[ ${words[i]} != -* ]]; then
local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
local root_command=${words[i]}
2023-10-30 03:49:10 +03:00
_command_offset ${i}
2023-10-23 03:31:00 +03:00
return
fi
done
2023-10-23 00:24:36 +03:00
}