5

I am trying to change the color of the prompt in my terminal to green text for the user at the start and white ~

Currently my .bash_profile file is configured the following way to provide a yellow color for the user name and a pink color for the ~

PS1='\e[33;1m\u@\h: \e[31m\W\e[0m\$'

Does anyone know how to modify the above to change to the colors to green and white?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156

4 Answers4

7
PS1='\e[32;1m\u@\h: \e[37m\W\e[0m\$'

The numbers after the [ are the color codes. See this reference.

bishop
  • 37,830
  • 11
  • 104
  • 139
  • Unreal! Legend, thanks! can accept the answer in 2 mins –  Aug 30 '16 at 16:33
  • do you know who to put a space between the name of the folder and the dollar sign? currently it looks like this `directory$ touch index.html` –  Sep 08 '16 at 15:21
  • 1
    `PS1='\e[32;1m\u@\h: \e[37m\W\e[0m \$'` should result in `directory $`. (Keep the slash and dollar sign together.) – bishop Sep 08 '16 at 15:46
2

You are looking to change the ANSI escape sequences, specifically the colors.

\e[...m takes a semicolon-separate list of codes to manipulate how the following text is displayed. 33 represents yellow foreground text, 1 represents bold text, 31 represents red foreground text, and 0 resets all values (foreground and background colors, styles, etc) to their terminal defaults.

# Make text yellow and bold, then make it red (keeping it bold)
# and finally restore the default
PS1='\e[33;1m\u@\h: \e[31m\W\e[0m\$'

To use green/white instead of yellow/red, change 33 to 32 and 31 to 37. Also, be sure to enclose characters that do not take up any space on screen inside \[...\] so that the shell can properly determine the length of your prompt.

PS1='\[\e[32;1m\]\u@\h: \[\e[37m\]\W\[\e[0m\]\$'

This assumes that your terminal understands ANSI escape sequences; a more portable method is to use tput to output the codes your actual terminal uses:

PS1='\[$(tput bold; tput setaf 2)\u@\h: \[$(tput setaf y)\]\W$(tput sgr0)\$ '

zsh, incidentally, makes this much easier; it has built-in escapes for changing the color in a terminal-independent way:

# 1. Everything between %B and %b is in bold
# 2. Everything between %F{x} and %f is in a different color;
#    x can be a color name, and you can switch from one
#    color to another without using %f
# 3. zsh is smart enough to account for built-in escapes when
#    computing the prompt lenght, so no equivalent of \[...\]
#    is needed
# 4. %n is the same as \u
# 5. %m is the same as \h
# 6. %~ is roughly the same as \W
# 7. %# is roughly the same as \$
PS1='%B%F{green}%n@%m: %F{white}%~%b%f%# '
chepner
  • 497,756
  • 71
  • 530
  • 681
  • thanks @chepner! gave you the up vote but already gave the green tick! thanks for your help tho –  Aug 30 '16 at 16:37
  • You can change answers if you like (not saying you need to, though). – chepner Aug 30 '16 at 16:53
0

This approach has two benefits over the others:

  1. it brackets the escape sequences using \[ and ``]to prevent character count problems withCTRL-A` editing of wrapped lines
  2. it uses tput to change the colors so that it is a little more self-documenting what is being done:

    PS1='\[$(tput setaf 2)\]\u@\h: \[$(tput setaf 7)\]\W\[$(tput sgr0)\]\$'

Greg Tarsa
  • 1,622
  • 13
  • 18
0

To change color of prompt in each section, try to use this simple repo:

https://github.com/joenmarz/bashrc-alias

You can change the color of each prompt section like:

  • username
  • '@' sign
  • hostname
  • time
  • bracket sign '[' and ']'
  • root indicator (# for root user), ($ for non-root user)

The look of your prompt will be like:

[username@hostname 00:00 AM ~/working/directory $]

Go to line 33 of this file (bashrc-alias/.bashrc) to customize each prompt section color variables:

  • open_brk_color     to change open bracket color
  • closed_brk_color     to change closed bracket color
  • at_color     to change '@' sign color
  • username_color     to change username color
  • hostname_color     to change hostname color
  • time_color     to change time prompt color
  • wd_color     to change working directory prompt color
  • ri_color     to change root indicator color

How To Install

  1. Clone this repository at your home directory git clone https://github.com/joenmarz/bashrc-alias
  2. Add the file path inside your existing ~/.bash file: . /home/$USER/bashrc-alias/aliases
  3. refesh your .bashrc source by typing: source ~/.bashrc

I made it easier by adding some variables to each prompt section.

JoenMarz
  • 143
  • 3
  • 13