限制zsh选项卡完成行为

时间:2017-05-30 16:31:30

标签: zsh zsh-completion

我的zsh有一些我不理解的完成功能,无法找到改变的地方。我有两个问题,我怀疑他们对我的问题有类似的“修复”。我用

初始化zsh完成系统
autoload -Uz compinit
compinit

获得高级完成功能,但如果没有compinit,我也会遇到以下问题。

首先:我的主目录中碰巧有一个名为mydir的目录,不幸的是,还有一个名为mydir的用户。当我想要更改到我的目录然后使用标签完成时,即

cd mydir/<TAB>

我获得了~myusername/mydir/的内容目录以及~mydir/的所有可用目录。我已经尝试过了

zstyle ':completion:*' users myusername

在我的.zlogin文件中,但它只会更改用户名本身的完成,而不会更改后续目录。是否有一些类似的开关来关闭其他用户的主目录的完成?或者,如果当前目录完成首先出现在完成菜单中,那将是很好的。

第二:我写了一个名为setup-file-with-a-long-name.sh的脚本,它位于我的主目录中。当我想通过

执行它时
source setup-file-with-a-long-name.sh

我从前几个字符开始,我按下<TAB>,我得到了许多可执行文件的列表,这些文件可能位于系统安装的$PATH上,但我不在乎关于所有这些文件,我只想让我的文件(当前目录中唯一的匹配)首先显示在菜单中并可通过<TAB> <TAB>访问,或者更好,在第一个{{1}之后接受}。 (如果我选择其中任何一个,它们无论如何都不起作用,因为源需要绝对路径,而不是文件名。因此,这是一种我不理解的行为,无法看到这对任何人来说都是有用的。 )

可能的解决方法:
1.写〜/明确 - 这是我想要避免的,因为我必须经常ssh到一个新的shell,并且想要开始导航而不考虑我是否在<TAB>。 2.不要使用$HOME - 好吧,我原则上喜欢上下文感知完成,我只是想根据我的需要调整它。

1 个答案:

答案 0 :(得分:0)

以下适用于bash,

man source -

source filename [arguments]
              Read  and  execute  commands  from filename in the current shell environment and return the exit status of the last command executed from filename.  If filename does not contain a slash, file
              names in PATH are used to find the directory containing filename.  The file searched for in PATH need not be executable.  When bash is not in posix mode, the current directory is searched  if
              no file is found in PATH.  If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.

禁用标志指令略高于sourcepath

的描述
       shopt [-pqsu] [-o] [optname ...]
              Toggle the values of variables controlling optional shell behavior.  With no options, or with the -p option, a list of all settable options is displayed, with an indication of
              whether or not each is set.  The -p option causes output to be displayed in a form that may be reused as input.  Other options have the following meanings:
              -s     Enable (set) each optname.
              -u     Disable (unset) each optname.
              -q     Suppresses normal output (quiet mode); the return status indicates whether the optname is set or unset.  If multiple optname arguments are given  with  -q,  the  return
                     status is zero if all optnames are enabled; non-zero otherwise.
              -o     Restricts the values of optname to be those defined for the -o option to the set builtin.
...
             sourcepath
                      If set, the source (.) builtin uses the value of PATH to find the directory containing the file supplied as an argument.  This option is enabled by default.

因此执行以下操作应从标签页完成中删除路径...

shopt -u sourcepath