Putting colour in your shell

In the past when I have tinkered with Linux I noticed that the shell has colour codings for various items like directories etc.

Is it possible to do the same in FreeBSD? I find having the colour coding can help me find things in a directory with many items alot faster.

Also, is it possible for FreeBSD to show what directory I am in without having to continually type pwd? In Linux it use to show this where FreeBSD puts the hostname.
 
Yes, it is possible. Simply use $ ls -G. For convenience, I use
Code:
alias ls        ls -FG
in ~/.cshrc. It is also possible to modify the colours as well, but I just use the defaults.

As for the path, set a shell prompt that shows the user, host, and path. My ~/.cshrc:
Code:
set prompt = "%B[%n@%m %~]%# "

  • %B Starts/stops boldfacing mode
  • %n is the username
  • %m is the host
  • %~ is the path
  • %# is the prompt ($ for normal users and # for root)

More options can be found here: tcsh(1).
 
I put:
Code:
set prompt = "[%B%n@%m %~]%# "

in my ~/.cshrc file and logged out and back in again but I didn't seem to make a difference. Is there a service I have to restart for this change to kick in?
 
No, there shouldn't be. Try $ source ~/.cshrc.

Are you using csh or tcsh? Different shells would require different settings. $ echo $SHELL should tell you which shell you're using.
 
Example for sh:

Put in your ~/.profile: (/etc/profile for everybody)

Code:
export LSCOLORS=hxgxbxBxHxfxDxHxHxhxhx

I thougt it was in the handbook, but can't find it no more.
Here's another BSD-related page about it:
http://plug-and-pray.blogspot.com/2008/02/lscolors.html

color example for sh script:

Code:
#!/bin/sh
colred ()
{
  printf '\033[1;31;40m'
}

colnormal ()
{
  printf '\033[0m'
}

# begin

printf "This is "
colred
printf "red"
colnormal
printf ", right?\n"
 
If you use (t)csh there's no need to alias ls -G just:

$ setenv CLICOLOR

You can change you default shell with $ chsh. Try not to change root's default shell.
 
I use the builtin ls(1) with colour support as shown above, but you can also install misc/gnuls if you want the same behaviour as linux's ls.
 
I tried:

Code:
alias ls        ls -FGl

which worked but when I logged out and back in again I was back to the defaults?
 
You need to add it to your shell's startup file. For csh, ~/.cshrc. For sh, ~/.profile. For bash, ~/.bash_profile.

See your shell's man page for more info.

My .bash_profile for example:

Code:
export LSCOLORS=Hxfxcxdxbxegedabagacad
alias ls='ls -FG'
 
I just installed bash, very nice!

I set the ~/.bash_profile file as follows:

Code:
DIR=Ex
SYM_LINK=Gx
SOCKET=Fx
PIPE=dx
EXE=Cx
BLOCK_SP=Dx
CHAR_SP=Dx
EXE_SUID=hb
EXE_GUID=ad
DIR_STICKY=Ex
DIR_WO_STICKY=Ex

export LSCOLORS="$DIR$SYM_LINK$SOCKET$PIPE$EXE\
$BLOCK_SP$CHAR_SP$EXE_SUID$EXE_GUID\
$DIR_STICKY$DIR_WO_STICKY"

export CLICOLOR="YES"

Much easier on the eyes and it shows the directory I am in so I dont have to contunally type pwd.

I tried setting

Code:
alias ls='ls -FGl'

in roots .bash_profile but it doesn't seem to work? Works fine when I login as a normal user but not when logged in as root, whys this? The colour coding looks fine, just not the ls alias...
 
xy16644 said:
I tried setting

Code:
alias ls='ls -FGl'

in roots .bash_profile but it doesn't seem to work? Works fine when I login as a normal user but not when logged in as root, whys this? The colour coding looks fine, just not the ls alias...

Is root using bash?
 
xy16644 said:
Is there a reason for leaving roots shell as csh?

AFAIK there's no real reason, changing root's shell is just considered to be bad practice.
If you want a root account with a different shell consider enabling the toor account and change the shell for that.
 
This is my .cshrc shell tcsh

Code:
if ($?prompt) then
        # An interactive shell -- set some stuff up
        set filec
        set history = 100
        set savehist = 100
        set autolist = TAB
        set mail = (/var/mail/$USER)
        if ( $?tcsh ) then
                bindkey "^W" backward-delete-word
                bindkey -k up history-search-backward
                bindkey -k down history-search-forward
                if ($uid) then
                        set prompt = "%{\033[32m%}%B<%n@%{\033[33m%}%m%{\033[33m%}>%b%{\033[0m%}%/ # "
                else
                        set prompt = "%{\033[31m%}%B<root@%{\033[33m%}%m%{\033[33m%}>%b%{\033[0m%}%/ # "
                endif
        endif
endif
 
I don't have any colors in my prompt but I did add something to make xterm (or a putty session) change the title:

Code:
switch($TERM)
	case "xterm*":
		setenv TITLE "%{\033]0;%n@%m:%~\007%}"
		breaksw
	default:
		setenv TITLE ""
		breaksw
endsw

<snip>

	if ( $?tcsh ) then
        <snip>
                set prompt = "${TITLE}%n@%m:%~%#"
        endif
endif
 
xy16644 said:
Is there a reason for leaving roots shell as csh?

I like the tab-completion and Ctrl-R history editing of bash. So bash is my root shell.

If your /usr/local is another filesystem, be sure bash is in your root filesystem in case your /usr/local refuses to get mounted for any reason.
 
Add the following to your .cshrc to enable tab completion and correction:

Code:
if ($?prompt) then
        # An interactive shell -- set some stuff up
        set autolist
        set autocorrect =       ambiguous
        set complete    =       enhance
        set correct     =       cmd
endif

To use Ctrl-R to search backwards the history add the following to .cshrc:
Code:
if ( $?tcsh ) then
                       bindkey "^R" i-search-back
endif

Essencially there isn't anything that bash does that t/csh can't do.
 
I like my tcsh prompt like this:
Code:
Sat, 01. Aug 2009,14:10:00
<FreeBSD 8.0-BETA2> [maggie:~]
lars@pts/5 >

The code for this is:
Code:
 set prompt = "\n%d, %D. %w %Y, %P\n<`uname -sr`> [%m:%~]\n%B%U%n@%l%u %#%b "
 
Back
Top