Bash 为什么这个自动完成功能不能在Linux中自动完成?

Bash 为什么这个自动完成功能不能在Linux中自动完成?,bash,ubuntu,autocomplete,Bash,Ubuntu,Autocomplete,在我的bashrc中。我正在尝试按如下方式bash完成命令scp function _scp_complete { COMPREPLY="" COMPREPLY+=( $(cat ~/.ssh_complete ) ) COMPREPLY+=( $( find . ! -name . -prune -type f ) ) } complete -F _scp_complete scp 这个想法是,当按下scp[tab]时,我会看到当前目录中的所有文件以及文本文件~/.ssh\u c

在我的bashrc中。我正在尝试按如下方式bash完成命令
scp

function _scp_complete
{
  COMPREPLY=""
  COMPREPLY+=( $(cat ~/.ssh_complete ) )
  COMPREPLY+=( $( find . ! -name . -prune -type f ) )
}
complete -F _scp_complete scp
这个想法是,当按下
scp[tab]
时,我会看到当前目录中的所有文件以及文本文件
~/.ssh\u complete
中列出的单词。假设此文件包含以下条目:

alex@192.0.0.1 alex@192.0.0.2

所需的行为如下:我键入
scp alex@[TAB]
,然后TAB completion将命令“完成”到scpalex@192.0.0. 自动,因为只有两个可能的参数以alex@开头(假设currect工作目录中没有类似的命名文件):

我在所描述的实现中得到的行为如下:我键入
scp alex@[TAB]
,而TAB completion不会完成任何操作,但会在命令下面列出所有可能的参数:

>scp alex@[TAB]
  alex@192.0.0.1 alex@192.0.0.1 file1 Music Pictures ./.emacs <ALL files in the current directory>
>scp alex@
>scp alex@[TAB]
alex@192.0.0.1 alex@192.0.0.1文件1音乐图片/.emacs
>阿历克斯@

如何修复函数以获得所需的行为

您需要使用
COMP_WORDS
数组来获取当前键入的单词。然后使用
compgen
命令根据原始单词列表生成可能的补全

请尝试以下操作:

_scp_complete()
{
  local cur=${COMP_WORDS[COMP_CWORD]}
  COMPREPLY=( $(compgen -W "$(< ~/.ssh_complete) $( find . ! -name . -prune -type f )" -- $cur) )
}
complete -F _scp_complete scp
\u scp\u complete()
{
本地cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen-W“$(<~/.ssh_complete)$(find.!-name.-prune-type f)”--$cur)
}
完成-F_scp_完成scp
有关更多详细信息,请查看此博客帖子:

请注意,我认为这种完成方式不适用于名称中带有空格的文件


还请注意,使用
$(
从文件中提取文本比使用
$(
从文件中提取文本更有效。

您需要使用
COMP_WORDS
数组来获取当前键入的单词。然后使用
compgen
命令根据原始单词列表生成可能的补全

请尝试以下操作:

_scp_complete()
{
  local cur=${COMP_WORDS[COMP_CWORD]}
  COMPREPLY=( $(compgen -W "$(< ~/.ssh_complete) $( find . ! -name . -prune -type f )" -- $cur) )
}
complete -F _scp_complete scp
\u scp\u complete()
{
本地cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen-W“$(<~/.ssh_complete)$(find.!-name.-prune-type f)”--$cur)
}
完成-F_scp_完成scp
有关更多详细信息,请查看此博客帖子:

请注意,我认为这种完成方式不适用于名称中带有空格的文件


还请注意,使用
$(
从文件中提取文本比使用
$(
更有效。

感谢您的帮助和解释!谢谢你的帮助和解释!