Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I can't remember where, but I've already seen the bash prompt's current path shortened in an interesting way: every directory contained in the path is replaced by its first letter only. For instance: path/to/some/directory would be shortened to p/t/s/d.

How can I reproduce that behavior ?

share|improve this question
    
You can't directly do this in PS1. You would need to set PS1 to contain a variable (PS1='${SHORT_PWD}\\$') and update that variable in $PROMPT_COMMAND. –  Gilles Mar 8 '13 at 1:30
    
What have you tried? Have you ever heard of $PS1? –  shakabra Mar 8 '13 at 1:41
add comment

3 Answers 3

up vote 2 down vote accepted

After playing with this for a while I got the answer you require:

Add this to your .bashrc file in your home directory, exit the terminal and renter it and you will get you prompt.

PS1='$(eval "sps")$ '
sps() {
   echo "$PWD" | sed -r 's|/(..)[^/]*|/\1|g'
}

It uses the declared function sps() to evaluate the path every time the variable PS1 which is the prompt, is displayed

ie

/ho/de/De/Ap/Ti$ pwd
/home/deth/Desktop/Apps/Tivo
/ho/de/De/Ap/Ti$ 

Or...if you insist on the one letter

PS1='$(eval "sps")$ '
sps() {
   echo "$PWD" | sed -r 's|/(.)[^/]*|/\1|g'
}

Which displays:

/h/d/D/A/T$ pwd
/home/deth/Desktop/Apps/Tivo
/h/d/D/A/T$ 
share|improve this answer
add comment

To truncate all directory names except the last one:

PS1='$(eval "sps")$ '                                                                                
sps() {                                                                                              
    python -c "import sys; dirs = sys.argv[1].split('/'); print '/'.join(d[:1] for d in dirs[:-1]) + '/' + dirs[-1]" $PWD
}
share|improve this answer
add comment

The bash prompt is set by the PS1 variable. Yours is probably set in ~/.bashrc. Mine is on line 58. You will see something like this

if [ "$color_prompt" = yes ]; then
    PS1=""

Edit

Here a google search I did.

PROMPT_COMMAND='CurDir=`pwd|sed -e "s!$HOME!~!"|sed -re "s!([^/])[^/]+/!\1/!g"`'
PS1="[\$CurDir] \$ "
share|improve this answer
3  
You are not answering the question at all. The difficulty isn't where to set the prompt (Anto already knows about .bashrc) but what to set it to. –  Gilles Mar 8 '13 at 1:28
2  
Yep, exactly Gilles, thanks. I've been looking everywhere over the web to find a way to do it, but all I could find so far was simple PS1 substitution, which is not what I want to achieve here. –  Anto Mar 8 '13 at 1:30
    
@Gilles Sorry if I try to teach and not just puke out answers. Here ya go buddy. Socrates would be so proud of me. –  shakabra Mar 8 '13 at 1:37
    
... and everybody knows how all the best teachers love to throw some "I have no idea what you want to do, but it looks like stupid crap" to their students. Moreover this is a Q&A, with, you know, pragmatic questions and answers... If you just want to flame or trash-talk people, you can go back to something like 4chan ;) –  Anto Mar 8 '13 at 1:45
    
Sorry I edited my post. –  shakabra Mar 8 '13 at 1:48
add comment

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.