Install zsh

ZSH #

The Z Shell (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 extension of the Bourne shell and includes many improvements. Recently, the adoption of zsh increased when Apple replaced their default shell, Bash, with 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 two ways to change the shell of a user.

Using the chsh Command #

Use the chsh command.

chsh -s zsh

Editing the /etc/passwd File #

First, lookup the location of the zsh binary.

which zsh

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

Next, edit the passwd file to change the shell’s location.

doas vi /etc/passwd

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

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

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, the following message will appear:

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

Configuring zsh Manually #

To configure zsh manually, pick option 1. This will lead to a new set of options 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 ---

Installing ohmyzsh #

Alternatively, install ohmyzsh, a configuration framework for zsh that allows customization with themes and various plugins. To install ohmyzsh, first install 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 in .zshrc. The default theme is the “robbyrussell” theme, named after the project’s founder. There are many built-in themes available as well as external themes. The built-in themes are showcased on the project’s GitHub page.

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, use the source command.

source ~/.zshrc