Jump to content

How To Coloring your Shell in FreeBSD ( ZSH / CSH )


Recommended Posts

  • Premium

1. Foreword

 

If you are like me and enjoy spending time fiddling with FreeBSD, or you want to make it a bit harder to avoid making mistakes because you edited the wrong file or something, it pays off to set up some coloring in your FreeBSD shell. Not for the aesthetics, but to identify easier: who we are (user), where we are (path), and what are we looking at (file coloring)

 

Spoiler

190018XuoUbab.png

 

First let's start with the subject of shells. Zsh is pretty popular among programmers, and it also provides an easy way to do exactly what we want through the Oh My Zsh addon. Oh My Zsh is a package designed to make zsh easier to use; you can head here for more details if you want to know more:

 

This is the hidden content, please

 

Note that you can use zsh as a root shell, but any Unix admin worth his salt will tell you that root should be using one of the default shells available with FreeBSD, since, if you run into any issue with the shell, you won't be able to su to root, which is pretty bad. So I will also explain how to do exactly the same coloring with FreeBSD's default shell, csh.

 

Now for the sake of example, let's say we have two users to deal with: root, and the user for our metin2 server which is aptly called metin2 [Note: a sensible approach in such setup is to forbid root from logging in through ssh and do that through the unprivileged metin2 user instead, using su whenever we need root access. I won't talk about this topic here though, just do not forget to add this metin2 user to the wheel group so he can su to root, if you follow this strategy.]

 

 

2. Installing Zsh

 

So let's login as root and install the zsh shell first. We can do it from packages or ports, just stick to one method or you will break dependencies and other nasty stuff.

pkg update
pkg install zsh

Or for ports:

portsnap auto

cd /usr/ports/shells/zsh

make install cleam

 

Now let's create our unprivileged user if we don't have one yet:

 

adduser metin2

 

We wil get a bunch of questions, just leave the defaults - ecept for the shell which will be zsh, and in "Invite to other groups?" you should type wheel here, so we can superuser (su) into root.

 

And of course you would want to type a password, though I prefer using ssh keys for login myself.

 

That was easy wasn't it?

 

 

3. Installing oh-my-zsh

 

Now let's switch to our new user with:

 

su metin2

 

Note: Something that annoys me is how the keypad doesn't work in zsh, specifically the / * - symbols which I use all the time. To amend this, you can add the keybindings in this post in .zshrc. Note they may differ depending on your keyboard - press ctrl+v and then the desired key to find out that key's code:

 

https://superuser.com/questions/742171/zsh-z-shell-numpad-numlock-doesnt-work

 

Make sure we are at the home directory (/home/metin2) and then proceed to install Oh My Zsh:

fetch https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
sh install.sh

 

Yes we can install oh-my-zsh even though we are not the root user; of course it will work only for this user.

 

Now try logging in a second Putty window to make sure everything works as intended - it's a good practice when we are changing stuff like shells, ssh or firewall parameters, to make sure we don't get locked out of our own system.

 

 

4. Setting up an oh-my-zsh theme

 

Once installed, by editing the .zshrc file found in our home directory you can change the color scheme or theme to any of the ones available - be aware though that the FreeBSD terminal only supports 16 colors by default, so all those fancy Linux ones won't work properly without heavy modification of the environment.

 

This is the hidden content, please

 

Luckily for you I will provide here my own color scheme perfectly adapted for this amazing operating system, in case you want to use it - just create a new file called .oh-my-zsh/themes/wom.zsh-theme  -mind the dot before oh-; paste this on it, save  and then set the theme as "wom" in the beginning of the .zshrc file -again mind the dot-.

 

# WoM2 oh my zsh theme by Shogun
if [ $UID -eq 0 ]; then NCOLOR="red"; else NCOLOR="white"; fi

PROMPT='%{$fg[$NCOLOR]%}%B%n@%m%b%{$reset_color%}:%{$fg[green]%}%B%0d%b%{$reset_color%} $(git_prompt_info)%(!.#.$) '
RPROMPT='[%*]'

# git theming
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}(%{$fg_no_bold[yellow]%}%B"
ZSH_THEME_GIT_PROMPT_SUFFIX="%b%{$fg_bold[blue]%})%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_CLEAN=""
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg_bold[red]%}▒~\~W"

# LS colors, made with https://geoff.greer.fm/lscolors/
export LSCOLORS="ExGxfxcxbxegehbhbgacad"
zstyle ':completion:*' list-colors "di=1;34:ln=35:so=32:pi=33:ex=1;31:bd=34;46:cd=34;43:su=30;41:sg=30;46:tw=30;42:ow=30;43"

