ZShell

date:

tags: zsh linux shell

categories: Linux

What is a shell?

The shell is a program that translates human readable words (commands) and converts them into binary data that the Operating System kernel can interpret. The shell can interpret commands entered from the keyboard or a file commonly refered to as a script.

You can access the shell through a terminal program on the local computer or remotely using SSH.

The most popular shells that I am aware of are:

ZSH

Zsh is a shell that builds on shells such as bash, ksh, and tcsh. Zsh offers some useful features such as better tab completion.

Installing Zsh

Windows

On Windows, you would need Cygwin or the newer Windows Subsystem for Linux. Check out a previous post for setting up Cygwin.

macOS

1
2
3
4
# MacPorts
sudo port install zsh
# homebrew
brew install zsh

Linux

On Red Hat family:

1
sudo dnf install zsh

On Debian family:

1
sudo apt-get install zsh

On SUSE family:

1
sudo zypper install zsh

Change default shell

Once you have installed zsh, you need to configure it as your default shell.

1
chsh -s /bin/zsh

Examples

CD shorthand

1
2
3
% repos
% pwd
/home/user/repos

In the example above, simply typing the name of a directory into the prompt will take you to that directory in the shell. On other shells such as bash, this would return an error.

Tab completion

1
2
ls
dir1 dir2 foo bar

Zsh improves tab completion. Try typing ls and tab twice. Zsh will display the files and directories that you can interact with and navigate through with arrow keys. It also works with cd and mk.

Directory shorthand

1
ls /f/b/ar

Try entering only a few characters to the paths to a directory you wish to navigate to. Zsh will expand these fuzzy directories and let you navigate with less typing.

Out of the box, Zsh has a lot to offer and there are a few additions that make it even better.

Oh my zsh

Oh my Zsh is an open-source framework of Zsh plugins and themes.

1
sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

or if you do not have the wget package:

1
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Once you install zsh, your zsh config file ~/.zshrc will be modified. This framework makes it easier to integrate the following plugins.

Powerlevel10k theme

Powerlevel10k is a theme for zsh that can be easily selected when you have Oh my zsh.

1
2
3
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
sed -i 's/_THEME=\"robbyrussell\"/_THEME=\"powerlevel10k\/powerlevel10k\"/g' ~/.zshrc
source ~/.zshrc

Now you should see the powerlevel10k config wizard:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
   This is Powerlevel10k configuration wizard. You are seeing it because you
 haven't defined any Powerlevel10k configuration options. It will ask you a few
                      questions and configure your prompt.

                Does this look like a diamond (rotated square)?
                  reference: https://graphemica.com/%E2%97%86

                                 --->    <---

(y)  Yes.

(n)  No.

(q)  Quit and do nothing.

Choice [ynq]:

Plugins

Zsh syntax highlighting

A plugin that easily can be installed when you have Oh my zsh that will highlight syntax that is typed into the terminal.

1
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

Zsh auto suggestions

A plugin that easily can be installed when you have Oh my zsh that will suggest a completed command based on reference from your history.

1
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

Zsh completions

A plugin that contains tab completion scripts that are not yet available in the main zsh release.

1
  git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/zsh-completions

Activate plugins

Update your ~/.zshrc file in the “plugins” section:

1
plugins=(git zsh-syntax-highlighting zsh-autosuggestions zsh-completions)

Activate the plugins:

1
2
source ~/.zshrc
autoload -U compinit && compinit

Now, when you begin typing, you should see suggestions based on your command history. Valid commands will be green and invalid commands will be red.

zsh_plugins
zsh_plugins

comments powered by Disqus