Slider

How to Install & Configure Zsh

Bash: the default shell preinstalled in most Linux distributions is rock solid and industry standard for many years. There is not much to complain about Bash. It does the job and is the default shell on almost all Linux distributions. All the Linux tutorials and blogs assume you are using Bash. However, there are other shells to explore, including the famous ZSH and Fish shells. However, Bash is not customizable as ZSH or Fish. The more you can customize your shell, the more you can tailor it for your needs and be productive.

 

Why not Fish?

Some time ago, I tried ZSH, but it was too slow to open. Spending more time to open the terminal than running the actual command didn't justify using ZSH. That's when I decided to try Fish. Though Fish offers even more customization than ZSH, it breaks compatibility with Bash. From simple export commands to complex Bash scripts won't work in Fish out of the box. Every time I copy-paste some CLI commands from a website, I had to change the syntax to Fish almost every time I copy commands from the internet. It defeats the purpose of Fish. I installed Fish to boost my productivity, but it was counterproductive when writing shell scripts.

How to Install & Configure Zsh

Once again, I switched back to ZSH to give it a try. Surprisingly it is as snappy as Bash and Fish this time. The slowness of ZSH could come from an older version, an extension I used, or due to my years-old hardware. However, after installing ZSH with all the necessary plugins, it runs as smoothly as Bash without sacrificing the features I love in Fish.


Installing Zsh

Zsh is already available in the official repositories of all Linux distributions.

Debian or Ubuntu-based:

sudo apt install zsh


Fedora:
sudo dnf install zsh


Arch:
sudo pacman -S zsh


Once installed, run the following commands to set it as your default terminal.
chsh -s $(which zsh)

After completing the above command, log out and log in again (or restart) to apply the changes.

Open a new Terminal and you will be welcomed by the Zsh configuration. Zsh requires new users to configure it before using it. I recommend Option 2: Populate your ~/.zshrc with the configuration recommended by the system administrator and exit because the Zsh Framework we are going to install next will override them anyway.

Zsh Framework

Zsh Framework is a collection of themes and plugins with additional features to install new plugins and themes. In addition, Zsh Frameworks come with its plugins to make the experience unique and better than vanilla Zsh. There are a handful of Zsh Frameworks available out there. For the list of frameworks and plugins, please refer to awesome-zsh-plugins. Even though there are enough frameworks to confuse you, I highly recommend sticking with the famous frameworks.

Oh My Zsh: The only Zsh framework with 146k GitHub stars by the time of writing this article is Oh My Zsh. Its popularity motivates more and more plugin developers to support Oh My Zsh. Therefore almost all Zsh plugins you come across will support Oh My Zsh. Unless you have a particular reason, I recommend choosing Oh My Zsh because of the plugin availability.

Prezto: Prezto is a fork of the Oh My Zsh framework with 12.8k GitHub stars by the time of writing this article. Prezto has a cleaner code than Oh My Zsh. It is claimed to be faster than Oh My Zsh. As a developer, I see the advantage of having clean and refactored code but as you can see more people like Oh My Zsh over Prezto based on the GitHub stars. If you find any issues with Oh My Zsh especially related to performance, Prezto could be your second choice. If you are a clean code advocate, Prezto could be your natural choice and it won't let you down.

There are several other Zsh frameworks out there but I stop with these two frameworks to avoid getting into the rabbit hole of frameworks.
 

Configuration

As recommended earlier, I am also using Oh My Zsh. The following section introduces the Zsh plugins I use to boost my productivity.

1. Oh My Zsh

NOTE: Oh My Zsh installer depends on git. Even after installing, you will need it to install the Zsh Plugins.
 
Run the following command to install Oh My Zsh.
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
I have tried many themes but never found something perfect as the Agnoster Theme. It makes your terminal modern and provides some nifty features to make your life easier. For example, if your previous command failed, there will be a "cross" symbol at the beginning of the line. While in a GitHub repo directory, this theme changes the color based on the git status.

Agnoster theme heavily relies on powerline fonts. Install the powerline fonts using the following command:

Debian or Ubuntu-based Linux distribution:
sudo apt install fonts-powerline

Fedora or Redhat based Linux distribution:
sudo dnf install powerline-fonts

Arch:
sudo pacman -S powerline powerline-fonts


Once the powerline font is installed, open the ~/.zshrc file and change the ZSH_THEME value to agnoster as shown below.
ZSH_THEME="agnoster"

After saving the changes, close the editor and restart the terminal to see the Agnoster theme in action.

For the awesome list of Zsh plugins, check this GitHub repository. Though there are hundreds of plugins, I need only three additional plugins to complete my Oh My Zsh experience.

3. Zsh Auto Suggestions

One feature I miss from Fish is its auto-suggestions based on the shell history. As you are tying a previously used command, this plugin will suggest the command to complete. Pressing the right arrow key will autocomplete the command for you. I always find it handy because it feels more natural than searching throw history manually. This plugin offers more than the auto-suggestion. For the complete list of features, visit the GitHub page.

To install the plugin to Oh My Zsh, run the following command to download the plugin.
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions


Edit the ~/.zshrc file and add zsh-autosuggestions to the plugins list as shown below.
plugins=(
	git
	zsh-autosuggestions)

Restart the terminal to get a new Zsh session.

One glitch I noticed with my current terminal color and Agnoster theme is the default autosuggestion font was not visible enough to see. Add the following line at the end of your ~/.zshrc file to change the foreground color of the autosuggestion text.
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#474747"

You can also customize the background and font style if it doesn't align with your taste.
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ff00ff,bg=cyan,bold,underline"


4. Copypath
In Bash, I used to have my shell command for copying the current path of the terminal to the clipboard. It is super useful when you want to open that folder or a file inside that folder in another app or just to add that folder to an environment variable. The copypath plugin eliminates the need for my script and provides the same solution out of the box. Copypath plugin is bundled into the Oh My Zsh framework. The only thing you need is to add the plugin to the plugins list and restart the terminal.
plugins=(
	git
	zsh-autosuggestions
	copypath)

Now from anywhere in your terminal, type copypath, and your current path will be copied to the clipboard. You can paste it anywhere you want.

5. Sudo
You can always use !! to rerun your last command with sudo. The sudo plugin adds a quick shortcut to do the same. Similar to the copy path plugin, sudo plugin is also bundled in the Oh My Zsh framework. Once you have enabled it in the plugins list, press the Esc key twice to rerun the last command with sudo privilege.
plugins=(
	git
	zsh-autosuggestions
	sudo
	copypath)


Though I don't use any other plugins at the time of writing this article, do not limit yourself to what I am using. If you are using some other plugins and find it useful, please let me know down below in the comments. Any interesting plugin recommended by our readers will be included in this article.

Tip

The out of the box shared history across multiple terminal sessions was annoying me. I always use separate terminal windows/tabs to work on different contexts. When I want to run the last command, I need the recent command from the same terminal instead of the recent command from a different terminal. To disable shared history, append the following lines to the ~/.zshrc file and restart your terminals.
setopt no_share_history
unsetopt share_history


0

No comments

Post a Comment

disqus,linuxedo

DON'T MISS

News,Linuxedo
© all rights reserved