# Colorize your BSD shell



## sidetone (Jun 9, 2022)

Colorize your Csh or Bourne shell. Bourne shells include sh(1), ksh variants (Korn shells), and zsh(1). csh(1) is a different kind of shell which uses a different syntax for commands.


*Environment*
Environment values can be added to your shell configuration file(s). These variables for Bourne shells can be set in $HOME/.profile. sh, zsh and variant ksh Posix Bourne shells can read from these files. Bash can also use these files, but it has variations in its more specific customization files.

Csh uses many of the same basic variables as Bourne shells, but the syntax incorporating them is different. This shell uses .cshrc for customization.

`set` can be used to see basic variables from bourne shells, and csh. For csh, `setenv` can be used to see its additional variables. `export` is used from sh, ksh variants, and zsh. These commands are typed without arguments to view their variables, which can be adjusted. These same commands and additional arguments can be used to set these variables. Bourne shells vary in how their variables are set.

`echo` with $ and the specific variable written in capital letters can also be used to see that variable from your shell.

To enable some configurations for sh, the following can be run from the commandline.

```
ENV=$HOME/.shrc; export ENV
```
This can also be added to the configuration file. Then, restart the shell. Other Bourne shells reference a different configuration file.


*Changing colors of ls output*
The environment value `LSCOLORS` refers to the color output for ls(1) in the terminal. This value be added to your shell configuration file(s).

ls(1):

```
LSCOLORS           The value of this variable describes what color to
                         use for which attribute when colors are enabled with
                         CLICOLOR or COLORTERM.  This string is a
                         concatenation of pairs of the format fb, where f is
                         the foreground color and b is the background color.
```


```
The color designators are as follows:
                               a     black
                               b     red
                               c     green
                               d     brown
                               e     blue
                               f     magenta
                               g     cyan
                               h     light grey
                               A     bold black, usually shows up as dark grey
                               B     bold red
                               C     bold green
...
```


```
The order of the attributes are as follows:

                               1.   directory
                               2.   symbolic link
                               3.   socket
                               4.   pipe
                               5.   executable
                               6.   block special
                               7.   character special
                               8.   executable with setuid bit set
                               9.   executable with setgid bit set
                               10.  directory writable to others, with sticky
                                    bit
                               11.  directory writable to others, without
                                    sticky bit
```


```
The default is "exfxcxdxbxegedabagacad", i.e., blue
                         foreground and default background for regular
                         directories, black foreground and red background for
                         setuid executables, etc.
```

`CLICOLOR` sets this from the shell's configuration file, so `ls -G` or its `alias` aren't needed. `COLORTERM` and `TERM` are mentioned as similar in function to `CLICOLOR`.

Different shells set these variables in different ways. Csh uses `setenv`, and Zsh uses `export` for setting these. Here's examples for Csh and Zsh:
.cshrc

```
setenv LSCOLORS "gx"
setenv CLICOLOR
```
.zshrc

```
export LSCOLORS=ex
export CLICOLOR
```

`LSCOLORS` is for basic ANSI colors. LS_COLORS is an alternative variable with a different syntax that comes with more colors, but it's available with certain shells: this variable can be found in the manpages of shells that use it. An example of this variable: 
	
	



```
LS_COLORS='di=32:ln=35:so=33:pi=31:ex=34:bd=34;40:cd=35;40:su=30;42:sg=37;42:tw=30;46:ow=31;46'
```


*Setting prompt colors*
To set up your prompt in csh, use: `set prompt=` with the variables within double quotes. Variables within .cshrc are prefixed with `%`.

The colors go within the bracket parenthesis `{}`, prefixed with `%`. `\33` is the character escape, so the next variables can be used. Within that is an ANSI variable for colors. `[x;xxm%`


```
set prompt = "%{\033[1;36m%}%~ %{\033[0m%}%# "
```
In this example, `[36;m%` and `[0m%` are ANSI code for colors. `%~` is used to show the current directory.

In my setup, the directories of the `ls` output and in the prompt have nearly the same color. I adjusted the color, so it can contrast to a dark background.






```
set prompt = "%{\033[1;33m%}%N:%{\033[1;36m%}%~ %{\033[0m%}%# "
```
 adds the user name with colors to indicate that. This is useful for when indicating root on the console. Jail name can also be displayed on the prompt in a similar way.





zsh uses `PS1=` for setting the prompt. `PS2=` is for the prompt, when the shell is waiting on an interactive response. Some have used, `PROMPT=` for setting the prompt in zsh. For color variables, zsh uses the word names, that are within `%F{` `}` and end enclosed by `%f`.


*More*
Examples of csh and zshrc configurations can be viewed and shared on: Thread share-your-tcshrc-file.21040 and Thread share-your-zshrc-file.62653. For a related thread on Csh, see: Thread howto-color-files-by-extension-in-tcsh.29669. For a guide on Zsh: https://zsh.sourceforge.io/Guide/zshguide02.html.

.shrc and .kshrc were a bit more challenging to set up. There are scarce examples or instructions for these on the forums. There are also not as many examples of these on the Internet as other shells. More on colorizing Bourne shells can be added below.

More on colorizing Bourne shells, Csh or information that's closely related to the subject is welcome to be added to this thread.

This is unrelated to colorizing, but `autoload -U history-search-end` can be added to .zshrc for auto completion.


