Reputation: 18595
In my .bashrc
, I got this:
PS1="[\w $]"
And every time when I cd
to a dir with a deep level, the shell prompt almost takes up the whole line, (terminal size: 80*24), like:
[/level_a_dir/level_b_dir/level_c_dir/level_d_dir/level_e_dir $]
Question
I want to cut the prompt short if the pwd
is longer than 20 chars, just keep the last dir, like:
[.../level_e_dir $]
#[/level_a_dir/level_b_dir/level_c_dir/level_d_dir] is replaced with ...
How to do it?
Upvotes: 2
Views: 519
Reputation: 26547
If you really want just 20 characters whetever that might be (or less), then the simplest I can think of is:
export PS1='[${PWD:$((${#PWD}-20))} $]'
I would drop the brackets if you don't have much space or think about having a two line prompt (which I personally hate :-)
Upvotes: 0
Reputation: 25736
I have done it in the following way.
First you have to create a shell script, truncate.sh:
#!/bin/bash
MAXLEN=20
REPLACEMENT="..."
# replace /home/user by ~
TPWD=$(echo ${PWD} | sed 's#'${HOME}'#~#;')
# truncate
if [ ${#TPWD} -gt ${MAXLEN} ] ; then
PWDOFFSET=$(( ${#TPWD} - ${MAXLEN} ))
TPWD="${REPLACEMENT}${TPWD:${PWDOFFSET}:${MAXLEN}}"
fi
echo ${TPWD}
Next you have to replace your PS1:
export PS1="[\$(truncate.sh) ] "
Upvotes: 2