Today, I want to start learning Zsh. How to install it, how to configure it and the tricky things. So, let’s go.

How to install Zsh?

To install it you will need to run

sudo apt install git curl zsh

Then you will want to put it as your main shell. To do that just run:

chsh -s $(which zsh)

and then logout or open a new shell session.

The first time you will have a “welcome tutorial”. It is very intuitive. Just follow the steps and you will be done.

How to install Oh My Zsh?

I come from Windows Powershell with the Oh My Posh terminal so I need the same for Zsh. Then I found “Oh My Zsh” so I’m good to go.

Zsh installation

As easy as run this line:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Zsh theme

By default Zsh has a basic theme. You can have a look at here for a full list of themes. Just as an eample, I will take honukai.

To install it go to the folder $ZSH_CUSTOM/themes/ and there run:

wget https://raw.githubusercontent.com/oskarkrawczyk/honukai-iterm/master/honukai.zsh-theme

Then you will need to select this theme in the ~/.zshrc file replacing the line ZSH_THEME="robbyrussell" by ZSH_THEME="honukai"

Zsh plugins

Last but not least, you will want some extra plugins. You will want to add the following in the .zshrc file:

plugins=(git common-aliases colored-man-pages zsh-autosuggestions zsh-syntax-highlighting)

And install the missing ones:

git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions

Some of the alias are gotten from here

Zsh tricks

Zsh Aliases

To use the aliases you will need to append them also in the .zshrc file with the same sintax than bash:

alias repo="cd ~/repo"

Add ssh-agent

To configure ssh-agent to run it if it´s not already running, just append this piece of code at the end of the .zshrc file:

# Ssh agent
SHORT_HOST="${HOSTNAME/.*/}"
ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"

# Oh-my-zsh compatible bash ssh-agent start script
function _start_agent() {
    if [[ -f "$ssh_env_cache" ]]; then
        . "$ssh_env_cache" > /dev/null
    fi

    if [[ -S "$SSH_AUTH_SOCK" ]]; then
      return 0
    fi

    echo "Starting ssh-agent ..."
    ssh-agent -s | sed '/^echo/d' > "$ssh_env_cache"
    chmod 600 "$ssh_env_cache"
    . "$ssh_env_cache" > /dev/null
    ssh-add -t 1d
}
_start_agent

# Ssh agent
SHORT_HOST="${HOSTNAME/.*/}"
ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"

# Oh-my-zsh compatible bash ssh-agent start script
function _start_agent() {
    if [[ -f "$ssh_env_cache" ]]; then
        . "$ssh_env_cache" > /dev/null
    fi

    if [[ -S "$SSH_AUTH_SOCK" ]]; then
      return 0
    fi

    echo "Starting ssh-agent ..."
    ssh-agent -s | sed '/^echo/d' > "$ssh_env_cache"
    chmod 600 "$ssh_env_cache"
    . "$ssh_env_cache" > /dev/null
    ssh-add -t 1d
}
_start_agent

unset ssh_env_cache
unset -f _start_agent

It comes from here with a little of tunning in my side.