When working in terminal, I’d like my prompt to look like:[user@host:.../cocoa/apple/examples]$I like to know a couple levels above where I am without it taking over the entire screen width. This function will let you show the last 3 levels or the whole path if less. It also shows ~ for your home path. Here is the code to get this to work in your .profile/.bashrc/whatever:
# Prompt that replace console title and uses breadcrumbsexport PS1='\[\e]2;\u@\H \w\a\][\u@\h:`breadcrumbs "$PWD" 3`]\$ ‘# Function that returns the last n path components of the specified path after# replacing $HOME with ~. Call like:# breadcrumbs path nfunction breadcrumbs{ echo $1 | sed “s|^$HOME|~|” | awk -v n=$2 ‘{ # Split the path into components count = split($0, components, “/”); # If there are less than n components, just print the whole path if (count <= n) { print $0 } else { # Else print the last n components if (count > n) { printf “…/” } for (i = count - n + 1; i < count; i++) { printf “%s/”, components[i] } printf “%s”, components[i] } }’}
Comments 4
I couldn’t get this to work Andrew, could you check and make sure WordPress didn’t mess it up?
Posted 27 Aug 2006 at 9:10 ¶I got it working, but had to make some pretty heavy changes, you can see what I did at:
http://files.nnutter.com/public/profile
Posted 27 Aug 2006 at 10:43 ¶Yes, wordpress has made a mess of this post. All my less than signs are missing.
Posted 27 Aug 2006 at 20:01 ¶I use zsh, the breadcrumbs thing is built in
http://docsrv.caldera.com:8457/cgi-bin/info2html?(zsh.info.gz)Prompt%2520Expansion
%d
Posted 04 Oct 2006 at 18:55 ¶%/
Present working directory ($PWD). If an integer follows the `%’,
it specifies a number of trailing components of $PWD to show; zero
means the whole path.
Trackbacks & Pingbacks 1
[...] Andrew wrote a tip today to have your bash prompt show the last ‘n’ folders of your current directory. His original suggestions is on his website, Readable Path for Terminal. [...]
Post a Comment