devops · April 11, 2021 0

Boost your productivity with ZSH and Git Pretty Prompt

For your efficiency and a productive work it’s important to have the right toolset. If you mostly work with terminals and also use Git, you should have all informations at hand without using commands for it. Therefore in this post we setup ZSH as your default Shell with some of my favorite plugins, like syntax highlighting, auto suggestions, Git pretty prompt and more.

Install ZSH and change your default Shell

The used Linux Distribution in this case is Debian buster especially Rasperry Pi Os on a Raspberry Pi 4. Check out my post for a Raspberry Pi Setup is you are interested. But every other distribution should also work, for example in Mac OS ZSH is already the default Shell, so no need for extra installation, just configure the plugins. Check also my next Blog Posts for automation with Ansible, I’ll post a whole Playbook for this setup for MacOS, Debian, CentOs or RedHat.

Open a terminal window and install ZSH with apt. If you want to install it on a remote system like a Raspberry Pi connect to it with SSH.

sudo apt update
sudo apt install zsh

Change your default shell to the new installed ZSH Shell.

chsh -s /bin/zsh

Next close your terminal or the connection to your remote System, you have to open a new terminal to get into your configured new ZSH Shell. If you start this the first time, there is no configuration therefore you get prompted for initial setup. If you just want to install my suggested plugins in the next sections, just select (q) to quit.

Quit the setup screen without any changes

Now you are finished with the initial setup, but without any plugins active, so the prompt is not really pretty, and some really handy features are missing. Therefore jump into the next sections to setup the right plugins for you, from my suggestions.

ZSH Plugin Installation

In the next sections we will setup all you need to boost your productivity when working with ZSH. There are also other possible Solutions out there, like oh my zsh to manage configurations, maybe have a look on it. I like to choose the plugins and configurations direct from the contributors on Github and with my Ansible automation, the manual steps which are described here are also not necessary, so it’s up to you.

Install GIT

First of all you have to install Git if you haven’t installed it till now. You need Git to clone the plugin configurations from the Github Repositories and it’s also necessary if you want to use the Git Pretty Prompt.

sudo apt update
sudo apt install git -y

Syntax Highlighting

The first plugin is for syntax highlighting. You could find the source and more documentation on Github zsh-users/zsh-syntax-highlighting from zsh-users. First of all Clone the repository locally in your ~/.zsh directory

git clone https://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/zsh-syntax-highlighting

Next you have to add the code into the ~/.zshrc Configuration File. You could also skip this step and first install all plugins. I have a link to the complete configuration file with all settings at the end of this blog.

echo "source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ~/.zshrc

If you added the config and want to see the changes you have to reopen your terminal or use the following command.

source ~/.zshrc 

After the activation of this first plugins, you see the highlighting for example ls in the screenshot is correct therefore green and ech is read because there is a missing o for echo

Syntax Highlighting with green and red indications

Auto Suggestion

The next plugin is for auto suggestion. You could find the source and more documentation on Github zsh-users/zsh-autosuggestions from zsh-users. Please find a detailed description of all steps in the previous section, and decide if you just want to install it and activate all together at the end or activate the plugin immediately.

git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
echo "source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh" >> ~/.zshrc
source ~/.zshrc 

If you have activated the plugin, now you can see the gray text is from the autosuggestion. Just press tab and you are ready to call the whole command.

Autosuggestion with your last command in gray

Subercrabtree – K for File and Git infos

This plugin is simply called with k and gives you a directory listing with color coded infos about file sizes and Git. You could find the source and more documentation on Github supercrabtree/k.git from supercrabtree.

git clone https://github.com/supercrabtree/k.git ~/.zsh/k
echo "source ~/.zsh/k/k.zsh" >> ~/.zshrc
source ~/.zshrc 

Some examples after you have activated the plugin and call k in any directory

Subercrabtree K – Color coded File Sizes and Git informations

Pretty Git Prompt

