Human Readable   

 

     
   
     

Adding Some Color to Bash and Console—Day 48

© Copyright Darrell Anderson.

One of the things that has mildly irritated me with Slackware is the hum-drum and boring screen displays when booting, as well as the boring command line prompts. Early in my migration I had solved this problem by adding some color coded echo messages throughout all the rc scripts. I really like this visual feedback and I have not gone overboard with this idea. I previously mentioned my basic approach. I would love to see the Slackware maintainer adopt this idea. I suppose some of the older Slackware purists and crumudegeons might dislike color in the rc scripts, but I think most people would embrace the idea. Nonetheless, I have liberally colorized my rc scripts. With my basic color scheme, I can tell at a glance how my boot process is proceeding.

I also had beautified my command prompt. There are two types of prompts: those from within a terminal inside X and those outside of X. I use Konsole as my xterm and I enjoy this versatile utility. Within that environment I copied an idea from Mandrake and created an environment variable to display my current path in the Konsole title bar. That idea saves me real estate in the typically smaller Konsole window.

Outside of X that envrionmnent varaible is meaningless so I devised a slightly different prompt to help remind me of my current path. All seemed to be working well until one day I typed a command that needed to wrap to the next line. The prompt wrapped but did not perform a line feed. I then tested this in X in Konsole and had the same problem. Well, now what do I do? Back to the web of course!

I did not find my answer immediately. In fact, I gave up after my first search session. Obviously I was not entering the correct keywords to find my needle in the haystack. A few days later, however, I tried again and found my answer.

I was using escape codes to colorize my prompts. I learned that the brackets ( [ and ] ) that are used to identify escape codes must be typed juuuuuust right or bash will interpret those codes as something to do rather than ignore. So I took a long cross-eyed look at my bash prompt configuration and sure enough, I was missing a “\[“ that would solve my problem. I edited, logged out, and then logged in. I typed a long command and then had line feeds along with my line wraps!

Here is my current prompt for my non-X console:

PS1='[\[\033[01;35m\]\u\[\033[00m\]@\[\033[01;36m\]\h\[\033[00m\]]$ '

Here is how I configure prompt and Konsole environment variable:

# are we in an interactive shell?
if [ "$PS1" ]; then
  case $TERM in
    xterm*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
    PS1='[\[\033[01;35m\]\u\[\033[00m\]@\[\033[01;36m\]\h\[\033[00m\]]$ '
   ;;
   *)
   ;;
  esac
fi

All of this is contained in my /etc/profile. You don’t have to like my tastes, but nonetheless you can use the same ideas to beautify your own system.

However, I had noticed that the KDE developers provide a special Kicker option to open a “root console.” This is nice and saves having to open Konsole and typing su root. This special option immediately asks for the root password. Additionally, typing exit closes the entire session whereas typing exit in a mortal user Konsole does not close the window and only terminates the root login.

This is all great! However, I prefer to use a traditional black screen and the bash color system, but the KDE developers open these special root consoles with a yellow background. I understand the design difference—something that looks different to remind users they are logged in as root and not mortal user. All well and fine, but my command prompt already reminds me of this difference. Additionally, the yellow background does not play well if using the bash color system. So off I went looking again to discern how to change the background to black.

The answer lies in some KDE configuration files “hidden” deep within KDE. Find your way into $KDEDIR/share/apps/konsole. Within that directory look for su.desktop. Open the file with a text editor. Scroll to the end of the file.

The first thing you’ll notice is that my complaint about the yellow background is probably a common complaint because the KDE developers have inserted two lines to configure the Konsole schema. All you need do is uncomment the Linux.schema for a black background and comment the BlackOnLightYellow.schema. Save the changes and restart your KDE session. Voila!

Oh, if you are a Midnight Commander user, you’ll probably want the same change for the special Midnight Commander Kicker option too. In the same directory be sure to edit mc.desktop.

One last note. In Windows I can create screen grabs with a simple keyboard combination. I can press Print Screen to capture an entire screen or Alt-Print Screen to capture the active window. The image is copied to the Windows clipboard. This wonderful trick does not exist so easily within the GNU/Linux world. However, the wonderful and progressive KDE developers provide a screeen capture utility called ksnapshot.

I edited my KDE hotkeys. Now when I press Ctrl-Print Screen the ksnapshot utility automatically opens.

I also added a hotkey sequence of Ctrl-Shift-Esc to open the the full blown KDE ksysguard utility. KDE comes preconfigured with Ctrl-Esc to open only the process list portion of ksysguard, but I prefer the full blown program. Windows users should recognize my new keyboard sequence as the same one used to open the Windows Task Manger.

Now I need to find a console-based screen capture utility. I have already performed some mild web searching but has so far struck out. I would love to capture some of my boot screens so readers can see how my colorized rc script system looks.

Finis.

Table of Contents