Take the 2-minute tour ×
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
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

4 Answers 4

up vote 5 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
    
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

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

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

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

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.