change color of prompt in bash profile - bash

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?
Thanks!

PS1='\e[32;1m\u#\h: \e[37m\W\e[0m\$'
The numbers after the [ are the color codes. See this reference.

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%# '

This approach has two benefits over the others:
it brackets the escape sequences using \[ and ``]to prevent character count problems withCTRL-A` editing of wrapped lines
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)\]\$'

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
Clone this repository at your home directory
git clone https://github.com/joenmarz/bashrc-alias
Add the file path inside your existing ~/.bash file:
. /home/$USER/bashrc-alias/aliases
refesh your .bashrc source by typing:
source ~/.bashrc
I made it easier by adding some variables to each prompt section.

Related

.tmux.conf color codes causing strange output

Follow-up to: Colored text printing spaces in shell script
I have a script I wrote (with some help from #Barmar) which displays my current CPU and memory load visually. The output looks like so:
I then put the following into my .tmux.conf file:
set -g status-right "#(~/load.sh)"
I reload my tmux config and get the following output in the bottom-right:
There are two issues:
The CPU section should contain 11 characters: a "clear color code" character (tput sgr0) and 10 spaces. Instead it contains (B[m
The MEM section... should exist. The entire [| ] has turned into a y> -- I don't even know how the square bracket is missing, that should get printed before any color codes or weird control characters
Can tmux status bars simply not contain color?
tmux status bars don't use ANSI escape codes, they use the same color code format as other things in tmux. You want something more like (assuming 256-color mode):
#[fg=colour28 bg=colour250]Hello World!

Process the bash output with color code

I have a Mac App (CodeRunner) that executes a script in login mode and shows the output in a window.
In error condition, it returns the code with escape characters to make the output hard to read.
Is there a way to process the color code? Is there a filter to remove the color code?
With Mac OS X, I used stderred library.
#export DYLD_INSERT_LIBRARIES="/LOCATION_OF_THE_LIB/libstderred.dylib${DYLD_INSERT_LIBRARIES:+:$DYLD_INSERT_LIBRARIES}"
Removing this library setup shows the strings without the code.
If I understand your question, the easiest way to control color in bash (or any terminal that supports them) is with ANSI escape sequences. Example:
#!/bin/bash
blue='\e[0;34m' # ${blue}
green='\e[0;32m' # ${green}
nc='\e[0m' # ${nc} (no color - disables previous color selection)
printf "${blue}This text is blue, ${green}this is green${nc}\n"
exit 0
There are a number of complete references for the ANSI sequences available on the web, but for basic colors, the following is generally sufficient:
black='\e[0;30m' # ${black}
blue='\e[0;34m' # ${blue}
green='\e[0;32m' # ${green}
cyan='\e[0;36m' # ${cyan}
red='\e[0;31m' # ${red}
purple='\e[0;35m' # ${purple}
brown='\e[0;33m' # ${brown}
lightgray='\e[0;37m' # ${lightgray}
darkgray='\e[1;30m' # ${darkgray}
lightblue='\e[1;34m' # ${lightblue}
lightgreen='\e[1;32m' # ${lightgreen}
lightcyan='\e[1;36m' # ${lightcyan}
lightred='\e[1;31m' # ${lightred}
lightpurple='\e[1;35m' # ${lightpurple}
yellow='\e[1;33m' # ${yellow}
white='\e[1;37m' # ${white}
nc='\e[0m' # ${nc} (no color - disables previous color selection)
Note: always reset the color to default at the end of a string with ${nc} ('\e[0m') to prevent continuation of the color after your script finishes. Lastly, if using echo, you must use echo -e to process the ANSI codes.
Note2: since you are seeing the codes and not the color, you have several possibilities (1) you are using echo without the -e option to allow proper interpretation of the codes (or something similar); or (2) the terminal you are using lacks color capability (though that is doubtful, as just about all modern terminals are VT based with default color handling available).
As suggesting by the comments below, you can also set color with tput (setab # for background and setaf # for foreground).

How to change RGB colors in Git Bash for windows?

I'm using Git Bash in Windows and for the purposes of my custom git log format, I'd like to modify the terminal's exact RGB color values so I can fine-tune the color outputs. My git log format is as follows in my global .gitconfig:
lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset)%x09%C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset) %C(dim white)%an%C(reset) - %C(white)%s%C(reset)' --branches --remotes --tags
I'd like to define in Git Bash what RGB value actually maps to 'blue', for example. Any idea how I can do this? Step by step instructions would be wonderful.
I setup my .bashrc and it isn't working for some reason. Colors aren't changing :-(. Here is a script I ran to see colors: http://pastebin.com/9EsYmGCj and the result: http://i.imgur.com/1dJ3o1a.png
This works for me to change the text colors used by Git Bash on Windows 7:
Click on the upper left corner of an open Git Bash window (the Git icon in the window frame).
A menu appears (the same that would appear with a regular DOS cmd Window). Choose the last entry: "Properties", UPDATE 2021: "Options..." (thanks AlexD!)
Go to tab "Colors"
Choose radio button "Screen Text"
Remember which color is currently assigned to "Screen Text" in the row of small color boxes (it has a black frame).
Then select the color you want to change by clicking on the corresponding color box. This color is now assigned as "Screen Text", which is what Git Bash uses for regular text. But don't worry, this change is only temporary and needed to modify the value of a color.
Now change the Red/Green/Blue values for the selected color. In my case I wanted to make the fifth color from the left (much) brighter. Let's call it "Color 5". This is the color Git Bash uses to show changed files with "git status". Whenever Git Bash wants to use "Color 5" it will use the new RGB value.
"Screen Text" is now still set to "Color 5". So click on the original color that you have remembered.
The changes made in this way are permanent but only valid for the shortcut you have used to start Git Bash. If you create a new shortcut you are back to the original colors.
If you are using the git-bash command prompt check if you have the file: %USERPROFILE%\.minttyrc
In that file you can fine tune the RGB values of the console colors in this way:
BoldBlack=128,128,128
Red=255,64,40
BoldRed=255,128,64
Green=64,200,64
BoldGreen=64,255,64
Yellow=190,190,0
BoldYellow=255,255,64
Blue=0,128,255
BoldBlue=128,160,255
Magenta=200,64,255
BoldMagenta=255,128,255
Cyan=64,190,190
BoldCyan=128,255,255
White=200,200,200
BoldWhite=255,255,255
For those of you coming here to get an answer to the actual Original Question the answer is to add the following line to the end of:
C:\Program Files\Git\etc\profile.d\git-prompt.sh
LS_COLORS=$LS_COLORS:'di=1;30:' ; export LS_COLORS
You may chose from these colors.
Black 0;30 Dark Gray 1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37
Git bash uses the default Windows console colors which may be tweaked in the registry. E.g. to increase readability, one can change the dark red and the dark magenta to a lighter version by applying the changes as indicated below:
Windows Registry Editor Version 5.00
; Default color scheme
; for Windows command prompt.
; Values stored as 00-BB-GG-RR
[HKEY_CURRENT_USER\Console]
; BLACK DGRAY
"ColorTable00"=dword:00000000
"ColorTable08"=dword:00808080
; BLUE LBLUE
"ColorTable01"=dword:00800000
"ColorTable09"=dword:00ff0000
; GREEN LGREEN
"ColorTable02"=dword:00008000
"ColorTable10"=dword:0000ff00
; CYAN LCYAN
"ColorTable03"=dword:00808000
"ColorTable11"=dword:00ffff00
; RED LRED --> To increase readability, use e.g. 000000aa for "ColorTable04"
"ColorTable04"=dword:00000080
"ColorTable12"=dword:000000ff
; MAGENTA LMAGENTA --> To increase readability, use e.g. 00aa00aa for "ColorTable05"
"ColorTable05"=dword:00800080
"ColorTable13"=dword:00ff00ff
; YELLOW LYELLOW
"ColorTable06"=dword:00008080
"ColorTable14"=dword:0000ffff
; LGRAY WHITE
"ColorTable07"=dword:00c0c0c0
"ColorTable15"=dword:00ffffff
When using MSYSGIT, Git Bash runs in the Windows Command Prompt. Consequently, it uses the colors defined for the terminal. Since Git Bash is a command that is always run, you can set its colors in the Command Prompt's Properties, rather than Defaults.
For instructions on changing the terminal colors, see: https://superuser.com/questions/199764/how-to-change-the-default-color-of-the-command-prompt
Note: I've found the Command Prompt's color options to be a huge pain to configure, as they frequently changed/reset while editing. The trick seems to be to tab out of the input boxes after editing.
Since those color names are linked to ANSI escape codes (as I mentioned in a previous answer), you can try the approach described in this issue:
Just add these to your .bashrc
echo -ne "\e]4;4;#007fff\a" # 4;4 is Dark Blue #005FFF
(pick an rbg value which seems more readable to you for color blue, or choosing from this palette)
2017 Update
Open Gitbash and click the icon in the upper left corner and select "Options"
From the options menu you can configure transparency, foreground color (text), background color, and cursor color. Pretty straightforward and easy.
Windows10 + GitBash: Warning Message Samples
The following samples will print out a red background with white text.
Original colors are RESTORED after the print.
Echo One Line Message:
MSG="MY_WARNING_MESSAGE_TEXT" BG="41m" FG="1m"
echo -en "\033[$FG\033[$BG$MSG\033[0m\n"
Block Of Colored Text with HARDCODED message:
BG="41m" FG="1m"
HD_CAT_VAR=$(cat << 'HEREDOC_CAT_VAR_REGION'
+-------------------------------------+
| |
| HARD_CODED_WARNING_MESSAGE |
| |
+-------------------------------------+
HEREDOC_CAT_VAR_REGION
)
echo -en "\033[$FG\033[$BG$HD_CAT_VAR\033[0m\n"
Block of Colored Text with VARIABLE message:
VARIABLE_WARNING_MESSAGE="OH_NOOOOOO!"
BG="41m" FG="1m"
HD_CAT_VAR=$(cat << HEREDOC_CAT_VAR_REGION
+-------------------------------------+
| |
+-------------------------------------+
$VARIABLE_WARNING_MESSAGE
+-------------------------------------+
| |
+-------------------------------------+
HEREDOC_CAT_VAR_REGION
)
echo -en "\033[$FG\033[$BG$HD_CAT_VAR\033[0m\n"
To change the windows console color, you can use Microsoft's Colortool:
The colortool will work with any .itermcolors scheme.
https://blogs.msdn.microsoft.com/commandline/2017/08/11/introducing-the-windows-console-colortool/
Github:
https://github.com/Microsoft/console/tree/master/tools/ColorTool

Bash, Always display colored bar at top of screen

Is there anyway to modify the bash profile scripts to always display a colored bar at top of screen. I have a requirement to show a colored hostname, username, and ipaddress on the screen at all times, but i don't want to overload PS1 as it would make the prompt take up over half of the default console width.
Not perfect, but this shows you how to fix part of your prompt on the first row of the screen:
PS1='\w \[\e[s\e[1;1H\e[42m\]\h \u ipaddress\[\e[0m\e[u\]\$ '
A breakdown:
\e[s - save the current cursor position
\e[1;1H - move the cursor to row 1, column 1 (numbered from the upper left-hand corner
\e[u - restore the cursor to the previously saved position
\e42m - make the background green
\e0m - restore the default foreground/background colors
\[...\] - enclose the various non-printing characters so that bash can correctly compute the length of the prompt.
Wikipedia lists other escape codes. The two things missing from this answer are how to extend the bar all the way across the string and how to set the correct IP address.
Update: I believe this covers the changes that ruckc made:
PS1='\[\e[s\e[1;1H\e[42m\e[K\h \u ipaddress\e[0m\e[u\]\w \$ '
How about add a \n inside your PS1, so that you always use a new line with full width?
if you are looking for something less hacky (but maybe overkill), consider byobu
https://en.wikipedia.org/wiki/Byobu_(software)
Alternatively, if you are using xterms, you could set the xterm title instead:
export PS1="\[\033]0;\u $(host $(hostname))\007\]\u#\h:\w\$ "
This sets your xterm title, and sets your prompt to contain username#host:pwd.
My .bashrc contains something like this so PS1 is set correctly depending on whether we're in an xterm or not:
if [[ -n "$TERM" ]] ; then
if ( echo $TERM | $GREP -q xterm ) ; then
export PS1="\[\033]0;\u#\h:\w\007\]\u#\h:\w\$ "
else
export PS1="\u#\h:\w\$ "
fi
fi

Custom Oh My Zsh theme: long prompts disappear / cut off

I had a go at making my own Oh My Zsh theme earlier. All is well, except when I type long lines in the prompt (any longer than the line seen below), the line disappears. The line re-appears if I resize the window, however.
Is there something in my theme that is causing this to happen?
If I type an additional character and then erase one, the cursor appears at the edge of the window.
You can view the code for the theme here. Here’s the bit I think we are concerned with:
# Build the prompt
PROMPT='
' # Newline
PROMPT+='${style_user}%n' # Username
PROMPT+='${style_chars}#' # #
PROMPT+='${style_host}%m' # Host
PROMPT+='${style_chars}: ' # :
PROMPT+='${style_path}%c ' # Working directory
PROMPT+='$(git_custom_status)' # Git details
PROMPT+='
' # Newline
PROMPT+='${style_chars}\$${RESET} '
Incidentally, your link is broken, highlighting one of the issues with posting a link to code instead of code itself - any future viewers of your question can't get a full picture.
I think your problem is that the 'color' characters you use should be escaped in a pair of %{...%}:
%{...%}
Include a string as a literal escape sequence. The string within the braces
should not change the cursor position. Brace pairs can nest.
Using your latest commit on github, I don't see this issue - did you fix it? However, I'm seeing some issues with cursor placement and line-drawing, particularly with TAB. When pressing TAB, the cursor is moved up one line:
Pressed TAB here.
Pressed TAB here.
The PROMPT is being re-drawn 'up' one line every time. This is fixed by encapsulating the color codes within the %{...%}:
# Solarized Dark colour scheme
BOLD="%{$(tput bold)%}"
RESET="%{$(tput sgr0)%}"
SOLAR_YELLOW="%{$(tput setaf 136)%}"
SOLAR_ORANGE="%{$(tput setaf 166)%}"
SOLAR_RED="%{$(tput setaf 124)%}"
SOLAR_MAGENTA="%{$(tput setaf 125)%}"
SOLAR_VIOLET="%{$(tput setaf 61)%}"
SOLAR_BLUE="%{$(tput setaf 33)%}"
SOLAR_CYAN="%{$(tput setaf 37)%}"
SOLAR_GREEN="%{$(tput setaf 64)%}"
SOLAR_WHITE="%{$(tput setaf 254)%}"
I'm not 100% sure without the original ~/.zshrc, but this should improve your prompt a little. :)
Apart from the orange, you can also use a terminal-based Solarized profile and the zsh colors, which might be more portable. I couldn't get the orange right without tput, though.
#autoload colors && colors
#SOLAR_YELLOW="%{$fg[yellow]%}"
#SOLAR_ORANGE="%{$(tput setaf 166)%}"
#SOLAR_RED="%{$fg[red]%}"
#SOLAR_MAGENTA="%{$fg[magenta]%}"
#SOLAR_VIOLET="%{$fg_bold[magenta]%}"
#SOLAR_BLUE="%{$fg[blue]%}"
#SOLAR_CYAN="%{$fg[cyan]%}"
#SOLAR_GREEN="%{$fg[green]%}"
#SOLAR_WHITE="%{$fg[white]%}"

Resources