Bash Completion: what can we do about it, what in the future

Bash allows you to enter command names and file names in arguments using the TAB key. But why not the usual options for teams? Why not, a completion system that tells you what the option does?

I heard about programmable termination ... but don't understand where it fits.

So my question is: is there a way to achieve what I ask? Other tools for combining with Bash might be .. or something else?

+3
source share
7 answers

If you want an even more frightening ending, check out zsh . It is very similar to bash and takes some of the nice features from ksh, tcsh, etc. It also fixes several annoying bash errors.

It can do pretty cool things, including filling out remote file names for scp (if you have key-based authentication installed) and mini-documentation at the end of the tab. For example, I was pleasantly surprised when I put a jar the other day:

[steven@sexy:~]% jar <tab>
0  -- store only without using ZIP compression
M  -- do not create manifest file
etc...
+5
source

Here's the official bash manual page: Bash Reference Guide: Programmable Termination

+2
source

OS X bash MacPorts, . /opt/local/etc/bash_completion.d .

+2

bash: .

+1

Bash . . Debian bash , .

:

alanhaggai@neon:~$ tar
A  c  d  r  t  u  x
+1

- .

, , .

, :

_cd(){
    # Backup old nullglob setting
    local shoptbakup="`shopt -p nullglob`"
    shopt -s nullglob

    local cur opts i opt
    local IFS=$'\n'
    cur="${COMP_WORDS[COMP_CWORD]}"

    # Home directory substitution
    if [[ "${cur:0:1}" == '~' ]]; then
        cur="$HOME/${cur:1}"
    fi

    if [[ $cur == */* ]]; then
        opts=(${cur}*/)
    else
        opts=(*${cur}*/)
    fi

    # Prevent trailing//
    i=0
    for opt in "${opts[@]}"; do
        #opts[$i]=${opt%${opt##*[!/]}}
        opts[$i]=${opt%/}
        i=$((i+1))
    done

    # Restore nullglob setting
    eval "$shoptbakup" 2>/dev/null

    COMPREPLY=("${opts[@]}")
}
complete -o filenames -o bashdefault -o nospace -F _cd cd

, :

$ ls
test 6/  test 7/  test 8/  test1/  test2/  test3/  test4/  test5/
$ cd 7[TAB]

:

$ cd test\ 7/

, , , , , script , . :

http://fahdshariff.blogspot.com/2011/04/writing-your-own-bash-completion.html

:

, script, listanimals. ,

# complete sendevent
_listanimals()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    if [[ $prev == "--sort-direction" ]]; then
        COMPREPLY=( $( compgen -W "horizontal vertical" -- $cur ) )
        return 0
    elif [[ $prev == "--type" ]];then
        COMPREPLY=( $( compgen -W "mammals reptiles birds fish marsupials insects misc" -- $cur ) )
        return 0
    elif [[ $prev == "--size" ]]; then
        COMPREPLY=( $( compgen -W "tiny small medium large huge" -- $cur ) )
        return 0
    fi


    # completing an option
    if [[ "$cur" == --* ]] || [[ $cur == "" ]]; then
        COMPREPLY=( $( compgen -W "--sort-direction --type --size" -- $cur ) )
    fi
}
complete -F _listanimals listanimals

:

$ listanimals --[TAB][TAB]
--size            --sort-direction  --type
$ listanimals --t[TAB]

:

$ listanimals --type

:

$ listanimals --type [TAB][TAB]
birds       fish        insects     mammals     marsupials  misc        reptiles

And I'm sure you can figure out the rest. This is a working example, so if you want to try just copy / paste it into a file (for example, sample.sh) and specify it in bash source sample.sh. You really don't need to have script names listanimals.

Hope someone finds this helpful because I found the pain to understand.

0
source

Source: https://habr.com/ru/post/1725534/


All Articles