Bash 用“@”符号完成
Bash complete with "@"-sign
我正在尝试为 scp 创建自动完成脚本。
该脚本从我的 .ssh/config 文件中读取用户和主机名
我的 .ssh/config 文件看起来像:
Host host1
HostName host1
User userA
port 22
Host host2
HostName host2
User userB
port 22
Host host3
HostName host3
User userB
port 22
我的 .autocomplete_scp.sh 文件是:
# 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= ; next } { print "@"o}'
}
complete -W "$(_scp_completion)" scp
我在我的 bashrc 中获取了这个文件。
现在,当我输入 userA
并按 Tab 时,自动完成功能会给我 userA@host1
。当我输入 userB
并点击 Tab 时,自动完成功能会给我 userB@
,但我无法获得完整的字符串(userB@host2
或 userB@host3
).
当我键入 userA@h
并按两次 Tab 按钮时,它也不起作用。所以它似乎因为 @
标志而卡住了。
(当我从 _scp_completion 函数中删除 @
符号时,它工作正常。)
有解决这个问题的想法吗?谢谢!
使其工作的简单方法是从 $COMP_WORDBREAKS
中删除 '@'
,否则 Bash 将自行处理 @
。你可以这样尝试:
COMP_WORDBREAKS=${COMP_WORDBREAKS//@}
complete -W 'userA@host1 userB@host2 userB@host3' scp
根据 bash 文档:
COMP_WORDBREAKS
The set of characters that the readline library treats as word separators when performing word completion.
我正在尝试为 scp 创建自动完成脚本。 该脚本从我的 .ssh/config 文件中读取用户和主机名 我的 .ssh/config 文件看起来像:
Host host1
HostName host1
User userA
port 22
Host host2
HostName host2
User userB
port 22
Host host3
HostName host3
User userB
port 22
我的 .autocomplete_scp.sh 文件是:
# 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= ; next } { print "@"o}'
}
complete -W "$(_scp_completion)" scp
我在我的 bashrc 中获取了这个文件。
现在,当我输入 userA
并按 Tab 时,自动完成功能会给我 userA@host1
。当我输入 userB
并点击 Tab 时,自动完成功能会给我 userB@
,但我无法获得完整的字符串(userB@host2
或 userB@host3
).
当我键入 userA@h
并按两次 Tab 按钮时,它也不起作用。所以它似乎因为 @
标志而卡住了。
(当我从 _scp_completion 函数中删除 @
符号时,它工作正常。)
有解决这个问题的想法吗?谢谢!
使其工作的简单方法是从 $COMP_WORDBREAKS
中删除 '@'
,否则 Bash 将自行处理 @
。你可以这样尝试:
COMP_WORDBREAKS=${COMP_WORDBREAKS//@}
complete -W 'userA@host1 userB@host2 userB@host3' scp
根据 bash 文档:
COMP_WORDBREAKS
The set of characters that the readline library treats as word separators when performing word completion.