For Bash, see: Thread colorize-your-bash-like-linux.45006.


----------



## Geezer (Jun 10, 2022)

Other things that can be colourised:

`grep --color=auto`

sysutils/screen or sysutils/tmux (preferably) lots of colourful customisation available.


----------



## JozanOfAstora (Jun 10, 2022)

You could use something like like this to give some colors to `man` (this is for `zsh`):


```
LESS_TERMCAP_mb=$(printf '\e[1;31m');        export LESS_TERMCAP_mb
LESS_TERMCAP_md=$(printf '\e[1;31m');        export LESS_TERMCAP_md
LESS_TERMCAP_me=$(printf '\e[0m');           export LESS_TERMCAP_me
LESS_TERMCAP_so=$(printf '\e[1;33;40m');     export LESS_TERMCAP_so
LESS_TERMCAP_se=$(printf '\e[0m');           export LESS_TERMCAP_se
LESS_TERMCAP_us=$(printf '\e[0;4;35m');      export LESS_TERMCAP_us
LESS_TERMCAP_ue=$(printf '\e[0m');           export LESS_TERMCAP_ue
```

Which in this case would render a man like this:


----------



## mer (Jun 10, 2022)

Thanks for this.  Now I know what env things I need to unset and aliases to clear.
My opinion:
I hate the colorization stuff being turned on by default.  Hurts my eyes and actually hides the data from me.
And if you are scripting the output, you go from a text file to a binary file with all the escape sequences which is a pain when you bring it up in an editor.

But I recognize that some people love it.


----------



## Eric A. Borisch (Jun 10, 2022)

Seeing this discussion yesterday prompted me to finally get this little toy of mine posted: https://github.com/eborisch/colcolor


----------



## leebrown66 (Jun 10, 2022)

I've recently been using misc/grc for colourizing any output.  You can customize it per command, so I've found it nice for `ipfw` to separate out addresses, tables, xmit, recv keywords, etc.


----------



## drhowarddrfine (Jun 10, 2022)

mer said:


> I hate the colorization stuff being turned on by default. Hurts my eyes and actually hides the data from me.


Agree. Most of the things I see on the web make a clown college out of everything while claiming it helps find things in code. But, in this case, the OP has sensible, mild settings that don't hit you in the face like elephant dung at Ringling Brothers.


----------



## zirias@ (Jun 10, 2022)

mer said:


> I hate the colorization stuff being turned on by default.  Hurts my eyes and actually hides the data from me.


It's meant to do the opposite, which works fine if done in a sane way...


mer said:


> And if you are scripting the output, you go from a text file to a binary file with all the escape sequences which is a pain when you bring it up in an editor.


A program that outputs escape sequences by default when stdout is NOT a tty is broken.

Still it isn't binary either. Esc is a control character defined in ASCII and it's the only one used.


----------



## mer (Jun 10, 2022)

I agree that the OP settings are reasonable, but some colors like the yellows and some greens hit the eye right in the peak of visible spectrum and seem brighter than they actually are.  So it's often the juxtaposition of the colors that cause issues (color photography type of stuff).

I'm starting to believe that a bunch of folks here grew up on black&white VT100, graduated to green|orange on black and their (our) eyes just can't deal with "modern sensibilities".

Over time, contrast and brightness become primary drivers of useability.  Higher contrast, with lower brightness is often easier to look at all day.

So please OP just take this as a bit of grumbling from someone barely younger than dirt.


----------



## sidetone (Jun 10, 2022)

Intended for sharing Bourne and Korn shell configurations: Thread share-your-shrc-kshrc-or-mkshrc-file-segments-bourne-or-korn-shell.85467. I had mksh in mind when referring to ksh on this thread. mksh is a little more user friendly and a little more advanced than ksh. Mksh is easier to set up than Ksh. Also a lot or all from .kshrc applies to .mkshrc.

Thread learning-materials-for-shell-shell-scripting.66660 is a thread asking about learning materials for sh and ksh.


Can another thread be used for discussing colorization of terminals/shells.


----------



## SirDice (Jun 10, 2022)

Yes. Please move discussions to the other parts of the forums. I'm fine with actual information being added to a how-to but let's try to keep how-tos nice and clean


----------



## DavidMarec (Jun 10, 2022)

Note that running FreeBSD 13.1, one can configure  ls to underline attributes instead of using bold color.
Set a capital character (e.g. X) as background color to underline the corresponding attribute:
`export LSCOLORS=EXfxcxdxbxegedabagacad`


----------



## grahamperrin@ (Jun 11, 2022)

From my ~/.cshrc at <https://forums.freebsd.org/posts/571056>: 


```
# setenv        MANPAGER        "less -sR"
# <https://unix.stackexchange.com/a/319231/13260>
setenv  MANCOLOR        1
setenv  MANWIDTH        tty
```

Is `MANCOLOR` `1` alone not enough to trigger colours, does it also require something like what JozanOfAstora described?



JozanOfAstora said:


> You could use something like like this to give some colors to `man` (this is for `zsh`):
> 
> …


----------



## Alain De Vos (Jun 11, 2022)

I have:

```
export COLORFGBG='15;0'
export COLORTERM=truecolor
export TERM=xterm-256color
export LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:'
```


----------

