Putting colour in your shell

xy16644

Aspiring Daemon

Reaction score: 13
Messages: 907

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.
 

dennylin93

Aspiring Daemon

Reaction score: 111
Messages: 783

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).
 
OP
OP
X

xy16644

Aspiring Daemon

Reaction score: 13
Messages: 907

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?
 

dennylin93

Aspiring Daemon

Reaction score: 111
Messages: 783

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.
 

MG

Active Member

Reaction score: 17
Messages: 194

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"
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 8,444
Messages: 32,480

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.
 

aragon

Daemon

Reaction score: 280
Messages: 2,029

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.
 
OP
OP
X

xy16644

Aspiring Daemon

Reaction score: 13
Messages: 907

I tried:

Code:
alias ls        ls -FGl
which worked but when I logged out and back in again I was back to the defaults?
 

aragon

Daemon

Reaction score: 280
Messages: 2,029

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'
 
OP
OP
X

xy16644

Aspiring Daemon

Reaction score: 13
Messages: 907

I am busy installed the bash3 port as I keep hearing about it ;-)
 
OP
OP
X

xy16644

Aspiring Daemon

Reaction score: 13
Messages: 907

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...
 

Carpetsmoker

Daemon

Reaction score: 184
Messages: 1,012

I don't know about bash, but for tcsh you should drop the equals sign (=), i.e.
alias ls "ls -FGl"
 

tangram

Aspiring Daemon

Reaction score: 74
Messages: 523

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?
 

dennylin93

Aspiring Daemon

Reaction score: 111
Messages: 783

It's best to leave root's shell as the default (csh), so just put
Code:
alias -FGl
in /root/.cshrc.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 8,444
Messages: 32,480

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.
 

olegrpg

New Member

Reaction score: 2
Messages: 5

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
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 8,444
Messages: 32,480

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
 

MG

Active Member

Reaction score: 17
Messages: 194

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.
 

tangram

Aspiring Daemon

Reaction score: 74
Messages: 523

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.
 

lme@

Administrator
Staff member
Administrator
Moderator
Developer

Reaction score: 300
Messages: 769

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 "
 
Top