如何从其他路径启用bash选项卡?

时间:2012-09-21 15:39:17

标签: bash autocomplete

我想启用bash tab-complete来查找目录,但不能查找当前目录。

例如,如果我这样做:

$ ls $P
dirs/ are/ here/
$ cd /not/the/P/path
$ ls
other/ stuff/
$ myProg <tab>
dirs/ are/ here

这改变了通常的行为,我通常会在当前目录中看到文件。

尽职调查:我能想到的最好的是:

_myProg ()
{
  local cur

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

  if [ "${P}x" = "x" ]; then
    return 1
  fi

  case "$cur" in
    *)
      pth=${P}/$( echo $cur | egrep -o "^.*/[^/]*$" )
      COMPREPLY=( $( compgen -W "$( cd $pth && ls -1d "$cur"* 2>/dev/null -- "$cur" )" ) )
      ;;
  esac

  return 0
}
complete -o nospace -F _myProg myProg

最初显示目录,但不允许我深入到我想要的目录(如ls工作)。

2 个答案:

答案 0 :(得分:0)

$CDPATH对您有帮助吗?请参阅Advanced Bash Scripting Guide

答案 1 :(得分:0)

_myProg()
{
    COMPREPLY=($(cd $P; compgen -f $2))
}
complete -onospace -F_myProg myProg