drmanitoba
drmanitoba

Reputation: 741

ZSH: automatically run ls after every cd

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?

Upvotes: 61

Views: 20070

Answers (4)

blessdog
blessdog

Reputation: 1

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.

Upvotes: 0

ZyX
ZyX

Reputation: 53604

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
}

Upvotes: 78

John Eikenberry
John Eikenberry

Reputation: 1229

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.

Upvotes: 27

RemakingEden
RemakingEden

Reputation: 312

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

Upvotes: 6

Related Questions