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?
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
}
emulate -LR zsh
for? –
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 source
d it from another dotfile? I'm assuming I just copy the function to the host-specific .zshrc
and remove the ls -a
. –
Alvinalvina list_all
to chpwd_functions
every time you source .zshrc
resulting in extra calls to ls -a
being triggered –
Steelhead git fetch --all --prune
on a cd
to a folder containing a .git
directory! –
Uncloak 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.
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 ls
mentioned here I also do things like set the terminal title and check for a local history file. –
Preconcerted 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()
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.
© 2022 - 2024 — McMap. All rights reserved.