Install zsh

The Z Shell, or zsh, is a Unix shell that can be used as a command interpreter for shell scripts as well as an interactive login shell. It is an extention on the Bourne shell and includes many improvements. Recently, the adoption of the zsh shell got a big boost when Apple replaced their default shell, the well known Bash, to zsh.

Installing zsh #

The first step is to install the zsh package.

doas pkg_add zsh

$ doas pkg_add zsh
Password:
quirks-3.182 signed on 2019-12-04T10:32:31Z
zsh-5.7.1: ok
$

Changing shells #

There are 2 ways to change the shell of a user.

  1. Use the chsh command.

    chsh -s zsh

  2. Edit the /etc/passwd file

    First, we need to lookup the location of the zsh binary

    which zsh

    $ which zsh
    /usr/local/bin/zsh
    $
    

    Next, we can edit the passwd file and change the location of the shell

    doas vi /etc/passwd

    Find the line with the user you want to change the shell for and edit the last part of the

    user:*:1000:1000:Firstname Lastname:/home/user:/bin/ksh
    

    and change it to zsh.

    user:*:1000:1000:Firstname Lastname:/home/user:/usr/local/bin/zsh
    

Shell configuration #

The new shell will take effect as soon as the user logs in next. When the user logs in again, they will be greeted by the following message.

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

--- Type one of the keys in parentheses ---

If you would like to configure zsh yourself, pick option 1. Picking option 1 will lead to a new set of options that allow you to create a configuration via dialogues.

Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Recommended.)

(2)  Configure the new completion system.  (Recommended.)

(3)  Configure how keys behave when editing command lines.  (Recommended.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.

(0)  Exit, creating a blank ~/.zshrc file.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.  The function will be run again next time.
--- Type one of the keys in parentheses ---

Alternatively, you can install ohmyzsh. Ohmyzsh is a configuration framework for zsh and it allows you to customize your zsh setup with great ease. It supports themes for your prompt as well as various plugins to make your life easier when dealing with Python, Vim, Git, Spotify, etc.

To install ohmyzsh, we first need to install 2 packages, cURL and Git.

doas pkg_add wget git

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

$ wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
--2019-12-06 14:10:29--  https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.36.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.36.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8074 (7.9K) [text/plain]
Saving to: 'install.sh'

install.sh                           100%[====================================================================>]   7.88K  --.-KB/s    in 0s

2019-12-06 14:10:29 (23.8 MB/s) - 'install.sh' saved [8074/8074]

Next, execute the install script.

sh install.sh

The configuration for zsh is still in .zshrc. The default theme is the “robbyrussel” theme, named after the founder of the project. There are a lot of built-in themes available as well as external themes. The built-in themes are showcased on the projects Github page: https://github.com/ohmyzsh/ohmyzsh/wiki/Themes.

The following example will load the “Bira” theme.

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="bira"

Logging out and in again is often the easiest way to make all changes take effect. Alternatively, it’s possible to use the source command.

source ~/.zshrc