ZSH,如何将文件完成仅限为当前目录(例如bash)

发布于 2025-02-09 19:21:29 字数 1683 浏览 0 评论 0原文

让我们首先看看Bash的作用:

我在其中只有1个文件的目录中:

(bme) me@test_399_zsh_comp$ tree
.
└── common.sh

0 directories, 1 file
(bme) me@test_399_zsh_comp$

现在,如果我键入source co <​​/code>和TAB,则BASH仅具有common.sh.sh and使用它。

$ source co <​​/code>(tab)=&gt; source common.sh

而ZSH“有益地”想要引入其他地方的文件(不是命令)。

test_399_zsh_comp%源co

(TAB)

test_399_zsh_comp % source co
code@                  comm*                  compress*              convertfilestops*      corelist-5.28*
code2flow@             command*               compression_tool*      convertformat*         corelist-5.30*
codesign*              common.sh              config_data*           convertsegfilestopdf*  corelist5.18*
codesign_allocate*     common.vuepress.sh     config_data5.30*       convertsegfilestops*   corelist5.30*
col*                   commonize@             configurations/        converttopdf*          coverage*
colcrt*                compare*               conjure*               converttops*           coverage-3.10*
colldef*               compare.txt            convert*               cookiecutter*          coverage3*
colrm*                 compile_et*            convert-keyring*       coreaudiod*            
column*                composite*             convertfilestopdf*     corelist@ 

有没有办法将完成与BASH保持一致?具体来说,我不想在当前目录之外寻找文件。

奇怪的是,ls的行为更像我在ZSH下想要的内容,仅完成common.sh。在ls ZSH下的参数完成之间是否有特别的牢记?

ls co <​​/code>(tab)=&gt; ls common.sh

Let's first see what bash does:

I am in a directory with only 1 file in it:

(bme) me@test_399_zsh_comp$ tree
.
└── common.sh

0 directories, 1 file
(bme) me@test_399_zsh_comp$

Now if I type source co and tab, bash only has common.sh and uses it.

$ source co (tab) => source common.sh

whereas zsh "helpfully" wants to bring in files (not commands) found elsewhere.

test_399_zsh_comp % source co

(tab)

test_399_zsh_comp % source co
code@                  comm*                  compress*              convertfilestops*      corelist-5.28*
code2flow@             command*               compression_tool*      convertformat*         corelist-5.30*
codesign*              common.sh              config_data*           convertsegfilestopdf*  corelist5.18*
codesign_allocate*     common.vuepress.sh     config_data5.30*       convertsegfilestops*   corelist5.30*
col*                   commonize@             configurations/        converttopdf*          coverage*
colcrt*                compare*               conjure*               converttops*           coverage-3.10*
colldef*               compare.txt            convert*               cookiecutter*          coverage3*
colrm*                 compile_et*            convert-keyring*       coreaudiod*            
column*                composite*             convertfilestopdf*     corelist@ 

Is there a way to bring completions more in line with bash? Specifically, I don't want to look for files outside the current directory.

Strangely enough, ls behaves more like what I want under zsh, completing to just common.sh. Is there something particular to keep in mind between ls and source argument completion under zsh?

ls co (tab) => ls common.sh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-02-16 19:21:29

完成系统正在搜索路径,以找到source命令的匹配脚本和文件。 ls的完成不使用路径,因为这是没有意义的 - 这就是为什么您在那里看到不同的结果。

改变行为的几种方法:

选项1

将文件名前缀./

source ./co tab

这将绕过路径搜索,因此,它只会在当前目录中找到文件。在许多其他情况下,这也是一个有用的习惯,可以具有一些安全益处。

它还将与一起工作,命令的较短形式:

。 ./co 选项卡

选项2

删除source> source命令的特定完成。一种方法是将此行添加到〜/.zshrc,在任何其他完成设置之后的某个地方(例如调用compinit):

unset> unset'_comps [source] '

现在完成将返回到不使用路径的默认版本。

The completion system is searching the PATH to find matching scripts and files for the source command. The completion for ls doesn't use PATH since that wouldn't make sense - that's why you're seeing different results there.

A couple of ways to change the behavior:

Option 1

Prefix the filename with ./:

source ./co tab

This will bypass the path search, so it's only going to find files in the current directory. It's also a useful habit in many other contexts and can have some security benefits.

It will also work with ., the shorter form of the command:

. ./co tab

Option 2

Remove the specific completion for the source command. One way is to add this line to ~/.zshrc, somewhere after any other completion setup (e.g. a call to compinit):

unset '_comps[source]'

Now completion will fall back to the default version, which does not use the path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文