Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 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
up vote 7 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}")'

then

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

produces:

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

I improved my answer thanks to @enzotib's

share|improve this answer
1  
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
    
(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) – G-Man Jan 25 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. – G-Man Jan 25 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/
enzotib@acer:/u/s/d/a/examples$
share|improve this answer

I like PROMPT_DIRTRIM in bash...

export PROMPT_DIRTRIM=2

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
1  
@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
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 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
2  
I think \W doesn't do at all what he's asking. – nicerobot Dec 14 '11 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 Dec 14 '11 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}"
share|improve this answer
    
(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. – G-Man Jan 25 at 5:46

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.

share|improve this answer

Your Answer

 
discard

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.