Level up your terminal: How to configure autocomplete in your macOS terminal
Tags: tutorial, cli, programming, productivity
Published: 2023-07-10 20:06:21

The terminal is a powerful tool. It's useful for many tasks, from creating, copying, moving and deleting files, to executing scripts and running programs. However, its default settings are not particularly user-friendly, and as software engineers we're used to our code editors helping us out.

Don't despair! With some simple changes you can drastically improve the zsh terminal experience by adding two super useful features - autosuggestions and syntax highlighting. And if you've just got [a new MacBook](https://mbrookson.uk/blog/2021-macbook-pro-initial-thoughts) like me then this is the perfect time to level up your setup!

Follow these simple steps to get set up:

## Use Homebrew to install zsh plugins

- Install zsh-autosuggestions
- Install zsh-syntax-highlighting

```shell
brew install zsh-autosuggestions
brew install zsh-syntax-highlighting
```

More plugins, including these two, can be found in the [zsh community project repo](https://github.com/zsh-users) on Github.

\* _This step assumes you already have [Homebrew](https://brew.sh) installed. I'll do a post on this soon! If you don't use Homebrew then check out the docs linked above for alternative ways to install._

## Add to your `.zshrc` file

If it doesn't already exist, create this file in the root of your user directory. The easiest way to do this is to run `touch ~/.zshrc`

Add the following lines to the `.zshrc` file to import the code for the plugins

```shell
source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
```

For more information about this file and other zsh configuration files, see [this post](https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout).

## Active the zsh changes

For your changes to take effect either restart your terminal window or run this command to force zsh to reload its sources in `.zshrc`:

```shell
source ~/.zshrc
```

And that's it! 🎉

Try it out for yourself. Now when you type in the terminal you should see syntax highlighting. Let's take the `mkdir test` command for example.

Typing `m` shows in red to highlight this is not a valid command. ![zsh syntax highlighting warning](https://mbrookson.uk/images/zsh-syntax-highlighting-warning.png 'zsh syntax highlighting warning')

Continuing to type `mkdir` turns it green, highlighting that this is a valid command! Complete running this command with `mkdir test`. ![zsh syntax highlighting match](https://mbrookson.uk/images/zsh-syntax-highlighting-match.png 'zsh syntax highlighting match')

Now this command has been run, the next time we type `m` the auto-suggestions has remembered the previous command and suggests it to us. Just hit the right arrow to auto-complete
the suggestion! ![zsh auto-suggestions](https://mbrookson.uk/images/zsh-auto-suggestions.png 'zsh auto-suggestions')

I hope this small change facilitates your workflow and makes using your zsh terminal shell more enjoyable!

---

For even more options to enhance your terminal check out projects like oh-my-zsh and the Fish shell. However, the simple changes outlined in this post are a great addition to level up your terminal
experience.