PS1 line-wrapping with colours problem

后端 未结 3 733
天涯浪人
天涯浪人 2020-12-13 12:56

Here\'s my PS1 variable:

PS1=\'\\u:\\W$(__git_ps1 \"\\e[32m\\][%s]\\e[0m\\]\")$ \'

Works great for picking up my Git branch, but it has the

3条回答
  •  旧时难觅i
    2020-12-13 13:28

    May I suggest the following method for colors in Bash, it makes the code much more readable and alot harder for you to miss an escape or two.

    Put the following in your ~/.bashrc

    BLACK=$(tput setaf 0)
    RED=$(tput setaf 1)
    GREEN=$(tput setaf 2)
    LIME_YELLOW=$(tput setaf 190)
    YELLOW=$(tput setaf 3)
    POWDER_BLUE=$(tput setaf 153)
    BLUE=$(tput setaf 4)
    MAGENTA=$(tput setaf 5)
    CYAN=$(tput setaf 6)
    WHITE=$(tput setaf 7)
    BRIGHT=$(tput bold)
    NORMAL=$(tput sgr0)
    BLINK=$(tput blink)
    REVERSE=$(tput smso)
    UNDERLINE=$(tput smul)
    

    A sample PS1 (or really anything that prints to the screen) would be:

     PS1="\[${WHITE}\](\[${YELLOW}\]\u@\h\[${WHITE}\])\[${NORMAL}\]$ "
    

    You need only put \[ \] around the color words.

    If you have a 256-color terminal, you can experiment with other numerical values to 'tput setaf' all the way up to 255.

提交回复
热议问题