fixed width bash prompt

Posted by seaofclouds on Stack Overflow See other posts from Stack Overflow or by seaofclouds
Published on 2010-05-27T17:36:22Z Indexed on 2010/05/27 18:01 UTC
Read the original article Hit count: 341

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about bash

Related posts about osx