Linked by Shane on Wed 24th Nov 2010 22:40 UTC
General Development "It is sometimes hard to form a mental model of a directory tree when working with the command line. GUI shells tend to provide more visual cues. So, like a lot of Bash users, I used to get around this by printing the current working directory in my shell prompt. However, things can get pretty cramped when dealing with deeply nested directories. A better solution is needed."
Permalink for comment 451078
To read all comments associated with this story, please click here.
sarahannalien
Member since:
2009-05-07

Here's what I've used for the last few years... on Mac OS X... this copy is tweaked a little to privatize... I don't know if this is generally useful for people or not, but in our environment some fairly long subpaths repeat themselves over and over again, so it occurred to me that I could run the path through a script to abbreviate those common paths.

Sarah


----- .bash_profile -----
PS1='$(/Some/place/sarahprompt.py)\n\!\$ '

----- begin sarahprompt.py -----
#!/usr/bin/python -E -S
import os
import time


# Date like: May17
tinydate=time.strftime("%b%d")

# \x1B is escape, \e doesn't work and I don't know why.

# Set title of window or tab
def title(str):
return '\x1B];'+str+'\007';

# Make a colored bar with text in it in the current window
def bar(str):
return "\x1B[2K\x1B[0;1;47m\x1B[0K"+str+"\x1B[0m";

# Get the current directory, and substitute ~ for home dir
current_path=os.environ["PWD"]
home=os.environ["HOME"]
if (current_path.startswith(home)):
current_path="~"+current_path[len(home):]

current_path=current_path.replace("/some/long/tedious/path/","/(abb rev)/")
#... add more replacements here ...

# Last two path elements (works right for less than two, too!
tinypath="/".join(current_path.split("/")[-2:])

# Prompt string sets the title and the bar
prompt=title(tinydate+" "+tinypath)+bar(current_path)

# Print the string (which passes it back to bash for display)
print prompt

----- end sarahprompt.py -----

Reply Score: 2