Bash: Completion of word containing @

From FVue
Jump to: navigation, search

Problem

If bash is programmed to complete ssh with a list of user@hosts from ~/sshs, the @ gets replaced.
This is ~/sshs

foo@www.foo.com
foo@foo

This is in ~/.bashrc

_ssh() {
    COMPREPLY=( $(grep "^${COMP_WORDS[COMP_CWORD]}" ~/sshs) )
} # _ssh()
complete -F _ssh ssh

And this is what happens:

user:~> ssh f<TAB>       becomes (ok):       user:~> ssh foo@
user:~> ssh foo@<TAB>    becomes (problem):  user:~> ssh foofoo@

Solution

Turn off hostname completion:

shopt -u hostcomplete && complete -F _ssh ssh

Drawback of this solution is eh, that hostcompletion is turned off. To turn it on again:

shopt -s hostcomplete

Journal

20060408

Found a bash completing package which also completes user@host like this:

# This function provides simple user@host completion
#
_user_at_host() {
    local cur

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}

    if [[ $cur == *@* ]]; then
        _known_hosts
    else
        COMPREPLY=( $( compgen -u -- "$cur" ) )
    fi

    return 0
}
shopt -u hostcomplete && complete -F _user_at_host $nospace talk ytalk finger

20060409

When turning off hostcompletion, my code seems to work:

shopt -u hostcomplete

Comments

blog comments powered by Disqus