Every time I type cd folder or cd .. I would like it to immediately run ls after.
Is there a way to do that?
Every time I type cd folder or cd .. I would like it to immediately run ls after.
Is there a way to do that?
Reposting the answer from another post
Put this in .zshrc to create alias for cl:
#print contents after moving to given directory
cl()
{
cd $@
ls
}
To override cd (not recommended), put this in .zshrc:
#print contents after moving to given directory
cd()
{
builtin cd $@
ls
}
I strongly recommend against overriding cd itself but essentially what you would like to do is:
alias cdl='cd $*; ls'
Here cdl is the name of your new command (it could also be cd if you really insist) and then you are assigning it to another command, which is itself a sequence of two commands:
cd $* where $* refers to all the arguments you are passing to cdlls
You could condition the execution of ls on the success of cd i.e. only run it if cd is given a valid folder and just do nothing otherwise:alias cdl='cd $* && ls'
UPDATE
@Cyrus, you are right, this doesn't work from within an alias (though it does if you just run from from the command line directly). This works though:
alias cdl='ls $*; cd $*'