The information in this thread has been moved to https://help.ubuntu.com/community/CustomizingBashPrompt
A thread for discussion of the wiki page only can be found here http://ubuntuforums.org/showthread.php?t=2012407
Thread closed.
If you have difficulties using the terminal because the prompt isn't visible enough or you would simply like it to look nicer, this howto is for you. It should work on any distribution if you use bash shell, just don't expect your existing .bashrc file to look like Ubuntu's. I've attached a before and after picture of my terminal as an example of what you could do.
So let's get started. Fire up your terminal and stay in your home directory.
1. Backup
First, let's backup our .bashrc file since we're about to make some changes to it:
Code:
cp .bashrc .bashrc-backup
The .bashrc file tells bash what to do when terminal is started, including how to show prompt.
2. Preparing the .bashrc
Use your favorite text editor to edit the original .bashrc file, eg.:Find this part:
Code:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Bash reads PS1 variable to see how to show primary prompt and PS2 for a secondary prompt (used when writing multi-line commands). In this "if" block, the first PS1 is a prompt which will be shown when color is turned on, and the second one (after "else") is being used when no color is desired. As you can see, color codes are quite tiring to use. Imagine you have to write things like \[\033[01;32m\] all over again. It's horrible. Fortunetly, bash lets us define variables that make our life easier. So paste this code above that "if" block:
Code:
# ANSI color codes
RS="\[\033[0m\]" # reset
HC="\[\033[1m\]" # hicolor
UL="\[\033[4m\]" # underline
INV="\[\033[7m\]" # inverse background and foreground
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGRN="\[\033[32m\]" # foreground green
FYEL="\[\033[33m\]" # foreground yellow
FBLE="\[\033[34m\]" # foreground blue
FMAG="\[\033[35m\]" # foreground magenta
FCYN="\[\033[36m\]" # foreground cyan
FWHT="\[\033[37m\]" # foreground white
BBLK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGRN="\[\033[42m\]" # background green
BYEL="\[\033[43m\]" # background yellow
BBLE="\[\033[44m\]" # background blue
BMAG="\[\033[45m\]" # background magenta
BCYN="\[\033[46m\]" # background cyan
BWHT="\[\033[47m\]" # background white
Now you don't have to write all that ANSI code over and over again. For instance, you can just use $FBLE to set foreground (text) to blue, instead of \[\033[34m\].
3. Setting the prompt without color
Now that you have this prepared, you can design your own prompt. Let's start with the one without color for simplicity (it's the one just below the "else" line). First, comment in the default PS1 that's already there to ignore it (put a # in front of the line), and then define your own PS1 and PS2 just below it.
I'll explain how I did mine. This is how they are defined:
Code:
PS1="[ ${debian_chroot:+($debian_chroot)}\u: \w ]\\$ "
PS2="> "
Secondary prompt is simple. It shows just >.
However, the primary one is a little bit more complicated. It contains some special characters for additional info about a session. You can find a list of those special characters in bash's man page under section PROMPTING:I used \u (username), \w (current working directory) and, of course, \\$ (the $ or # symbol, depending on your privileges). You don't need to worry about what "${debian_chroot:+($debian_chroot)}" means, just always copy-paste it in front of "/u". So my primary prompt will be shown like this (when I'm in my home directory): [ penguin: ~ ]$
4. Setting the prompt with color
If you want to use a colored prompt, you must find the line in your .bashrc that says #force_color_prompt=yes and remove # from it. This means that bash will from now on prefer what is defined above the "else" line. Comment in the old PS1 that's located there just like you did before, and then copy-paste your PS1 and PS2 from below "else" (colorless versions). You now have the base layout and you just need to add some color to it.
The simplest way to deal with it is by inserting variables from that big chunk of code you pasted before. First put a $RS at the end of both PS1 and PS2 so you don't change formating of other text in the terminal. All ANSI color codes change only the text behind them, and you can only turn off $HC, $UL and $INV codes by using $RS. Here's how I added color to my prompt:
Code:
PS1="$HC$FYEL[ $FBLE${debian_chroot:+($debian_chroot)}\u$FYEL: $FBLE\w $FYEL]\\$ $RS"
PS2="$HC$FYEL> $RS"
I used hicolor at the beginning and left it on until that last $RS. Then I just switched between colors I wanted (blue and yellow). So I got this prompt: [ penguin: ~ ]$
When you want to see what you've done, just save changes to .bashrc file, start another terminal and enjoy your new prompt. If you want to see how your secondary prompt looks like, enter just \ as a command in the terminal.
Tip: If you're using gnome-terminal and want to fine tune your color selection, right click the terminal, choose Edit Current Profile... and go to Colors tab. Note that these changes effect the complete terminal, not just prompt. I don't know how to do this for any other terminal emulators, I'm a GNOME man.
5. Reverting to old settings
If for some reason you wish to return the old prompt or if you broke your .bashrc, the easiest way is to just recover old .bashrc file you backed up. Run a terminal and use:
Code:
cp .bashrc-backup .bashrc
Voila! Everything is back to normal.
For the end
I tried to make this howto as simple to understand as I could. If you don't understand or don't know how to do something, if you have a suggestion or think I made a mistake somewhere, feel free to contact me. This is the first howto I wrote so any feedback is appreciated.
This is just my small way to say thanks to people in this forum who helped me a lot, so ... thanks.