SETTING UP ZSH Shell

Z-shell (or Zsh) is an interactive shell for operating systems based on the Linux kernel. Even if you are a novice user you have probably already opened the terminal several times and executed some commands there. The place where you did this is called the shell. The shell used by default in most Linux distributions is called Bash.

The Zsh shell is essentially the same as Bash, but much more interesting. It supports interactive autocompletion via Tab, automatic file search, integrated regular expressions, and beautiful themes. In this article we will see how to install and configure Zsh.

INSTALLING ZSH

To install Zsh on Ubuntu, run this command:

sudo apt install zsh

To install Zsh on Fedora or CentOS use the dnf package manager:

sudo dnf install zsh

After that, you can start a command shell.

SETTING UP ZSH

To start a command shell, run the following command:

zsh

The first time you start Zsh, it will prompt you to configure your shell. Here you need to press key 1 to get to the setup menu:

1. HISTORY SETTING

In this menu let’s start with the first item. There you can find the command history settings. Press 1, then to change the number of commands that will be stored in history press 3 (Number of lines of history to save to $HISTFILE) and enter the desired number:

For example, I increased the number of stored strings to 5000. To return to the main menu press 0 (Remember changes and return to main menu)

2. AUTO SUPPLEMENT

Next, you need to configure the autocompletion. Here you can select item 1 to leave all default settings – Turn on completion with default options:

3. KEYBOARD LAYOUT

In the third point of the settings you can change how keystrokes on the keyboard will be processed while typing and editing commands. By default the Emacs style is used, however, you can enable Vim style if you use this editor. To do this, press 3 first, then 1 to edit the layout and select v for Vim:

4. OTHER OPTIONS

The fourth item contains other shell options. You can enable or disable them:

  • Change directory given just path – change directory if you enter the path to a non-executable file in the terminal;
  • Use additional pattern matching features – allow to use additional characters when creating matching patterns, this includes #, ~ and ^ characters, except standard * and ?
  • Unmatched patterns cause an error – output an error if the pattern does not match;
  • Beep on – give a signal when an error occurs;
  • Immediately report changes in background job status – to report changes in background job status.


To activate the option press its number and then press s (set), to deactivate press u (unset).

5. SETTINGS RETENTION

When you are done, go back to the main menu. There, to save all changes press 0. The utility will warn you that configuration lines have been added to the configuration file and it is not worth editing them, you can only edit what is before or after them. If you want to fix something in them, run the command zsh-newuser-install again.

Once you have finished configuring it, you will be in the shell.

6. SHELL PROMPT

Probably the calling card of any shell is an input prompt. It is him that we constantly see while working with the shell. By default, the prompt looks too simple. The main invitation template is in the $ PS1 or $ PROMPT variable . This prompt appears when you type any command and is the most fun to customize. The following variables are available:

  • % m – hostname;
  • % n – username;
  • %? – return code of the last command;
  • % d – current directory;

In addition, various colors are supported here. The color can be set for the background or for the text. Hence the syntax for setting the color is:

% { $ destination [color] %}

The destination can be fg , fg_bold, or bg . And as a color:

  • white – white;
  • black – black;
  • green – green;
  • cyan – purple
  • red – red;
  • yellow – yellow;
  • magenta is orange.

Use % {$ reset_color%} to reset the color . Then you can collect your input prompt, for example:

PS1="%{$fg[green]%}%[email protected]%n%{$fg[red]%}$ %{$fg[blue]%}%d %{$fg[yellow]%}>%{$reset_color%}"

In order for this to work, add not only this line to the ~ / .zshrc file, but also the following:

autoload -U colors && colors

As a result, you will receive an input prompt like this:

7. INSTALLATION OH MY ZSH

If all this is not enough for you, you can install the script oh my zsh, which implements many additional features in zsh. Here you can select different invitation themes, add plugins like git, add various autocompletion features, and much more. To install this script, run it:

curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh

The script will install all plug-ins and themes oh my zsh into your home folder, and completely replace the .zshrc file with your own:

8. THEME SETTING

One of the most interesting features oh my zsh – themes. Topics define the colors of the text, the appearance of the invitation and much more. All available themes can be found in the ~/.oh-my-zsh/themes folder:

You can see how this or that theme looks on this page. To select a theme, open the ~/.zshrc file and type the theme name in ZSH_THEME. This is how you set up oh my zsh. I like the agnoster theme, for example, so:

vi ~/.zshrc
ZSH_THEME=agnoster

Then you can restart zsh:

But for this particular theme to work, you need to install the font package:

sudo apt install fonts-powerline

9. DEFAULT SHELL

When you’re done with the configuration, all that remains is to set zsh as the default shell for your user. To do this, use the chsh command:

chsh

The utility will ask for a password and then the path to the executable file of the shell you want to use:

After that, the default zsh shell will be used for the current user.

Leave a Reply

Your email address will not be published. Required fields are marked *