Sometimes I like to have very customized bash aliases and maybe keybindings when I enter on a folder (can be a project where I'm used to type lots of commands from CLI).

Lets suppose when I enter a folder like "projectAlpha/" I expect some custom aliases like:

  • alias doTaskX='./app arg1 arg2...|filter arg1 arg2...'
  • ...

More than it I was thinking of:

  • Display a helpful message showing aliases and common commands
  • Displaying some common custom keybindings for my project

But as soon as I leave the root of my project these settings become unavailable.

What is the best approach to do it today?


Bash has special variable PROMPT_COMMAND which content is executed every time after the command is executed, so you can set it as follows:

PROMPT_COMMAND='[[ "$PWD" = /tmp ]] && { alias ll=ls; echo "Remember: ll=ls"; } || { unalias ll 2>/dev/null; }'

From now on every time you will be in the /tmp directory alias ll will be created and in all other directories it will be removed.

Probably more practical is to gather all your aliases and other stuff in one function (say makealias) and all unaliases in the other function makeunalias which you can run depending on your needs, something like

PROMPT_COMMAND='[[ "$PWD" = /tmp ]] && makealias || makeunalias 2>/dev/null'

Notice that stderr redirection is present only to prevent some useless in this case alerts.

| improve this answer | |
  • ty, i'll try it. +1 (if i could up) – gzinho Jun 15 '16 at 15:58
  • 1
    @user3697611 You don't need any reputation to accept the answer to your own question. Click on the check mark next to the answer. – Barmar Jun 22 '16 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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