ZSH Customization for MacOS

6519 VIEWS

· ·

Before we begin, this article is a great place to start to get zsh installed in order to customize your zsh prompt. 

In this tutorial I will walk through my zshrc file and demonstrate how to set up syntax highlighting and auto-suggestions. How to install and use some of the most common plugins for your zshrc file, shown below:

  • zsh-autosuggestions – auto suggests commands while typing in zsh prompt
  • zsh-syntax-highlighting – highlights commands while typing in zsh prompt
  • history – returns your historical commands and allows to easily grep previously run commands
  • common aliases – provides shortcuts for many commonly used commands

Before we dive into all the fun things you can do with your terminal, you may be asking but where does all this custom configuration live? That’s a great place to begin this journey. 

You will need to go to open iTerm and navigate to your /etc directory, there you will see this file named zshrc. This is where you begin to customize your zsh prompt. 

Then type “vi ~/.zshrc” if you’re using vim. You could also use nano or your favorite text editor of choice. 

This will take you to your configuration file where you can begin customizations. Your zshrc file will look something like this: 

Text-Highlighting and Auto-Suggestions

Now that we have the rc file up for zsh configuration. We can start with the customizations! 

First, let’s enable text-highlighting, this is a common plugin used by engineers to easily highlight your commands as you type them. This allows you to notice syntax errors and fix them prior to running the command. 

You will need to download the following repos. 

  1. zsh-autosuggestions: “git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions”
  2. zsh-syntax-highlighting: “git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting”

After cloning the repos, you can now add the edits in your plugins in the zshrc file like shown below. 

Navigate to your zshrc file by typing “vi ~/.zshrc” if you’re using vim, and make the following edits to the file. 

“plugins=(git zsh-autosuggestions zsh-syntax-highlighting)”

After making the changes, close the configuration file, and reopen your terminal. You’ve now set up autosuggestions, and syntax-highlighting. 

An example of this working is shown below, as you can see as I type “git” into the command line the text is highlighted from the previous git command that I ran. 


I can press the up arrow key on my keyboard and it will autofill in the command, or I can press the tab key and the options for “git” will show up and I can sort through the list and select the command I would like to use with git like so. 

History

In your zshrc file, let’s add some plugins that will make your life a whole lot easier. The first plugin we will install is the history plugin. Traditionally if we recall a command from history we would grep it through the terminal.

Luckily there is a plugin that makes this a bit easier to use. The plugin is called history in zshrc. For this plugin we will just be adding to your zshrc file under plugins just like we did for zsh-autosuggestions and zsh-syntax-highlighting. 

Open and close the terminal after applying the update. 

You can test out the plugin by typing “h” in your terminal. Your search history will be returned.

Typing “hs”  followed by a search time will search your history with a term in mind. I searched for all of my commands containing “sudo” in this example below.

Common Aliases

The last plugin we will go over is called common-aliases. This plugin creates helpful shortcut aliases for many commonly used commands. You will need to add this final plugin to your zshrc file. You can read more about this plugin here. 

We can list all the dot files on our system pretty quickly using the alias “ldot”. 

In addition to this plugin, when you set up zsh it does automatically come with some aliases built-in. You can view what comes with zsh by typing “alias” in the zsh prompt.

Conclusion

In this article we gave you a quick walkthrough on how you can customize your zsh prompt by making simple changes to your zshrc file. You may feel like you are a wizard now using your new terminal but there’s still more you can do with zsh. The possibilities are endless, you can even write your own plugins through the github repo.  They currently have over 2k contributors, so remember to have fun with it! 


Alicia Luciano is a Platform Infrastructure Engineer with 6 years experience in technology. She has worked with databases as a DBA at a large-scale organization, focusing on ML to predict growth trends for capacity planning strategies. She then dove into SRE type work as an Infrastructure engineer at several startups throughout the Colorado area, where she focuses heavily on monitoring and creating SLIs,SLOs and SLAs for micro-service systems.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu
Skip to toolbar