A fast, customizable, pure-shell, asynchronous Git prompt for Zsh. You could find the source and more documentation on Github woefe/git-prompt.zsh from woefe.

git clone --depth=1 https://github.com/woefe/git-prompt.zsh ~/.zsh/git-prompt.zsh
echo "source ~/.zsh/git-prompt.zsh/git-prompt.zsh" >> ~/.zshrc
source ~/.zshrc 

There are different examples for customizing the prompt. I have my own customization you are free to use. I’ll add the code in Github, but till then just open an editor to copy the content direct there, and activate it.

vi ~/.zsh/git-prompt.zsh/wep4you.zsh
echo "source ~/.zsh/git-prompt.zsh/wep4you.zsh" >> ~/.zshrc
source ~/.zshrc 

The code you have to copy into the file ~/.zsh/git-prompt.zsh/wep4you.zsh

ZSH_GIT_PROMPT_FORCE_BLANK=1
ZSH_GIT_PROMPT_SHOW_UPSTREAM="full"

ZSH_THEME_GIT_PROMPT_PREFIX="%B · %b"
ZSH_THEME_GIT_PROMPT_SUFFIX="›"
ZSH_THEME_GIT_PROMPT_SEPARATOR=" ‹"
ZSH_THEME_GIT_PROMPT_BRANCH="⎇ %{$fg_bold[cyan]%}"
ZSH_THEME_GIT_PROMPT_UPSTREAM_SYMBOL="%{$fg_bold[yellow]%}⟳ "
ZSH_THEME_GIT_PROMPT_UPSTREAM_PREFIX="%{$fg[yellow]%} ⤳ "
ZSH_THEME_GIT_PROMPT_UPSTREAM_SUFFIX=""
ZSH_THEME_GIT_PROMPT_DETACHED="%{$fg_no_bold[cyan]%}:"
ZSH_THEME_GIT_PROMPT_BEHIND="%{$fg_no_bold[cyan]%}↓"
ZSH_THEME_GIT_PROMPT_AHEAD="%{$fg_no_bold[cyan]%}↑"
ZSH_THEME_GIT_PROMPT_UNMERGED="%{$fg[red]%}✖"
ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[green]%}●"
ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg[red]%}✚"
ZSH_THEME_GIT_PROMPT_UNTRACKED="…"
ZSH_THEME_GIT_PROMPT_STASHED="%{$fg[blue]%}⚑"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}✔"

PROMPT=$'┏╸%(?..%F{red}%?%f · )%n@%m %B%~%b$(gitprompt)\n┗╸%F{blue}❯%f%F{cyan}❯%f%F{green}❯%f '
RPROMPT=''

With this customization you get a multiline prompt with Git informations, like your branch, the upstream branch, changes and all you need to immediately see if you are up to date or if there are changes, a real productivity boost if you often work with git in command line.

Git Prompt with my own customizations

Kubectl Autocomplete

If you also work with Kubernetes and have installed kubectl you could activate Autocompletion. To activate this add the following line into your .zshrc File

echo "source <(kubectl completion zsh)" >> ~/.zshrc
source ~/.zshrc 

ZSH Configuration File

As I already mentioned before. You could use the whole configuration file from here, if you haven’t activated every plugin step by step, and installed all at once. Please also have a look at the order, there is a plugin which should be last in your file:

source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.zsh/git-prompt.zsh/git-prompt.zsh
source ~/.zsh/k/k.plugin.zsh

# Last 2 Lines
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source ~/.zsh/git-prompt.zsh/wep4you.zsh

Summary

You have installed and activated ZSH as your default shell. For productivity enhancement you selected the right plugins for your needs. You have used the suggested configuration for your prompt, or changed it to your needs. You know exactly what you have activated and have the links to more documentation to also get more out from the given plugins.

If you have any questions or suggestions, or if you would like further information, please let me now in the comments. 

WordPress Appliance - Powered by TurnKey Linux