Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

vim has a really nice feature which it utilizes in its paths when they're a bit long:

enter image description here

It abbreviates the path to the document in the tab at the top. Is there a way to do something similar to this so my bash prompt doesn't look like this:

rfkrocktk@work-laptop ~/Documents/Projects/Work/maventest/src/main/java/com/tkassembled/ $ 
share|improve this question
So, what you want is rfkrocktk@work-laptop ~/D/P/W/m/src/main/java/com/tkassembled/ $ ? Ideally, using some threshold for maximum prompt length? – nicerobot Dec 14 '11 at 19:21
Exactly, that's what I'm looking for. – Naftuli Tzvi Kay Dec 14 '11 at 19:31
Ok, i provided my answer. – nicerobot Dec 14 '11 at 21:31

4 Answers 4

up vote 6 down vote accepted

Try this:

export PROMPT_COMMAND='PS1X=$(perl -pl0 -e "s|^${HOME}|~|;s|([^/])[^/]*/|$""1/|g" <<<${PWD})'

or, pure bash:

export PROMPT_COMMAND='PS1X=$(p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf "${q:1}")'


export PS1='\u@\h ${PS1X} $ '


rfkrocktk@work-laptop ~/D/P/W/m/s/m/j/c/tkassembled $

I improved my answer thanks to @enzotib's

share|improve this answer
Can you provide an example of what this would look like as a prompt, for posterity? – Naftuli Tzvi Kay Dec 14 '11 at 21:32

Similar to @nicerobot answer, but somewhat shorter:

PROMPT_COMMAND='pwd2=$(sed "s:\([^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '

This will show the followin example output:

enzotib@acer:/h/enzotib$ cd
enzotib@acer:/h/enzotib$ cd /usr/share/doc/acpid/examples/
share|improve this answer

I like PROMPT_DIRTRIM in bash...


will change your example prompt to...

rfkrocktk@work-laptop ../com/tkassembled/ $

It works for me.

share|improve this answer
Doesn't do what he asked but thanks for informing me of the feature. – nicerobot Dec 18 '11 at 19:53
@nicerobot Yeah, you had the real answer. :) I know it doesn't give the abbreviation he was asking for, but often when we see simple options our expectations change. Also, it is standard, and as such I felt it deserved a mention. – BentFX Dec 19 '11 at 3:44
I feel that one of the functions of SE is to not only provide the perfect answer to the question, but also to provide other options for the OP and others who find the question through searching. Notice the answer context I provided "I like... works for me." Still some believe it deserves a down vote. – BentFX Dec 19 '11 at 3:59
I tend to see answers get down voted that don't directly answer the question but I agree with you that providing alternatives can be a valid response or answer. – nicerobot Dec 19 '11 at 20:10
BentFX's answer doesn't answer the OP's question as a whole but exactly the subject Abbreviated current directory in shell prompt? which is for probably most of the visitors of this topic exactly what they are looking for. – TNT Dec 9 '14 at 10:36

The format for use in your PS1 is \W (see the PROMPTING section in the bash man page).

PS1="\u@\h \W\$ "

And you might want to read the other options there, you can do some cool stuff with your command line.

share|improve this answer
I think \W doesn't do at all what he's asking. – nicerobot Dec 14 '11 at 19:20
@nicerobot It doesn't give the first initial of every component of the path, but it does give the current working directory in a shorter manner. I'm pretty sure it's not possible to abbreviate the directory in that manner without using some rather ugly script executed each prompt. We'll see what he has to say about this. – Kevin Dec 14 '11 at 19:36

Your Answer


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.