ZSH:每次 cd 后自动运行 ls
所以我现在已经让 ZSH 做了所有这些很酷的事情,但是如果我可以让它在每次调用“cd”后隐式运行“ls -a”,那就太棒了。我认为这必须放在 .zlogin 文件或 .aliases 文件中,我只是不确定最好的解决方案是什么。想法?参考资料?
So I've got ZSH doing all this cool stuff now, but what would be REALLY awesome is if I could get it to run 'ls -a' implicitly after every time I call 'cd'. I figure this must go in the .zlogin file or the .aliases file, I'm just not sure what the best solution is. Thoughts? Reference material?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:
查看文档(
zshbuiltins
、cd
内置说明或 钩子函数)我找到了一个更好的方法:它使用chpwd
函数:或使用
chpwd_functions
数组:将以下进入 .zshrc:
EDIT:
After looking at documentation (
zshbuiltins
, description ofcd
builtin or hook functions) I found a better way: it is using eitherchpwd
function:or using
chpwd_functions
array:Put the following into .zshrc:
简短版本。
快速解释这里发生的事情。
此行基本上只是加载贡献的函数
add-zsh-hook
。每个特殊函数都有一个要调用的函数数组当该功能被触发时(例如通过更改目录)。此行向该数组添加一个函数。分解一下...
这部分指定我们要向
chpwd
特殊函数添加一个新函数。-Uz
通常是推荐的参数,它们被传递给用于函数参数的自动加载(即下一位)。第二部分是函数。这就是通常所说的匿名函数。这是一个尚未指定名称的函数。它不需要名称,因为它只是在一个数组中。
Short version.
Quick explanation of what's going on here.
This line basically just loads the contributed function
add-zsh-hook
.Each of the special functions has an array of functions to be called when that function is triggered (like by changing directory). This line adds a function to that array. To break it down...
This part specifies that we are adding a new function to the
chpwd
special function. The-Uz
are generally the recommended arguments for this, they are passed to the autoload used for the function argument (ie. the next bit).This second part is the function. It is what is generally referred to as an anonymous function. That is a function that hasn't been assigned a name. It doesn't need a name as it is just going in an array.
我不知道为什么会出现这种情况,或者这是否更好,但我发现这也适用于 .zshrc。似乎比大多数答案都短得多,尽管它可能缺少一些我不明白的东西。
chpwd() ls -a
I don't know why this is the case or if this is better however I have found that this also works in .zshrc. Seems a lot shorted than most of the answers although maybe it is missing something I don't understand.
chpwd() ls -a
chpwd()
和ls -a
是在命令行上一起使用的两个单独的命令。chpwd()
在大多数 shell 中不是有效命令,但在zsh
shell 中,它是一个每当当前工作目录更改时就会调用的函数。它可用于定义每当用户导航到新目录时应采取的操作。ls -a
是一个列出目录内容的命令,包括隐藏文件和目录(名称以点.
开头的文件和目录)。-a
选项告诉ls
显示所有文件,包括隐藏文件。因此,当一起使用时,
chpwd() ls -a
会在每次当前工作目录发生更改时执行ls -a
命令。例如,这可以用于在用户导航到某个目录时自动列出该目录的内容。不过,值得注意的是,chpwd()
不是标准命令,并非在所有 shell 中都可用,因此这种用法可能并非在所有情况下都有效。chpwd()
andls -a
are two separate commands that are being used together on the command line.chpwd()
is not a valid command in most shells, but in thezsh
shell, it is a function that is called whenever the current working directory changes. It can be used to define actions that should be taken whenever the user navigates to a new directory.ls -a
is a command that lists the contents of a directory, including hidden files and directories (those whose names start with a dot.
). The-a
option tellsls
to show all files, including hidden files.So when used together,
chpwd() ls -a
would execute thels -a
command every time the current working directory changes. This could be used to automatically list the contents of a directory whenever the user navigates to it, for example. However, it is worth noting thatchpwd()
is not a standard command and is not available in all shells, so this usage may not work in all cases.