Zsh: Increase your productivity when using the terminal

Recently, I’ve become a big fan of replacing bash with zsh as my standard shell on Debian. Combined with the Oh My Zsh (GitHub) framework, it makes the terminal more convenient to use, offering better history search, autocompletion and plugins for many popular tools, such as git, systemctl and kubectl. I can now just enter these commands and press TAB in order to view their options. Colours inform me on whether a command has completed successfully. Moreover, as of macOS Catalina, it is the default shell on Apple computers.

sudo apt install zsh git curl

curl -Lo install.sh https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
nano install.sh

to download and inspect, then install:

sh install.sh

When prompted, choose yes to change the default shell to zsh.

rm install.sh

Optional extras

Zsh “user@hostname” prompt

I was missing one thing after installing ohmyzsh, which was the user@hostname information in the command prompt. Not having it is very confusing when using different sessions at the same time. However, I also didn’t want to create a separate theme or alter the existing zsh prompt too much. At the moment, the defaults are good enough for me.

In order to get the user@hostname prompt back after installing ohmyzsh, like in bash, but keeping the information zsh provides when e.g. browsing a git repository, execute:

cat <<EOF >> ~/.zshrc
export PROMPT="%{\$fg[green]%}%n@%m%{\$reset_color%} \${PROMPT}"

EOF

After logging out and in again, user@hostname should be added in front of every prompt.

Enable auto updating

nano ~/.zshrc

Uncomment

zstyle ':omz:update' mode auto      # update automatically without asking

Add colour to the ip and ls command

cat <<EOF >> ~/.zshrc
# Print ip information in colour
alias ip='ip --color=auto'
alias ls='ls --color=auto'

EOF

Snap packages support

Unfortunately, after installing zsh, snap packages were no longer added to PATH. Below are instructions on how to fix that.

sudo nano /etc/zsh/zprofile

add

# Expand $PATH to include the directory where snappy applications go.
snap_bin_path="/snap/bin"
if [ -n "${PATH##*${snap_bin_path}}" -a -n "${PATH##*${snap_bin_path}:*}" ]; then
    export PATH=$PATH:${snap_bin_path}
fi

# Ensure base distro defaults xdg path are set if nothing filed up some
# defaults yet.
if [ -z "$XDG_DATA_DIRS" ]; then
    export XDG_DATA_DIRS="/usr/local/share:/usr/share"
fi

# Desktop files (used by desktop environments within both X11 and Wayland) are
# looked for in XDG_DATA_DIRS; make sure it includes the relevant directory for
# snappy applications' desktop files.
snap_xdg_path="/var/lib/snapd/desktop"
if [ -n "${XDG_DATA_DIRS##*${snap_xdg_path}}" -a -n "${XDG_DATA_DIRS##*${snap_xdg_path}:*}" ]; then
    export XDG_DATA_DIRS="${XDG_DATA_DIRS}:${snap_xdg_path}"
fi

Back to bash

If you would like to switch back to using bash as your default shell, execute chsh -s /bin/bash

Sources