Okay, here’s another.
MAKE THE PROMPT PRETTY
I spend untold hours at the shell prompt. No, no traipsing through menus for me. Clickity-clack on the keyboard and it’s done, faster than you can say “get off my lawn!”
Alas, on Linux the terminal prompt string is always initialized to something kind of ugly like
eirinn@go-braugh-pc:~$
(Where Eirinn is using her computer, which she has named “go-braugh-pc” for network identification, and the current working directory is her home folder.)
Nicer terminal emulators even try helping with that jumble by highlighting the different parts with pretty colors.
But for a simple home PC, you just don’t need all that stuff for your local hacking. On Windows all you need to see is the current working directory.
Okay, so, the current username is nice too. We’ll keep that.
AN ASIDE FOR SANITY
First, we are absolutely
not going to change the
system prompt. Just
my local user prompt. For example, if
Seamus comes and sits down and types
su seamus
, he wants to see his prompt, not
Eirinn’s.
Likewise, if Eirinn use internet magic to log in on another computer somewhere, she wants to see a different prompt — the default one telling her which computer she is logged into is a Good Thing™.
And it is especially important when switching to
root, where you need to sit on your hands every time you even consider pressing
Enter. A switch from your everyday, friendly-looking prompt to something that screams “YER NOT IN KANSAS ANYMORE DOROTHY!” is A Very Good Thing™.
Alright, enough rationalizing about it. Let’s make stuff happen.
RC FILES
An R-C file is the old name pirates used to refer to excess rum consumption when managing CTSS systems. These days we just call them config scripts or something stupid like that.
In your home directory you have a number of files beginning with a period (or “dot”), some of which also end with “rc”. For some odd reason we call them dot-files. You can only see them if you give
ls the correct Vulcan nerve pinch. Open up a terminal (press
Ctrl+Alt+T or mouse over to the start menu). Type:
ls -adF .*
You’ll notice there are quite a few dot-files. Some of them are directories, even.
The one we are interested in is named “
.bashrc”. Go ahead and open it up in a text editor. (Type
xed .bashrc
to open it in the Linux version of
Notepad. And if you are unfortunate enough to have opened it with a Dark theme, you can go to
View →
Highlight mode and select “Plain Text” from the top. That or spend time dinking with the highlighting theme. You might want to turn off Word Wrap as well. Or if you are comfortable with
Nano or
Emacs or
Vim or something, you can type that instead:
vi .bashrc
.)
Remember, when on Linux, if you don’t know what it is, don’t touch it! This will remain true as we look through this file.
PS1 –IS– THE DROID YOU ARE LOOKING FOR
About 60 lines down you’ll see stuff about “
$color_prompt”. Alternately, you can just search for the text “
PS1”. (That’s papa sierra
one.)
You will find an unholy-looking incantation something like:
|
PS1='\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\ [\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
|
Don’t worry, it isn’t as bad as it looks. (It’s worse.)
A few lines below it you’ll also see:
1 2
|
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
|
That line is a whole lot less scary. The difference is that the first line has terminal control color codes mixed in to it, but otherwise the two lines are the same. And these are the two lines we will modify. (There is also a line somewhere below those two for an xterm, but unless you intend to dial in to your computer from the library or something, you can safely ignore it.)
What it says is this:
• If you are using
chroot, show it in parentheses.
• Show the username.
• Show an
@
.
• Show the hostname (what your computer calls itself on the network).
• Show a
:
• Show the current working directory.
• Show a
$
(or
%
) which means you are a normal user.
I am going to change that to get rid of the hostname and uncrunch it a little. We’ll keep the chroot string — it’s dead useful when developing programs, and besides, I’d definitely like some indication to remind me if I chrooted. My new prompt string looks like this:
PS1='\n${debian_chroot:+($debian_chroot) }\u \w $ '
Now I see a much less cluttered:
michael ~ $
It even adds a blank space between the end of the last command and the new prompt. Very pretty!
PS1 IN COLOR
All righty then, time to get out the magic chickens. We’re gonna wave a couple of them around.
The first is this little script obtained from
https://unix.stackexchange.com/a/269085/451669:
list-colors.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#!/bin/bash
color(){
for c; do
printf '\e[48;5;%dm%03d' $c $c
done
printf '\e[0m \n'
}
IFS=$' \t\n'
color {0..15}
for ((i=0;i<6;i++)); do
color $(seq $((i*36+16)) $((i*36+51)))
done
color {232..255}
|
Save it somewhere useful. Don’t forget to make it executable! (
chmod +x list-colors.sh
)
The next is a website:
https://www.kirsle.net/wizards/ps1.html
Skip all the others that claim to make PS1s for you. They suck.
Noah explains why on that page itself, but you can just Trust Me™ that he’s one of the Elder Ones.
On his site, build your PS1. The components of mine look like this:
Text : "\n${debian_chroot:+($debian_chroot) }"
Set color : Green
Text : "\u"
Set color : Blue
Text : " \w"
Set color : Magenta
Text : " $"
Reset style
Text : " "
Copy from the
Result text box below and replace the color PS1 string in your
.bashrc.
Now run that little shell script in a terminal (
./list-colors.sh
). It will print out all the colors available to you and their indices. For example, color
001 is bright red.
Don’t worry, the color is what matters. Color 001 is simply bright red and can be used for both foreground and background colors.
Choose the color you like best for each part of your prompt and change the “
tput setaf 4” to use the index of the color you wish to have it. I have a “dark” desktop theme going, so my final prompt string reads:
|
PS1="\n${debian_chroot:+($debian_chroot) }\[$(tput setaf 10)\]\u\[$(tput setaf 12)\] \w\[$(tput setaf 13)\] $\[$(tput sgr0)\] "
|
Save the changes and start a new terminal emulator to see the new prompt.
while (!satisfied)
{
edit_and_save( ".bashrc" );
open_new_terminal_and_view_results();
}
That’s it! You have now conquered Level 1 of the deep magic.
More to come... Post size limit ’n all...