automatically run ls after changing path using autocd
So everytime I change the directory I would like the new path to automatically get listed with ls
.
There are solutions if the path gets changed using cd
but I never found a solution that would work when the path gets changed using the autocd
option.
Would be quite convenient, has anyone achieved that before?
Solution 1:
You can achieve this by changing the variable PROMPT_COMMAND
, which is executed before PS1, e.g.:
PROMPT_COMMAND='ls'
This will list each time you get a new prompt. Of course you only want to execute ls
if the directory has changed:
PROMPT_COMMAND='[[ $my_currdir != $PWD ]] && ls; my_currdir=$PWD'
This checks if the new directory isn't the same as the last one set, and then executes ls
, after which your current directory is stored in my_currdir
I found an even better line here, where they use bash as a file manager:
PROMPT_COMMAND='[[ ${__new_wd:=$PWD} != $PWD ]] && list; __new_wd=$PWD'
since it is possible that __new_wd
is unset, you can assign a default value which will be used in that case (see bash
man pages, search for :=
)