DEV Community

Cover image for An Enhanced Shell With ZSH

An Enhanced Shell With ZSH

simondosda profile image Simon Originally published at Updated on ・4 min read

When I started my first work as a developer, the rules were clear:

We request our employees to work on Unix systems so they get used to our servers' environment.

My New Boss

Which meant I had to switch to Linux...

Not that I had never tried it before, but every time I couldn't see a real benefit to it, while hardware recognition was often a problem and it lacked some software like the Adobe suite.

So I jumped on the Linux bandwagon...and I loved it!

When it comes to coding, Linux is amazing.
You have incredible scripting functionalities and easy use of main developer tools like git, node, python, docker, and so on.

All of this is greatly due to the shell! While most of humanity has totally forgotten about command-line interfaces to the benefit of more accessible graphical user interfaces, the shell remains an incredible tool for developers seeking automation and efficiency.

As one of our main tools, it would be too bad not to shape it to our needs. In this article I will present my current setup. I am very interested to know yours though, do not hesitate to share it in the comment section.

Meet the Z shell

While the default bash shell does a great job, there are several reasons to prefer its younger cousin, zsh.

Among those, it comes with a better auto-completion feature, combined with a spelling correction and approximate completion when a command is entered.

zsh spelling correction
zsh spelling correction in action

It also provides interesting usage features such as convenient search patterns using globbing or the use of vi when typing a command (you can enable it with set -o vi).

Last but not least, it provides a lot of customization of the prompt or functionalities with plugins, which I will come back to shortly.

How to install it?

Before starting to customize our shell, we need to set zsh as our default shell.

chsh -s /bin/zsh
Enter fullscreen mode Exit fullscreen mode

The easiest way to customize our shell is to use a framework, the most well-known being Oh My ZSH. You can install it with a simple command.

curl -L | sh
Enter fullscreen mode Exit fullscreen mode

And... That's it!

Now let's dive into the customization of our z shell.

Some useful plugins

A lot of plugins are already downloaded by Oh My Zsh, you just need to enable the one you want by adding them to the plugins list of your .zshrc file.

Here are the plugins I use on my end:

  • git: provides a set of shell aliases for git like g: git, gco: git checkout or gcb: git checkout -b.

  • extract: allows you to extract a file by doing extract file.tar.gz (can be used with the option -r to remove the original file). Much easier to remember than tar zxvf file.tar.gz!

  • virtualenv: displays the current python virtual environment name in the prompt (see below for its customization)

  • zsh-autosuggestion: the one I can't live without anymore! It displays auto-completion based on the previous commands typed.

For zsh-autosuggestion, I use it with an extra configuration, bindkey '^ ' forward-word, that allows you to accept a suggestion word for word by typing ctrl + space (while hitting the right arrow accepts the complete line).

I also add the following line to the .zshrc file: ZSH_AUTOSUGGEST_STRATEGY=(history completion). It allows the autosuggestion to fallback to the system autocompletion, the one you will get by hitting tab, if there is no history.

zsh autosuggestion, once you have tried it, there is no coming back

A custom display

Oh My Zsh comes with a lot of predefined themes for your prompt, and you might just find the one you want there.

This is how I started, but I was quickly frustrated. Either I liked the colors of one but not the display, or I liked the display but the colors were not to my taste.

Trying several themes can be quite time-consuming, so I decided to start from a theme I liked and customized it to get what suited me. In my case this is how it looks like:

my custom display

It is pretty straightforward. It displays (when relevant) the name of your python environment (as python is one of my main programming languages), then the name of the computer, the local directory in relative format, and the current git branch (when relevant as well).

I like the simplicity and compactness of it: all the information that I need is here, on one line, and with no loss of space. This is very personal and I strongly advise you to configure your theme that will best suit your taste.

For this, there's nothing easier, just go to the theme file that you want to modify (in my case ~/.oh-my-zsh/themes/geoffgraside.zsh-theme) and adapt it to your need.

Here is what I came up with.

$(git_prompt_info) %(!.#.$) '
ZSH_THEME_GIT_PROMPT_PREFIX=" %{$fg[yellow]%}("
Enter fullscreen mode Exit fullscreen mode

Each value is framed with %{$fg[<color>]%}<value>{$reset_color%} to set the foreground color.

$(virtualenv_prompt_info) displays the python virtual environment if relevant (thanks to the virtualenv plugin presented above), %n the computer's name, and %~ the relative path.

For the git branch name, I use $(git_prompt_info) with a specific prefix and suffix that allows me to get rid of displaying git: before the branch name.

Final Thoughts

I will stop here, that's already quite a perspective on zsh. There is much more to discover about it, and I hope I gave you a desire to take ownership of your shell.

The best thing I can advise you is to give it a try and then progressively add functionalities that suit you.

You can get the details of my .zshrc config file on my github config files repository.

Discussion (4)

moopet profile image
Ben Sinclair

Most of the things people claim you can do with zsh are scripts collated through oh-my-zsh which is a popular library of tools. They're not zsh things really, and most can be done with bash as well.
The main reason people see zsh as better than bash isn't because of its features. It's because the version of bash on Macs is a decade out of date, - in turn because Apple don't want to use copyleft software - and Macs are overly-represented in the social-media side of development.

simondosda profile image
Simon Author

Thanks for your comment Ben. I don't doubt you can do the same things with bash, as a matter of fact I don't have any strong opinion about bash vs zsh. My goal here is just to present the tools I use in the hope it can be useful to other people. Whatever shell you are using, the time you spend to understand and customize it to your need will always be profitable.

codingsafari profile image
Nico Braun

check this out. You can use it on top of zsh or with other shells too. I am fine with bash and this.

simondosda profile image
Simon Author

Thanks for sharing this Nico, definitely worth a look

Forem Open with the Forem app