aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/completion.bash
blob: a921770be26d37406c3ef2c1e9fbb84e101fa7cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# builtin with builtins
complete -b builtin

# cd/pushd with directories
complete -d cd pushd

# command/hash/type with commands
complete -c command hash type

# help with topics
complete -A helptopic help

# set with options
complete -A setopt set

# shopt with shell options
complete -A shopt shopt

# sudo with commands (and not files)
complete -c sudo

# unset with shell variables and functions
complete -v -A function unset

# ssh/sftp/ssh-copy-id with config hostnames
_ssh() {
    local word config hosts option value
    word=${COMP_WORDS[COMP_CWORD]}
    config=$HOME/.ssh/config
    hosts=()

    # Bail if the configuration file is illegible
    if [[ ! -r $config ]]; then
        return 1
    fi

    # Read hostnames from the file, no asterisks
    while read -r option value _; do
        if [[ $option == Host && $value != *'*'* ]]; then
            hosts[${#hosts[@]}]=$value
        fi
    done < "$config"

    # Generate completion reply
    COMPREPLY=( $(compgen -W "${hosts[*]}" -- "$word") )
}
complete -F _ssh ssh sftp ssh-copy-id

# scp/rsync with local files and hostnames (colon suffixes)
_scp() {
    _ssh
    COMPREPLY=( "${COMPREPLY[@]/%/:}" )
}
complete -f -F _scp scp