ZSH: automatically run ls after every cd
Asked Answered
zsh
B

4

60

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?

Breland answered 18/10, 2010 at 22:50 Comment(0)
D
77

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
}
Downwards answered 18/10, 2010 at 23:13 Comment(6)
This works really well, I forgot to mention I have auto_cd turned on so this only works when I explicitly call 'cd'. Is there a way to get this to behave this way any time I change directories?Breland
what is emulate -LR zsh for?Thermometer
@Thermometer man zshbuiltins. Resets some options (here: for the duration of current function), not really required here, but it is good to put emulate zsh/emulate -L zsh at the start of every function so that you will know that this function won’t ever break independent of whatever user has set. -R is an overkill, it resets all options.Downwards
This works for me and I want to continue using it, but one of my computers is really slow so I don't want to. How do I override this if I've sourced it from another dotfile? I'm assuming I just copy the function to the host-specific .zshrc and remove the ls -a.Alvinalvina
The last option has the disadvantage of appending list_all to chpwd_functions every time you source .zshrc resulting in extra calls to ls -a being triggeredSteelhead
use this to run git fetch --all --prune on a cd to a folder containing a .git directory!Uncloak
P
26

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.

Preconcerted answered 1/8, 2017 at 17:59 Comment(3)
Wow! This works, but it's so much dirty that I spent couple of hours digging man pages to understand. According to zsh docs, second line is supposed to assign a function body ls -a; to multiple names: add-zsh-hook, -Uz and chpwd (which it effectively does). But at the same time, it really executes original add-zsh-hook with all those arguments. Why? zsh.sourceforge.net/Doc/Release/…Communize
Note the advantage of this over the below use of chpwd is that this lets you add any number of new functions to one of the special functions. In addition to the ls mentioned here I also do things like set the terminal title and check for a local history file.Preconcerted
How do I disable this?Sutherlan
I
6

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

Ingrowth answered 10/3, 2019 at 15:53 Comment(0)
D
0

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.

Daphne answered 27/4, 2023 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.