Reputation: 35
I'd like to set my bash prompt to a fixed width, and make up the difference in space before the $, so whether long or short, my prompt remains the same width:
[name@host] ~/Directory/Dir...Another/LastDir $
[name@host] ~/Directory(branch) $
Currently, in a short directory path my prompt looks something like this:
[name@host] ~/Directory(branch) $
a deeper directory path looks like this:
[name@host] ~/Directory/Dir...Another/LastDir $
You can see I've truncated the PWD in the middle so I can see where the path begins, and where it ends. I'd like to make up the difference before the $.
Here is my current prompt:
# keep working directory to 30 chars, center tuncated
prompt_pwd() {
local pwd_symbol="..."
local pwd_length=30
newPWD="${PWD/#$HOME/~}"
[ ${#newPWD} -gt ${pwd_length} ] && newPWD=${newPWD:0:12}${pwd_symbol}${newPWD:${#newPWD}-15}
}
# set prompt
prompt_color() {
PROMPT_COMMAND='prompt_pwd;history -a;title_git'
PS1="${WHITEONMAGENTA}[\u@\h]${MAGENTA} \w\$(parse_git_branch) ${MAGENTABOLD}\$${PS_CLEAR} "
PS1=${PS1//\\w/\$\{newPWD\}}
PS2="${WHITEONTEAL}>${PS_CLEAR} "
}
In my search, I found A Prompt the Width of Your Term which does do some fill, but couldn't get it working for this particular prompt.
Upvotes: 3
Views: 1349
Reputation: 42448
If your path is too short ([ ${#newPWD} -le ${pwd_length} ]
), you can use printf(1) to extend it.
printf -v newPWD "%-*s" $pwd_length "$newPWD"
This will left-justify the string in $newPWD
with a field width of $pwd_length
.
printf(1) is built into bash so it is not too expensive to run each prompt.
Upvotes: 3
Reputation: 6257
You can set the environment variable BASH_COMMAND
, which allows you to define the prompt manually. You may use expand -t
to get it to the desired length.
Upvotes: 1
Reputation: 5943
What I've done is position my directory and such above my prompt, so I don't have to worry about it:
myusername@mybox:/home/myusername/some/directory
$
This way, I can also have the added benefit of copying the line above for SCP and SSH commands without having to prepend the user@host:/directory example.
Here's my $PS1 variable, should you like to try it out:
\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$
Upvotes: 0