Force prompt on specific directories

4

Is there a way to change the prompt on a different directories on the same machine? For example, I want a simple prompt "[\W] \$" in development directories.

Ideas?

Konzepz

Posted 2011-01-01T17:42:22.873

Reputation: 785

1It depends on which shell you're using. – Paused until further notice. – 2011-01-01T19:09:24.400

Answers

6

You can put something like this in your .bashrc.

prompt-function() {
    case $PWD in
        /a/dev/dir\
        |/another/dev/dir) 
            PS1="[\W] \$"
            ;;
        *)
            # Change this to the default prompt
            PS1="\$ "
            ;;
     esac
}

PROMPT_COMMAND=prompt-function

Wuffers

Posted 2011-01-01T17:42:22.873

Reputation: 16 645

+1 I would use a case statement instead of a series of elsif statements. You could even put the directories in an array and iterate over them with a for loop. You could use globbing patterns with any of those techniques or you could use regexes with [[ $PWD =~ $pattern ]]. – Paused until further notice. – 2011-01-01T19:13:08.560

@Dennis: I edited my answer with a case statement. Thanks for the suggestion. – Wuffers – 2011-01-01T19:16:49.297

If the action is the same, you can list the action once after alternative cases separated by pipe characters. foo|bar) action;; You can put the cases on separate lines by putting a backslash and newline before or after the pipe character. – Paused until further notice. – 2011-01-01T21:47:42.843

@Dennis: Edited my answer accordingly, again. Thanks for the suggestion! – Wuffers – 2011-01-01T21:58:05.007