Ask Your Question
2

How do I get a colour prompt?

asked 2014-06-13 01:05:45 -0600

LInux user gravatar image

updated 2014-06-14 09:25:44 -0600

FranciscoD_ gravatar image

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 flag offensive close merge delete

Comments

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?

Ahmad Samir gravatar imageAhmad Samir ( 2014-06-13 08:27:51 -0600 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2014-06-14 09:23:28 -0600

FranciscoD_ gravatar image

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.

edit flag offensive delete link more
0

answered 2014-06-14 10:23:42 -0600

deusdara gravatar image

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}

$ sudo vim /etc/bashrc Password: {user password}

Add the following to the top of the file to change the interactive shell’s

command-line prompt ##(i.e., BLUE = user; RED = superuser):

Set the command-line prompt's color:

blue == user

red == superuser function setprompt {

The old interactive shell prompt:

PS1="[\u@\h \W]\$ "

#

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).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-06-13 01:05:45 -0600

Seen: 10,041 times

Last updated: Jun 14 '14