The following is a shell function that I use in my command prompt to display ONLY the last n subdirectory names (for example the last 3 subdirectory names). The \W which is the default PS1 magic that displays the current working directory in your promote ONLY displays the name of the directory and none of the path. The \w magic displays the entire path, which can be way too long. My __cwd() function displays the last n subdirectories
So changing your default prompt from:
PS1="\h:\W \u\$ "
to
PS1="\h:\$(__cwd 3) \u\$ "
#
# __cwd() - function to create current working directory prompt string. I
# want less than the entire path, and more than just the last
# element in the path. It turns out the bash prompt string can
# execute a script function, so I created my own bit of code to
# generate the current working directory prompt string I want.
#
# Usage: PS1="...\$(__cwd 3)..."
#
# Where: 3 in the above example is the number of subdirectories to
# keep. 3 is also the default if no value is given.
# Output: /
# /usr
# /usr/share
# /usr/share/vim
# /.../share/vim/vim63
# ~/
# ~/Music
# ~/Music/iTunes
# ~/Music/iTunes/iTunes\ Music # notice spaces are honored
# ~/.../iTunes/iTunes\ Music/Beach Boys
# ~/.../iTunes\ Music/Beach\ Boys/Made\ in\ the\ U.S.A.
#
__cwd()
{
n="${1:-3}" # if no dir count, default to 3
p="#${PWD}" # add # so after split # represents the root dir
IFS="/" # split on directory divider '/'
set ${p/#\#${HOME}/\~} # Replace #$HOME with ~ and split into dir parts
p="${1}" # ${1} has # or ~, save it
shift $(($#-(n+1) < 0 ? 0 : $#-(n+1))) # remove excess leading dirs
[[ "${1}" != [#~]* ]] && p="${p}/..." # ... represents a partial path
for ((j=2; j<n+2; j++)); do p="${p}/${!j}"; done # append last n dirs
while [[ "${p}" = [#/]/* ]]; do p="${p#?}"; done # del leading /'s and #
while [[ "${p}" = *[^~]/ ]]; do p="${p%?}"; done # del trailing /'s
printf "%q" "${p}" # generate path str w/shell magic chars quoted
}
As my professors used to say: "Figuring out how __cwd() works is left as an exercise for the student." 🙂
In reality, my PS1 prompt is much more complex. I also colorize my prompt. For example:
PS1="\D{%a@%R}[\[\e[44;36;1m\]\h\[\e[0m\]]\[\e[34;1m\]\$(__cwd 3)\[\e[0m\]> "
This will generate a prompt similar to the following:
Thu@15:09[BobBookPro]~/.../iTunes/iTunes\ Music/Beatles>
Where 'BobBookPro' will be Cyan text on Blue (I couldn't get the forum to generate the blue background, but it really does look nice when you see it on the actual terminal. And the "~/.../iTunes/iTunes\ Music/Beatles" will be Blue.
Where:
\D{%a@%R} is prompt magic to substitute the day of the week @ time of day.
All the \[...\] notation tells the prompt that non-printing character codes are happening between. In this case the color codes are inside the \[...\]
- \e[44;36;1m says cyan text on blue
- \e[34;1m says blue text
\h is the computer name. If you are having any problems getting the Mac to always use your System Preferences -> Sharing -> Computer name, you could replace "\h" with "$(networksetup -getcomputername)"
#
# Prompt Colors
# -foreground- ----bold---- -underline-- --reverse--- -background-
# black \e[30m \e[30;1m \e[30;4m \e[30;7m \e[40;fg;1m
# red \e[31m \e[31;1m \e[31;4m \e[31;7m \e[41;fg;1m
# green \e[32m \e[32;1m \e[32;4m \e[32;7m \e[42;fg;1m
# yellow \e[33m \e[33;1m \e[33;4m \e[33;7m \e[43;fg;1m
# blue \e[34m \e[34;1m \e[34;4m \e[34;7m \e[44;fg;1m
# magenta \e[35m \e[35;1m \e[35;4m \e[35;7m \e[45;fg;1m
# cyan \e[36m \e[36;1m \e[36;4m \e[36;7m \e[46;fg;1m
# White \e[37m \e[37;1m \e[37;4m \e[37;7m \e[47;fg;1m
#
# Reset \e[0m
#
I have several different Mac, Linux, Solaris, AIX system I work with on a regular basis. I use different hostname colors so it is easier for me to see by the color with terminal window I'm currently using. Also when I'm scrolling through my scroll back history, the colored prompts make it easy to see the commands vs the output, which is very useful if the output is very long.
Message was edited by: BobHarris