Is it possible to change the colors in the command prompt for the user@computer
, as well as the the current directory and command parts of the prompt display?
I've already seen something like this done by OSX users, but I don't know how to do the same thing in gnome terminal (I can only change foreground and background colors).
It'd be very useful when, for example, trying to compile programs that have errors, since long, unformatted messages make it hard to distinguish which lines are commands and which are output.
You can edit the settings editing the file:
~/.bashrc
.Open the file:
gedit ~/.bashrc
.Look for the line with
#force_color_prompt=yes
and uncomment (delete the#
).Look for the line below
if [ "$color_prompt" = yes ]; then
that should looks like:Pay attention at the part
\u@\h
it is saying "user@host" and the number before it\[\033[01;32m\]
indicates the color. This is what you have to change. For example, lets change the user to purple, the "@" to black and host to green. Edit the line so it looks like:Result:
The colors numbers are:
References: 1, 2.
You can try the BashrcGenerator. This is by far the easiest way to get a prompt like you want. I've noticed that the colors defined here may be different from your own system, but that's a small issue. With the generated code you can change the colors yourself.
Server user:
Server root:
And if needed you can change hostname color to reflect different type of servers.
I use different format for my local computer:
My favorite now:
This last prompt has one nice touch. It adds a newline after the prompt, and an empty newline before. Now you can display the complete directory path without problem, and it makes it more clear where a new command starts, in case of long output.
Another update, as ZSH is now the default shell on Macos. This is to be edited in .zshrc:
For details, see this detailed HOWTO.
In short, you can alter the prompt by editing the $PS1 environment variable. There's so much to say here, that I'll just show you my prompt and refer you to the link above for more details.
The color-related parts are in the function
setPrompt
:In addition to colors, my prompt has a few other features, such as abbreviated directory names (see the function
bash_prompt_shortener
), automatic display of the last command's exit status if nonzero (functioncheckExitStatus
), and display of the time in the rightmost columns (functionprint_prompt_time
).You can use a bash generator service. For example, I like to make it through Ezprompt. This is an example I made:
After getting the bash results you are satisfied with, edit your prompt by running the following
export
command or by editing the~/.bashrc
file with thenano
text editor orvim
:and adding the
export
command:I wrote a file that can help you customise everything
Execute the file with a leading . to make it run on the same terminal else it will create a child terminal and results thereof will not be seen. Like this:
. shortern_path_terminal.sh
How it works:
The variable PS1 controls the display of the terminal.
u - Refers to the user
w - Refers to the current working directory
W - Shows the full path up to the current working directory
h - Displays the name of the system
[\033[01;32m] - Precedes the texture of text
01 bold
00 not bold
{colour}m
Reference