ZSH:每次 cd 后自动运行 ls

发布于 2024-09-28 04:51:20 字数 128 浏览 1 评论 0原文

所以我现在已经让 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 技术交流群。

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

发布评论

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

评论(4

浅沫记忆 2024-10-05 04:51:20

编辑:
查看文档(zshbuiltinscd 内置说明或 钩子函数)我找到了一个更好的方法:它使用 chpwd 函数:

function chpwd() {
    emulate -L zsh
    ls -a
}

或使用 chpwd_functions 数组:

function list_all() {
    emulate -L zsh
    ls -a
}
chpwd_functions=(${chpwd_functions[@]} "list_all")

将以下进入 .zshrc:

function cd() {
    emulate -LR zsh
    builtin cd $@ &&
    ls -a
}

EDIT:
After looking at documentation (zshbuiltins, description of cd builtin or hook functions) I found a better way: it is using either chpwd function:

function chpwd() {
    emulate -L zsh
    ls -a
}

or using chpwd_functions array:

function list_all() {
    emulate -L zsh
    ls -a
}
chpwd_functions=(${chpwd_functions[@]} "list_all")

Put the following into .zshrc:

function cd() {
    emulate -LR zsh
    builtin cd $@ &&
    ls -a
}

绾颜 2024-10-05 04:51:20

简短版本。

autoload -U add-zsh-hook
add-zsh-hook -Uz chpwd (){ ls -a; }

快速解释这里发生的事情。

autoload -U add-zsh-hook

此行基本上只是加载贡献的函数 add-zsh-hook

add-zsh-hook -Uz chpwd (){ ls -a; }

每个特殊函数都有一个要调用的函数数组当该功能被触发时(例如通过更改目录)。此行向该数组添加一个函数。分解一下...

add-zsh-hook -Uz chpwd

这部分指定我们要向 chpwd 特殊函数添加一个新函数。 -Uz 通常是推荐的参数,它们被传递给用于函数参数的自动加载(即下一位)。

(){ ls -a; }

第二部分是函数。这就是通常所说的匿名函数。这是一个尚未指定名称的函数。它不需要名称,因为它只是在一个数组中。

Short version.

autoload -U add-zsh-hook
add-zsh-hook -Uz chpwd (){ ls -a; }

Quick explanation of what's going on here.

autoload -U add-zsh-hook

This line basically just loads the contributed function add-zsh-hook.

add-zsh-hook -Uz chpwd (){ ls -a; }

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...

add-zsh-hook -Uz chpwd

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).

(){ ls -a; }

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.

陌若浮生 2024-10-05 04:51:20

我不知道为什么会出现这种情况,或者这是否更好,但我发现这也适用于 .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

别再吹冷风 2024-10-05 04:51:20

chpwd()ls -a 是在命令行上一起使用的两个单独的命令。

chpwd() 在大多数 shell 中不是有效命令,但在 zsh shell 中,它是一个每当当前工作目录更改时就会调用的函数。它可用于定义每当用户导航到新目录时应采取的操作。

ls -a 是一个列出目录内容的命令,包括隐藏文件和目录(名称以点 . 开头的文件和目录)。 -a 选项告诉 ls 显示所有文件,包括隐藏文件。

因此,当一起使用时,chpwd() ls -a 会在每次当前工作目录发生更改时执行 ls -a 命令。例如,这可以用于在用户导航到某个目录时自动列出该目录的内容。不过,值得注意的是,chpwd() 不是标准命令,并非在所有 shell 中都可用,因此这种用法可能并非在所有情况下都有效。

chpwd() and ls -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 the zsh 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 tells ls to show all files, including hidden files.

So when used together, chpwd() ls -a would execute the ls -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 that chpwd() is not a standard command and is not available in all shells, so this usage may not work in all cases.

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