Operating System - HP-UX
1748006 Members
4568 Online
108757 Solutions
New Discussion юеВ

limiting length of PS1 that uses pwd

 
Roger May_1
Occasional Contributor

limiting length of PS1 that uses pwd

I have reviewed several of the posts that requested info on how to add the current path to the command prompt.

My question is how to limit the length of the prompt to say 30 characters?

If I am working in a term window and traverse a long directory path, the prompt gets so long that I cannot see much of what I type.

So when the path is long (say over 30 characters), I would like to have the prompt truncate the beginning of the path and leave the last 10-15 characters.

For example, when using this for the prompt in .profile:

export PS1=`hostname`:'$PWD # '


I see the following:

# cd /htc/technology
rx203:/htc/technology #

But when the path is long, I need to limit the display:

# cd softwaresystems/regional/field_locations
rx203:/htc/technology/softwaresystems/regional/field_locations #

(I would prefer to see something like the following, with the hostname, some filler like " ... ", and only the end of the path)

rx203: ... d_locations #


7 REPLIES 7
Steven Schweda
Honored Contributor

Re: limiting length of PS1 that uses pwd

It's not classy, but it may be close to what
you want:

dy # cat ~/pwdp.sh
#!/bin/sh
x=` pwd `
xs=` echo "$x" | sed -e 's/^.*\(.\{16\}$\)/\1/' `
if [ "$x" != "$xs" ]; then
xs="...$xs"
fi
echo "$xs"

dy # export PS1='` ~/pwdp.sh ` '
...sr/local/src/zip cd /usr
/usr


where the user input was:
cd /usr
Dennis Handly
Acclaimed Contributor

Re: limiting length of PS1 that uses pwd

By solution is simple. My PS1 is just "$ ". If I need the CWD, it is in the window title.

You can use escape sequences for hpterm and dtterm to set the title.

function settitle { # Set the ivoterm titlebar:
if [ "$TERM" = "hp2392a" -o $TERM = "hpterm" ]; then
ti="$host $PWD"
echo "^[&f0k${#ti}D${ti}\c"
elif [ "$TERM" = "dtterm" -o "$TERM" = "xterm" ]; then
ti="$host $PWD"
echo "^[]2;${ti}^G\c"
fi
}

^[ is really an escape and ^G is a control G.

Of course to drive all of this, I have pushd/popd functions, including cd.
Bill Hassell
Honored Contributor

Re: limiting length of PS1 that uses pwd

Most of the time, I get everything I need to know about the current directory by displaying just the current and parent directories and by using some built-in shell constructs, it is easy to assign them to PS1:

HN=$(hostname)
export PS1='$HN...${PWD##${PWD%/*/*}/} # '

If you want to use some highlighting, you can pick from this list:

# set char enhancments only if interactive (ie, TERM is set)
# If TERM is not set or the TERM value is not found in terminfo
# files, then enhancements are null.

export HB=$(tput dim 2>/dev/null) # dim text
export HV=$(tput smso 2>/dev/null) # 1/2 bright inverse
export IV=$(tput bold 2>/dev/null) # inverse
export UL=$(tput smul 2>/dev/null) # underline
export BL=$(tput blink 2>/dev/null) # blink
export EL=$(tput el 2>/dev/null) # clear to end of line
export ED=$(tput ed 2>/dev/null) # clear to end of display
export EE=$(tput sgr0 2>/dev/null) # end all enhancements

Then add them to your PS1 prompt:

HN="$IV$(hostname)$EE"
export PS1='$HN...$HB${PWD##${PWD%/*/*}/}$EE # '

Modern terminal emulators can display a number of video enhancements and these can often be configured with different colors within the emulator. After running all the export lines above, run this echo to show all the enhancements supported by your terminal:

echo "normal $HV smso $HB dim $IV bold $UL smul $BL blink $EE normal"


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: limiting length of PS1 that uses pwd

Many things are possible. My point was that
one can use a shell script to do some more
complicated things than can easily be placed
directly into a PS1 assignment.

Personally, I use PS1="` hostname ` # ", and
if I forget which directory I'm in, I find
that "pwd" tells me what I need to know
pretty easily. (But I'm a stone-axe kind of
guy.)
Andy Torres
Trusted Contributor

Re: limiting length of PS1 that uses pwd

What I have done is this, "borrowed" from this very forum some time ago: Put the present working directory and miscellaneous info on the line above.

Try this in your profile:
===============================
export HOST=`/usr/bin/hostname`
export PS1='
$LOGNAME@$HOST: [$PWD] \

# '
===============================
Peter Nikitka
Honored Contributor

Re: limiting length of PS1 that uses pwd

Hi,

I recommend scp/rcp-compatible output:
HOST=${HOST:-$(uname -n)}
PS1="$LOGNAME@$HOST:\${PWD#$HOME/}[!]
"

gives relative pathnames for files under $HOME.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Eric Raeburn
Trusted Contributor

Re: limiting length of PS1 that uses pwd

Another option to consider is to simply insert a newline before the prompt character. Using the long path in your example, if

export PS1=$(hostname):'$PWD'$(echo "\n# ")

then you would have:

rx203:/htc/technology/softwaresystems/regional/field_locations
# _

This allows much longer paths to be visible, while avoiding visual confusion.

-Eric