Hi
I am not the author of this trick, I found it years ago from a post somewhere, by some forgotten genius. I have implemented this as part of my installation ever since.
Wouldn’t it be nice to tell if you are executing commands in superuser mode, simply by looking at the command-line. Well, the following does this, by changing the color of the prompt: red for superuser, blue for normal user.
Edit /etc/bashrc:
$ sudo cp -a /etc/bashrc /etc/bashrc_b4colors
Password: {user password}
$ sudo vim /etc/bashrc
Password: {user password}
Add the following to the top of the file to change the interactive shell’s
command-line prompt ##(i.e., BLUE =
user; RED = superuser):
Set the command-line prompt's color:
blue == user
red == superuser function setprompt {
The old interactive shell prompt:
PS1="[\u@\h \W]\$ "
#
The new interactive shell prompt: local BLUE="[$(tput setaf 4)]" local
RED="[$(tput setaf 1)]" local
RESET="[$(tput sgr0)]"
If id -u
returns 0, you have superuser privileges. if [ id -u
= 0
] then PS1="$RED[\u@\h \W]\$ $RESET"
else PS1="$BLUE[\u@\h \W]\$ $RESET"
fi }
and modify the following line:
[ "$PS1" = "\s-\v\\$ " ] && PS1="[\u@\h \W]\$ "
to the following, to call the setprompt function:
[ "$PS1" = "\s-\v\\$ " ] && setprompt
Save, and Close the file.
Launch another Terminal window. Note that the prompt color is now blue, and changes to red if you enter superuser mode (i.e., issue the su command).
Now, it is easy to find the command-line prompt whenever a command’s output causes the terminal window to scroll. Also, it is easier to tell when you are running with superuser privileges (i.e., a security issue).
Changing the prompt colour based on the UID isn't a bash upstream feature, rather a distro customisation. In Mageia they use a package called colorprompt to achieve that; basically they use this .sh file to do that (go there and click "download" to get it). You can then put it in /etc/profile.d/ and it should work.
Note that I haven't tested it myself.
As for vim, what's the mimetype of the file you're editing?