Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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 :=)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.