Now once you are done you can open a new session (either login on a second Putty window if you're unsure about whether your changes could break something, or else just su to the same user again su metin2) and play around with the ls and cd commands to check directory listing and autocompletion are properly colorized.

 

If you want to make your own theme, you can replace the parameters of the PROMPT (self explanatory), LSCOLORS (directory listing) and list-colors (autocomplete) - for the later two you can get the appropiate strings here:

 

https://geoff.greer.fm/lscolors/

 

Note that the brownish color in this tool is actually yellow in FreeBSD, and that to access the second set of 8 colors you should mark "bold" which is not really bold but a different shade of the same color.

 

 

5. Shell coloring in csh

 

Okay time to move to the root user and its default system shell which is a bit less user friendly.

 

I prefer to get rid of the user specific settings file /root/.cshrc by deleting all the lines on it except for the comments at the start (hint - Ctrl+K in the ee editor deletes the line you're in) and then edit the system wide /etc/csh.cshrc file instead, so the changes apply to all users which use csh.

 

# $FreeBSD: releng/11.3/etc/csh.cshrc 50472 1999-08-27 23:37:10Z peter $
#
# System-wide .cshrc file for csh(1).
#
# WoM2 theme by Shogun on 31st April 2020

umask 22

set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin)

set color

setenv  EDITOR  ee
setenv  PAGER   less
setenv  BLOCKSIZE       K

setenv  LSCOLORS        ExGxfxcxBxegehbhbgacad
setenv  LS_COLORS       "di=1;34:ln=1;36:so=35:pi=32:ex=1;31:bd=34;46:cd=34;47:su=31;47:sg=31;46:tw=30;42:ow=30;43"

alias h         history 25
alias j         jobs -l
alias la        ls -aFG
alias lf        ls -FAG
alias ll        ls -lAFG
alias ls        ls -G

set     red="%{\033[1;31m%}"
set   green="%{\033[1;32m%}"
set  yellow="%{\033[1;33m%}"
set    blue="%{\033[1;34m%}"
set magenta="%{\033[1;35m%}"
set    cyan="%{\033[1;36m%}"
set   white="%{\033[1;37m%}"

set     end="%{\033[0m%}" # This is needed at the end of the prompt

if ($?prompt) then
        # An interactive shell -- set some stuff up
        set prompt = "${red}%N@%m:${green}%~${white} %# ${end}"
        set promptchars = "%#"

        set filec
        set history = 1000
        set savehist = (1000 merge)
        set autolist = ambiguous
        # Use history to aid expansion
        set autoexpand
        set autorehash
        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
        endif

endif

# Clean up after ourselves...
unset red green yellow blue magenta cyan yellow white end

 

These settings will result in exactly the same coloring as in my wom theme for oh-myzsh; besides I set ee as the default editor and less as the default pager. Feel free to change these to your liking.

 

For those curious about the topic I will explain the parts of this file related to coloring, since documentation on the net is pretty sparse about this:

 

set color enables coloring for autocomplete. Without this, the LS_COLORS line below won't have any effect.

 

setenv LSCOLORS will define the colors for directory listing (ls) - note that for this to work ls needs to be invoked with either the -G or the -l parameter, that's why we add the alias ls ls -G line. The parameters defining colors here are the same as in oh-my-zsh.

 

setenv LS_COLORS defines the color scheme for the autocomplete, just like list-colors did in oh-my-zsh. Once again, it works the same way parameter wise. Interestingly, this parameter is used for ls in Linux, hence the confusing naming of the variables.

 

The set red, green, etc lines are just a helper for coloring the prompt later on the script here: set prompt = "${red}%N@%m:${green}%~${white} %# {end}"

 

 

6. Notes

 

What if we want to use zsh and oh-my-zsh for root as well? In that case you should not install it from the unprivileged user but from root.

 

And what if we want to install oh-my-zsh for several unprivileged users, leaving root with csh (that's my own setup) - You will notice that if you try to install oh-my-zsh a second time, you will get an error. The way around this is, assuming we are logged in as this other user (let's call it "www") and in its home directory - let's call it /home/www:

unset ZSH

rm .zshrc

rm .zsrhz-local (if exists)

Now you can follow the same steps listed earlier to install it as this user.

 

 

7. TL; DR

 

If you want some prettier colors but can't bother reading this or don't feel confident changing stuff, just copy and paste this on your putty window while you are logged in:

 

echo 'set color' >> ~/.cshrc
echo 'alias ls ls -G' >> ~/.cshrc

 

Happy BSDing!

Edited by Shogun
  • Metin2 Dev 11
  • Confused 2
  • Scream 1
  • Good 1
  • Love 1
  • Love 11
Link to comment
Share on other sites

  • 1 year later...
  • 5 months later...
  • 1 year later...

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.