Take the 2-minute tour ×

On my Mac OS X 10.6, I have my prompt set to

PS1='\[\033[01;32m\][\w\[\033[00m\]\[\033[01;34m\]$(__git_ps1 " (%s)")\033[01;32m\]]\[\033[00m\]\$ '

How can I limit the number of directories in my prompt? The \w part?

This is because when I go deep in a directory tree, the prompt takes up the whole width of the terminal.

share|improve this question
Just a tip IMO useful: split the prompt on two lines; something like '\w\n\$ '. –  cYrus Feb 9 '12 at 12:34

2 Answers 2

up vote 3 down vote accepted

you already use a function in your prompt for your git branch name probably, which is nice, so just do the same for your path:

# shorten a path in $1 to max of $2 characters, prepending a "..."
function __shortpath {
        echo "..."${1: -$len}
        echo $1
PS1='\[\033[01;32m\][$(__shortpath "\w" 50)\[\033[00m\]\[\033[01;34m\]$(__git_ps1 " (%s)")\033[01;32m\]]\[\033[00m\]\$ '

It will limit the path to the last 50 character. This is an example. You could also think of cutting off only at a slash, but that make it more difficult.

function __droppath {
        echo "..."$p
        echo $1
PS1='\[\033[01;32m\][$(__droppath "\w" 50)\[\033[00m\]\[\033[01;34m\]$(__git_ps1 " (%s)")\033[01;32m\]]\[\033[00m\]\$ '

Maybe it's also a good idea to secure the while loop by checking if new length is shorter than old length (otherwise quit) or if new length is 0 in which case you may return the last value of $p - or continue with __shortpath.

share|improve this answer

I personally don't have any paths in my prompt, I don't like how it moves the prompt location, even if it's cut down.

I put the current location in the title bar:

USER=$(/usr/bin/id -un)
HOSTNAME=$(uname -n)
PROMPT_COMMAND='echo -ne "\e]0;$USER@${HOSTNAME}: $(pwd -P)\a"'

The command for USER getting the current user is linux/gnu flags, MacOSX/bsd flags may differ.

share|improve this answer

Your Answer


