Trust us when we say Oh My Zsh makes your terminal usage much 10x more enjoyable and extremely fun even when working.
However, while it provides a great default setup out of the box, there are many ways to further customize and enhance your Zsh environment.
In this tutorial, we will explore the advanced tips for Oh My Zsh users, focusing on syntax highlighting and other productivity-boosting features.
Installation
NOTE: Ensure that you have Zsh installed on your system. You can use your default package manager to configure it on your machine.
Next, if you haven’t already, you can install Oh My Zsh by running the following command in the terminal:
Basic Configuration
Oh My Zsh creates a default configuration located in “~/.zshrc”. Whenever you need to customize it, you need to edit this file using a text editor.
Syntax Highlighting
Syntax highlighting in Oh My Zsh allows us to visually distinguish the commands, files, and more. It also allows you to know whether the command that you wish to run is correct or not.
To enable it, start by cloning the repository.
Next, add or update the following line in your “~/.zshrc”.
Finally, apply the changes by reloading the configuration file.
You can customize the highlighting colors to suit your preferences. You can do this by defining the color configurations in “~/.zshrc”.
ZSH_HIGHLIGHT_COLORS['pattern']='fg=blue,bold'
This example sets the color for pattern matching to blue and bold.
Oh My Zsh also allows us to enable the highlighting for aliases and functions. To enable this feature, we can add the following entries to the Zsh configuration file:
ZSH_HIGHLIGHT_ENABLE_FUNCTIONS=true
In other cases, you may wish to disable the syntax highlighting for specific commands. We can achieve this by adding exceptions in the configuration as demonstrated in the following example:
ZSH_HIGHLIGHT_IGNORE_SELF=true
These lines prevent the highlighting of comments and self-executed commands.
Advanced Prompt Customization
Let us move on to a more advanced prompt customization using the Oh My Zsh framework.
We can use the Powerlevel10k theme to get a highly customizable and feature-rich prompt.
We can install it by running the following command:
We can then set the theme by editing the “~/.zshrc” file. Add the entry as follows:
Upon launch, the theme will run the setup wizard which walks you through the basics of setting up the theme.
For a complete guide on this, we have an existing tutorial on how to setup and configure the Powerlevel10k.
Custom Elements
Powerlevel10k allows us to add the custom prompt elements. For example, to display the current Git branch and status, we can add the entry as follows:
Prompt Themes and Styles
To further customize the appearance of the prompt, we can define custom styles in “~/.zshrc”. Reference the documentation for more details.
Plugins and Auto-Suggestions
Oh My Zsh has a vast collection of plugins that enhance the shell experience. To manage plugins, add them to the plugins array in your “~/.zshrc”.
For example, to enable the Git and Docker plugins, use the following command:
Adding Custom Plugins
If you can’t find a specific plugin, you can create your own or clone; one from a Github repository. Place the plugin script in “~/.oh-my-zsh/custom/plugins/plugin_name” and add it to the plugins array.
Zsh Auto-Suggestions
Zsh also supports auto-suggestions which predict and complete your commands based on the previous command history. To enable it, add the following entry into “~/.zshrc”.
Aliases and Functions
Aliases are commands that act as subsequent or shortcuts for long commands. To define a custom alias, edit the Zsh configuration file and follow the given syntax:
alias update='sudo apt-get update && sudo apt-get upgrade'
In the given example, the “ll” alias represents the “ls –alF” command while “update” represents the full update commands.
Zsh Functions
We also have the ability to define functions in Zsh. Functions are more powerful than aliases as they include a custom logic and execution.
We can define them in the same configuration file as follows:
local current_datetime
current_datetime=$(date "+%Y-%m-%d %H:%M:%S")
echo "Current Date and Time: $current_datetime"
}
We can then run the previous command to display the date and time as follows:
Organizing the Aliases and Functions
To keep the aliases and functions organized, we can create separate files in “~/.oh-my-zsh/custom/aliases” and “~/.oh-my-zsh/custom/functions”, and then source them in the “~/.zshrc” config file.
source ~/.oh-my-zsh/custom/functions/*
This allows you to keep the aliases and functions in different subdirectories while still accessing them at any part in the filesystem.
Conclusion
In this tutorial, we walked you the basics of installing and setting up Oh My Zsh on your machine. We then proceeded to cover more complex and detailed concepts such as prompt customizations, theme installations, plugin usage, auto suggestions, aliases and functions, and many more.