![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
|
Tutorials & Tips The place to find Ubuntu related Tips & Tricks. |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
A Carafe of Ubuntu
![]() Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
|
HOWTO: Customizing bash prompt
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 2. Preparing the .bashrc Use your favorite text editor to edit the original .bashrc file, eg.: Code:
gedit .bashrc 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
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 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="> "
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: Code:
man bash 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"
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 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.
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield Last edited by MiCovran; May 18th, 2009 at 07:18 AM.. Reason: Updated for new .bashrc |
|
|
|
|
|
#2 |
|
Skinny Soy Caramel Ubuntu
![]() |
Re: HOWTO: Customizing bash prompt
Great How-to.... I will try to customize it...
Thanks. |
|
|
|
|
|
#3 |
|
First Cup of Ubuntu
![]() |
Re: HOWTO: Customizing bash prompt
this is great, when I get home, I'll try it
__________________
Hammerfall is real music for programmers!!! HAMMERFALL RULES!!! |
|
|
|
|
|
#4 | ||
|
A Carafe of Ubuntu
![]() Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
|
Re: HOWTO: Customizing bash prompt
Quote:
Quote:
![]() BTW, I felt like section 3 - setting the prompt - wasn't clear enough for people with no scripting or programming experience, so I rewrote it in a more "step-by-step" approach.
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield Last edited by MiCovran; November 17th, 2007 at 11:17 AM.. |
||
|
|
|
|
|
#5 |
|
First Cup of Ubuntu
![]() Join Date: Dec 2007
Location: Sweden
Beans: 10
Ubuntu 7.10 Gutsy Gibbon
|
Re: HOWTO: Customizing bash prompt
Good stuff, now I can finally see where my latest compile results start.
|
|
|
|
|
|
#6 | |
|
A Carafe of Ubuntu
![]() Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
|
Re: HOWTO: Customizing bash prompt
Quote:
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield |
|
|
|
|
|
|
#7 |
|
Just Give Me the Beans!
![]() Join Date: Aug 2008
Beans: 56
Ubuntu 9.04 Jaunty Jackalope
|
Re: HOWTO: Customizing bash prompt
Thanks for that MiCovran, very helpful!
Just a small correction... The ANSI color variables must be defined prior to the prompts that use them, otherwise it doesn't work, at least for me. So they can't be pasted at the end of file, but anywhere before the first attempted usage. Thanks again, uvdevnull |
|
|
|
|
|
#8 |
|
First Cup of Ubuntu
![]() Join Date: Jul 2008
Beans: 2
|
Re: HOWTO: Customizing bash prompt
|
|
|
|
|
|
#9 |
|
Skinny Extra Sweet Ubuntu
![]() Join Date: Sep 2006
Beans: 3,171
Ubuntu Karmic Koala (testing)
|
Re: HOWTO: Customizing bash prompt
Great howto, thanks!
|
|
|
|
|
|
#10 | |
|
A Carafe of Ubuntu
![]() Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
|
Re: HOWTO: Customizing bash prompt
Quote:
It's actually quite simple to customize it, you just have to play a little bit. For changing the colour of the prompt just play with all the different variables you pasted into your .bashrc file (it's that big chunk of code from the howto). For instance, if you want to change colour of text in some part of the prompt to green, first find the "foreground green" line. There you'll see that the variable name is "FGRN" (on the beginning of the line). Then just add "$FGRN" before the text you want to change (in the "PS1" variable). However, if you want to change contents of the prompt, then take a look at the bash man file (also available at this page): Code:
man bash As an example, PS1="$FGRN(My first bash prompt) \t $RS" would simply show "(My first bash prompt)" and current time after it. All in green, of course.
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield |
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|