Howto install and make a nice ZSH command prompt on FreeBSD Mac OSX and Linux

So you want to start enjoying arguable worlds best shell zsh (Z shell).
Well, I will show you how to install zsh on FreeBSD, Mac and Linux and how to customize the zsh prompt to get a nice coloured and informative command prompt which will update your current working path.
Also I will show you how to use editor vi with coloured syntax highlighting and how to use ls to display your files in colours. You can get access to all the advanced features from the Z shell. You can win a lot of time with features like browsing a remote server with scp and the wonderfull TAB-completion that can recognize and correct typos for you.
It’s really perfect for lazy people ;)

BUT (there is always a ‘but’) you will have to take the time to configure it first time to get all those advanced options.
Well, just take the time to do it, it’s worth it ! Really !
Anyway, once installed I never looked back.


OK, now let’s skip the bullshit and just get started !
If you haven’t installed zsh yet, install it.

Snow Leopard
On Snow Leopard zsh is allready available, just click on Accounts in System Prereferences.
Unlock the advanced pane by clicking on the padlock. Then right click on your name should give you advanced
options. In the resulting screen choose /bin/zsh as your Login shell.

FreeBSD build from ports.
# cd /usr/ports/shells/zsh && make install clean

Debian/Ubuntu based
# apt-get install zsh

CentOS/Redhat based
# yum install zsh

OpenSuSE/SLES/SLED
# zypper in zsh

Finally, change your account to use zsh as your default shell
FreeBSD users:
# chpass -s /usr/local/bin/zsh

Linux based OS (probably all)
# chsh -s /bin/zsh

Remember, do NOT set ‘root’ to use _any_ shell that is not part of the base system. If your ports/packages breaks or
if its not available for whatever reason you can not login anymore. (Now you have been warned :P)
Instead, if you need to administer a lot as root and still want to use zsh you can gain root using ‘sudo -s’ option.
That way you get root keeping your own environment and your own $PATH.
There is nothing wrong with sudo so if it’s not there on your system, install it. It’s available in your package manager/ports system. Yes, also in yours ;)

Also, if your OS complains about a non-standard shell then either zsh is not (properly) installed or the path to your
zsh executable is wrong. Look for it using the following command
# which zsh

The users configuration of the Z shell is stored in a file .zshrc in the users home directory. You can make this file
yourself or you can follow a wizard-like setup which will create your own personal .zshrc file.
Starting zsh for the first time will get you a wizard-like setup to get you started, but if you didn’t you can start it by
typing the following commands.
# compinstall -Uz compinit
# compinit
# compinstall

If you are in a rush and don’t have the time to do the wizard thingy just skip it for now and do it when you have the
time to do it good. Anyway, to make your zsh prompt more informative and with some fancy colors just append the following lines to your .zshrc file in your home directory.

zsh prompt colors:

autoload colors; colors
export PS1="%B[%{$fg[red]%}%n%{$reset_color%}%b@%B%{$fg[cyan]%}%m%b%{$reset_color%}:%~%B]%b "

Display host and working path in top bar of your Terminal:

case $TERM in
sun-cmd)
precmd () { print -Pn "\e]l%~\e\\" }
preexec () { print -Pn "\e]l%~\e\\" }
;;
*xterm*|rxvt|(dt|k|E)term)
precmd () { print -Pn "\e]2;%n@%m:%~\a" }
preexec () { print -Pn "\e]2;%n@%m:%~\a" }
;;
esac

Your prompt will look like this examples.
Left is my FreeBSD laptop, right is my Macbook (changed color cyan to yellow).

zshfbsd zshmac

More colors

How do I get those syntax highlighting colors to show op when editing files using vi ?
On FreeBSD install vim-lite using packages
# pkg_add -r vim-lite

Or install vim-lite from FreeBSD ports:
# cd /usr/ports/editors/vim-lite && make install clean

For Linux based OS search for vim-nox and install it using one of the examples shown above.
Then edit your .zshrc file and make an alias to use vim everytime you start vi from your zsh shell
# echo "alias vi='vim'" >> ~/.zshrc

Append ‘syntax on’ to your .vimrc in your home directory
# echo "syntax on" >> ~/.vimrc

FreeBSD also recommends to append ‘set nomodeline’ in .vimrc
# echo "set nomodeline" >> ~/.vimrc

Now next time you start your shell and open vi you will actually start to love using vi :)

Even more colors

How do I get those colors using the ls command
# ls -G
Or, if you want your dirs and files be listed in colors all the time, you can create an alias for it.
# echo "alias ls='ls -G'" >> ~/.zshrc

After next login you will have all your colours in your shell.

Question:
Why all this? Why bother, I _hate_ colours …
Answer:
Well, because ;)
Everybody likes colours ;)

2 Responses

Leave a Reply