Find where your PS1
is set in your .bashrc
and insert '\[\e[1m\]'
at the beginning and \[\e[0m\]
at the end.
\[
and \]
are necessary so the shell knows the mess inside takes up 0 space on the screen, which prevents some screwed up behavior when doing line-editing. You don't need to worry too much about it.
\e[
is known as the CSI (control sequence introducer). You'll see it used in most of the codes listed on the referenced Wikipedia page. \e
means the escape character.
- If you look in the SGR table on the Wikipedia page, you'll see the 1 is the number for bright/bold text, and 0 is for reset. Thus
CSI 1m
turns on bold and CSI 0m
resets the font so the rest of your text is normal.
Wikipedia has a full list of ANSI escape codes that you can use if your terminal emulator supports it.
Edit
For portability and readability, you should use tput
instead of hard-coding escape codes. The only downside is the tput
approach won't work with terminals that support ANSI codes but have broken or missing terminfo databases, but in that case the broken terminfo is a bigger problem as many of your console apps that rely on terminfo may not work properly.
Here's an example of what I do in my .bashrc
:
# color names for readibility
reset=$(tput sgr0)
bold=$(tput bold)
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
user_color=$green
[ "$UID" -eq 0 ] && { user_color=$red; }
PS1="\[$reset\][\[$cyan\]\A\[$reset\]]\[$user_color\]\u@\h(\l)\
\[$white\]:\[$blue\]\W\[$reset\][\[$yellow\]\$?\[$reset\]]\[$white\]\
\\$\[$reset\] "
Here's what a genericized version of mine would look like. The 0
is the exit status of the last command.
