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/ $ 
  • 2
    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
    Commented Dec 14, 2011 at 19:21
  • Exactly, that's what I'm looking for. Commented Dec 14, 2011 at 19:31
  • Ok, i provided my answer.
    – nicerobot
    Commented Dec 14, 2011 at 21:31

6 Answers 6


I like PROMPT_DIRTRIM in bash...


will change your example prompt to...

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

It works for me.

  • Doesn't do what he asked but thanks for informing me of the feature.
    – nicerobot
    Commented Dec 18, 2011 at 19:53
  • 3
    @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
    Commented Dec 19, 2011 at 3:44
  • 1
    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
    Commented Dec 19, 2011 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
    Commented Dec 19, 2011 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
    Commented Dec 9, 2014 at 10:36

Try this:

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

or, pure bash:

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


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

produces (notice the ~ for ${HOME}):

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

I improved my answer thanks to @enzotib's

  • 1
    Can you provide an example of what this would look like as a prompt, for posterity? Commented Dec 14, 2011 at 21:32
  • (0) Your github link is broken.  (1) You might want to take a closer look at the $PWD~ test.  Currently, if HOME is /home/ed and PWD is /home/edwina, you get ~wina in the Perl version and ~/ina in the shell version.  (2) Your shell code chokes if the current directory contains any wildcard (i.e., pathname expansion) characters; e.g., PWD = /abc/*/def.  You can fix this by adding set -o noglob or set -f.  … (Cont’d) Commented Jan 25, 2016 at 5:41
  • (Cont’d) …  (3) You don’t need nearly so many braces.  In the Perl command, $HOME (which is inside double quotes) and $PWD are good enough.  In the shell command, the last three shell variable references are fine, but the first three can be "${PWD#$HOME}", "$PWD", and "$p".  (4) See also mbentley’s answer and my comments thereon. Commented Jan 25, 2016 at 5:43

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/

Adding on to enzotib's answer, the following snippet will:

  1. Convert /Path/To/Your/Home/ to ~/ (so a path will be ~/a/b/c instead of /P/T/Y/H/a/b/c)
  2. Use the first letter of a 'dot file' instead of showing only the dot (/a/./c/d will become /a/.b/c/d):

    PROMPT_COMMAND='PS1_PATH=$(sed "s:\([^/\.]\)[^/]*/:\1/:g" <<< ${PWD/#$HOME/\~})'
    export PS1='\u@\h:$PS1_PATH\$ '

Make sure you use single quotes or bash will expand it prematurely.


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.

  • 2
    I think \W doesn't do at all what he's asking.
    – nicerobot
    Commented Dec 14, 2011 at 19:20
  • 1
    @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
    Commented Dec 14, 2011 at 19:36

I love the output from nicerobot's example but I found one issue. I have a directory that has a hyphen in it, A-E, and it was seeing -E as an argument to printf when it was my current working directory. To fix this, I added -- to the last printf and added quotes around the output in case there is nothing to output:

p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf -- "${q:1}"
  • (1) Good catch on the issue of the directory name containing - as the second character.  While it’s true that -- is the general solution to the issue of arguments beginning with -, a better method for printf is to say printf "%s" "${q:1}", as this also protects against arguments containing %.  (For example, try PWD = /home/mbentley/Documents/Projects/Work/maventest/src/main/java/com/abc%def.  (2) See also my comments on nicerobot’s answer. Commented Jan 25, 2016 at 5:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .