Python Oh My Zsh for Fun and Productivity

Python Oh My Zsh for Fun and Productivity

Sep 19, 2020

As a Pythonista you probably spend quite a bit of time on terminal in different stages of development. In this Python Oh My Zsh tutorial I will introduce you to a powerful tool which will surely make you more productive on the command line. We will first talk a bit about the importance of an optimized environment and an introduction to Oh My Zsh. Then we'll shift gears to talking more specifically about Python.

I hope you will enjoy this overview of Python Oh My Zsh and it inspires you to make your work environment just a bit more optimized. While setting up these changes can require upfront investment of time, you will find that in the long run this will be worth it. Using more powerful tools don't just save you the time directly avoided by typing a few characters less. More than that it can help you reduce your cognitive load, which is at a premium when you're working on a non-trivial project, as you already have a lot going in your head. I would like to conclude this argument by referring to an insightful blog by Kent Dodds, a massive figure in the JS world, titled An Argument for Automation: Why it can be worth spending 1 hour automating a 10 second task.

Background on Python Oh My Zsh

Oh My Zsh is an open source program which provides an easy way to install themes and plugins in your terminal. If you are using Ubuntu or older versions of Mac, you might be using the terminal called "bash" (Bourne Again Shell) by default. bash is a solid choice for your shell but there's another big fish in the game called zsh. We won't go into the details of their distinctions and I personally enjoy using both. In short zsh is extended bash with certain improvements, in turn inspired by other shells like ksh and tcsh. In case you want to learn more about the awesome things you can do with zsh or learn more about shell scripting in general, you might want to refer to these top knotch literature.


Oh My Zsh

Oh My Zsh is simply an aggregator of third-party themes and plugins for zsh. It provides a simple and uniform way to install and configure different themes and plugins. It also has an auto-update tool to update the installed themes and plugins. You can look at their GitHub to see their vast collection of zsh themes and plugins.

Ensuring zsh is installed

You should first check which shell you are currently running. On a Linux based or Mac machine you can do this by opening the terminal and entering the command: echo $SHELL

If the output is /usr/bin/zsh, /bin/zsh or anything suggesting it's running a zsh binary, it means you already have zsh installed and it's your default shell. You can now skip to the installation of Oh My Zsh.

In case your output for the above command was something different, it's likely you either don't have zsh installed, or zsh is not your default shell.

You can check if zsh is installed (and in your PATH) by simply running zsh on your terminal. If zsh is present it will open a zsh shell as a child inside your parent shell. Otherwise it will throw an error saying zsh is not a recognized command.

If zsh wasn't recognized you will have to install it first. On Ubuntu and other Debian based systems you can do so by running these two commands:

sudo apt update
sudo apt install zsh

This will install zsh and you can confirm it by running zsh command again on your terminal.

Ensuring zsh is your default shell

When you open a terminal and run the echo $SHELL, the output will indicate which shell you're using. If it doesn't say zsh we need to switch out default shell to zsh. One of the safest and easiest ways to do this is to run the following command: chsh -s $(which zsh)

As you might have guessed, it calls chsh which is used to change the shell, and which zsh will give you the full path to the zsh executable.

Installing Python Oh My Zsh

Like mentioned before, Oh My Zsh have a repository on GitHub. You can read the simple and well-explained instructions on their GitHub's readme to learn how to install Oh My Zsh and also how to configure your themes and plugins.

Generally, installing a plugin on Oh My Zsh is as simple as adding an item to your plugins list in ~/.zshrc file. You can use any of your favorite text editors like vim (vi ~/.zshrc), nano (nano ~/.zshrc) or gedit (gedit ~/.zshrc) to edit this file. Once you open this file you need to navigate to the appropriate configuration item and make the changes there. For example to change your zsh's theme you just need to update the ZSH_THEME="robbyrussell" line to ZSH_THEME="agnoster" and so on. The same applies for plugins and you can add or remove a plugin by updating the PLUGINS list in ~/.zshrc.

Personally I like to keep my configurations lightweight and don't use too many plugins. And the choice of plugins and themes is quite personal and will vary for each person based on their preferences as well as their workflow/style. However there's one feature on a terminal that I value very highly and which I feel can lead to significant improvement in productivity. And that's the autosuggestions feature which we'll discuss next.

Oh My Zsh Autosuggestions

Unfortunately Oh My Zsh doesn't have an inbuilt or easy way for adding autosuggestions. So we will have to perform an extra bit of work to get autosuggestions working on our Oh My Zsh setup. So we will use a tool called zsh-autosuggestions to add Oh My Zsh Autosuggestions. This tool provides a convenient way for Oh My Zsh users to install it to their setup.

You simply need to visit their Oh My Zsh installation section. There you will see the simple instructions to easily add zsh-autosuggestions to your Oh My Zsh.

Python Oh My Zsh plugin

Oh My Zsh is a great tool for working on the terminal in general. It's useful no matter what kind of work you are doing or what programming language you are using. However as Python is one of my go-to tools and given this blog is about Python, I will talk about the Python plugin for Oh My Zsh.

You can add the Python plugin by simply adding python to your PLUGINS list in ~/.zshrc. You can visit the GitHub for this Python Oh My Zsh plugin to learn about the usage. The main functionalities it provides is searching for a .py file, or searching for a specific content within .py files. It also has a useful command for deleting byte code and cache files, and some other functionalities.

There are also some other Python related plugins on Oh My Zsh such as pip which caches the names of available pip packages from PyPi index.

Python Oh My Zsh: Using the Django plugin

One of my favorite Pytho-related plugins on Oh My Zsh is its django plugin. You can view its README on their GitHub. It provides tab-completion for the related subcommands which are used all the time if you are developing a Django app. This plugin along with the Oh My Zsh autosuggestions tool by itself is bound to save you some precious time when you're working on developing a web app.

powerlevel10k theme for Python Oh My Zsh

Themes on a terminal are more than just about visual aesthetics. And if one has a doubt over it you should simply introduce them to the powerlevel10k theme. This is an feature-packed theme which really packs a lot of punch. To just name a few things, it can show you the returned status of previous command, execution time of previous command, current git branch, disk_usage, current Python virtualenv, your battery status, your public IP and many more, all directly on to your terminal's prompt. Of course you are not going to use most of them and the way you use this tool will depend on your specific needs. It's easy to customize powerlevel10k and the defaults are sensible. You might want to enable the Python related prompt sections which are disabled by default. You can install powerlevel10k from their GitHub README's Oh My Zsh section. You will also want to install some extra fonts that are needed by powerlevel10k.

If you found this article useful, consider buying me a coffee :)


Nov 11, 2020 06:20
Wow I feel like a l33t hacker after reading this.

Copyright © 2020 Python Automation Tutorial. All Rights Reserved