I am trying to create an autocomplete script for scp
.
The script reads the user and hostname from my .ssh/config
file.
My .ssh/config
file looks like:
Host host1
HostName host1
User userA
port 22
Host host2
HostName host2
User userB
port 22
Host host3
HostName host3
User userB
port 22
My .autocomplete_scp.sh
file is:
# SSH
function _scp_completion() {
pcregrep -o -M 'HostName [a-zA-Z.]+[\n\t\s]+User [a-zA-Z]+'
$HOME/.ssh/config | awk 'NR % 2 == 1 { o=$2 ; next } { print $2"@"o}'
}
complete -W "$(_scp_completion)" scp
I source this file in my bashrc.
Now when I type userA
and press Tab, the autocomplete function will give me userA@host1
. When I type userB
and hit Tab, the autocomplete function will give me userB@
, but I am not able to get the full string (userB@host2
or userB@host3
).
It also doesn't work when I type userA@h
and hit the Tab button twice. So it seems to get stuck due to the @
sign.
(When I remove the @
sign from the _scp_completion
function it works fine.)
Any ideas to fix this? Thanks!
The easy way to make it work is to remove the @
char from $COMP_WORDBREAKS
or Bash would handle @
by itself.
COMP_WORDBREAKS=${COMP_WORDBREAKS//@}
complete -W 'userA@host1 userB@host2 userB@host3' scp
According to man bash
:
COMP_WORDBREAKS
The set of characters that the readline library treats as word separators when performing word completion.