# How do I get a colour prompt?

Hi,

I am using Fedora 20. I found there is no enough color prompts in the fedora terminal. One example is that when switching to root, the "root@localhost" keeps same color, not like in other distribution turns to other color.

Also, I could not turn on the color syntax in vim, even I typed "syntax on".

edit retag close merge delete

3

Changing the prompt colour based on the UID isn't a bash upstream feature, rather a distro customisation. In Mageia they use a package called colorprompt to achieve that; basically they use this .sh file to do that (go there and click "download" to get it). You can then put it in /etc/profile.d/ and it should work.

Note that I haven't tested it myself.

As for vim, what's the mimetype of the file you're editing?

( 2014-06-13 08:27:51 -0600 )edit

Sort by » oldest newest most voted

You can change your prompt. Please google "bash colour prompt PS1". Basically, you have to change your PS1. For example, this is what I use in my ~/.bashrc:

# For 256 colours in xterm
if [ "$TERM" == "xterm" ] || [ "$TERM" == "screen" ] || [ $TERM == "screen-256color" ]; then #TERM=xterm-256color export PROMPT_COMMAND='printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' # Bash prompt # Colours: # txtblk='\e[0;30m' # Black - Regular # txtred='\e[0;31m' # Red # txtgrn='\e[0;32m' # Green # txtylw='\e[0;33m' # Yellow # txtblu='\e[0;34m' # Blue # txtpur='\e[0;35m' # Purple # txtcyn='\e[0;36m' # Cyan # txtwht='\e[0;37m' # White # bldblk='\e[1;30m' # Black - Bold # bldred='\e[1;31m' # Red # bldgrn='\e[1;32m' # Green # bldylw='\e[1;33m' # Yellow # bldblu='\e[1;34m' # Blue # bldpur='\e[1;35m' # Purple # bldcyn='\e[1;36m' # Cyan # bldwht='\e[1;37m' # White # unkblk='\e[4;30m' # Black - Underline # undred='\e[4;31m' # Red # undgrn='\e[4;32m' # Green # undylw='\e[4;33m' # Yellow # undblu='\e[4;34m' # Blue # undpur='\e[4;35m' # Purple # undcyn='\e[4;36m' # Cyan # undwht='\e[4;37m' # White # bakblk='\e[40m' # Black - Background # bakred='\e[41m' # Red # badgrn='\e[42m' # Green # bakylw='\e[43m' # Yellow # bakblu='\e[44m' # Blue # bakpur='\e[45m' # Purple # bakcyn='\e[46m' # Cyan # bakwht='\e[47m' # White # txtrst='\e[0m' # Text Reset # using tput commands FGBLK=$( tput setaf 0 ) # 000000
FGRED=$( tput setaf 1 ) # ff0000 FGGRN=$( tput setaf 2 ) # 00ff00
FGYLO=$( tput setaf 3 ) # ffff00 FGBLU=$( tput setaf 4 ) # 0000ff
FGMAG=$( tput setaf 5 ) # ff00ff FGCYN=$( tput setaf 6 ) # 00ffff
FGWHT=$( tput setaf 7 ) # ffffff BGBLK=$( tput setab 0 ) # 000000
BGRED=$( tput setab 1 ) # ff0000 BGGRN=$( tput setab 2 ) # 00ff00
BGYLO=$( tput setab 3 ) # ffff00 BGBLU=$( tput setab 4 ) # 0000ff
BGMAG=$( tput setab 5 ) # ff00ff BGCYN=$( tput setab 6 ) # 00ffff
BGWHT=$( tput setab 7 ) # ffffff RESET=$( tput sgr0 )
BOLDM=$( tput bold ) UNDER=$( tput smul )
REVRS=$( tput rev ) export PS1="$FGGRN$[\u@\h $FGBLU$ \W$FGRED$\$(__git_ps1 $$%s$$)$FGGRN$]\$$RESET$" #export PS1='$FGGRN$[\u@\h $FGBLU$\W$FGRED$\$(__git_ps1 $$%s$$)$FGGRN$]\$$RESET$' else export PS1="[\u@\h \W\$(__git_ps1 $$%s$$)]\$" fi  Note that I also use __git_ps1, which adds git information to it. I'm not sure why you haven't gotten colours in vim. Please open a separate question for that issue. The answer will be something different. I'm editing this question to limit it only to colour prompts. Please note that this question isn't really Fedora specific. more Hi I am not the author of this trick, I found it years ago from a post somewhere, by some forgotten genius. I have implemented this as part of my installation ever since. Wouldn’t it be nice to tell if you are executing commands in superuser mode, simply by looking at the command-line. Well, the following does this, by changing the color of the prompt: red for superuser, blue for normal user. Edit /etc/bashrc:$ sudo cp -a /etc/bashrc /etc/bashrc_b4colors Password: {user password}

#

# The new interactive shell prompt: local BLUE="[$(tput setaf 4)]" local RED="[$(tput setaf 1)]" local RESET="[$(tput sgr0)]" # If id -u returns 0, you have superuser privileges. if [ id -u = 0 ] then PS1="$RED[\u@\h \W]\RESET" else PS1="$BLUE[\u@\h \W]\$ $RESET" fi } and modify the following line: [ "$PS1" = "\s-\v\\$" ] && PS1="[\u@\h \W]\$ " to the following, to call the setprompt function: [ "$PS1" = "\s-\v\\$ " ] && setprompt

Save, and Close the file.

Launch another Terminal window. Note that the prompt color is now blue, and changes to red if you enter superuser mode (i.e., issue the su command). Now, it is easy to find the command-line prompt whenever a command’s output causes the terminal window to scroll. Also, it is easier to tell when you are running with superuser privileges (i.e., a security issue).

more