ZSH,如何将文件完成仅限为当前目录(例如bash)
让我们首先看看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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完成系统正在搜索
路径
,以找到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 thesource
command. The completion forls
doesn't usePATH
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
tabThis 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
tabOption 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 tocompinit
):unset '_comps[source]'
Now completion will fall back to the default version, which does not use the path.