10 November 2007

Color your (CLI) world!

Here is a quick tip on adding some color to your command line interface (CLI). And, when I say CLI, I mean both the console and your typical graphical terminal as well (xterm, Eterm, etc.).

First, and easiest, is to color the directory listings. There are two ways to do this. One, you can define the variable CLICOLOR in your environment. In bash, you could do this with the command "export CLICOLOR=1". And, if you really like it, then just put that same statement into your ".bash_profile" file and it will become effective every time you log in. See picture above, and then say either "oooh!" or "aaah!"

The other way to do the exact same thing, is to just add the option "G" to your "ls" commands. Above, I could have gotten the exact same output by typing "ls -ltG" regardless of whether the variable CLICOLOR existed in the environment. I guess if you're already using an alias for "ls" this might be a good way to do it. If you're using several aliases for several different sort of "ls" commands, then maybe CLICOLOR would be better. But, they're both easy to set-up and it doesn't much matter which method you use.

Next, let's get some syntax-sensitive color into our text editor. If you use "vi"* and you do any sort of scripting or programming, you should try installing the port "editors/vim". In fact, I install "editors/vim-lite" because I'm really not interested is using the graphical/X vim. Now, if you open any sort of script file that vim can tell is a script or program**, all you've got to do is enable the syntax setting by typing ":syntax enable" (make this automatic by putting it into your "~/.vimrc" file, colon and all). Once this setting is enabled, vim will color the different words and comments on the screen according to the syntax of the appropriate language -- as long as it has rules settings for that language -- and it has rules already for most. I can tell you it works nicely out of the box for shell and python scripts. If you don't have any scripts or code files handy, see the magic at work on one of the scripts that's already on a FreeBSD box. Maybe try, "vim -R /etc/rc.sendmail". See the screen snip below. Say "aaah!" or "oooh!" as appropriate.


Being an old crank, I've been known to poo-poo this fancy color stuff. By golly, when I was your age we had amber-only WYSE terminals that burned off your eyelashes and warmed all coffee within a 5 foot radius! But, I've got to admit, once you've had a taste of this syntax-sensitive, visual sugar, there's no going back. It really helps you to take in your scripts a lot quicker. And, once you're used to it, you'll howl the next time you have to script or program without it.

*(If you don't use "vi", then I recommend it as a great CLI tool -- and, obviously, its clone "vim", too. Be aware that they're not really, really easy to learn. :) But, they're worth the trouble and can help you to navigate and change files much more quickly than any standard editor.)
**(The way vim normally tells which syntax to use is via the interpreter defined after the shebang on the first line of the script. It might also use file extensions when there is no interpreter defined.)
***(Cool wallpaper behind my transparent Eterm is from Digital Blasphemy.)

Labels: ,

0 Comments:

Post a Comment

<< Home