Skip to content

Instantly share code, notes, and snippets.

@GLMeece
Last active November 2, 2024 19:03
Show Gist options
  • Save GLMeece/4b51037daa0d6b83256f80b560246f38 to your computer and use it in GitHub Desktop.
Save GLMeece/4b51037daa0d6b83256f80b560246f38 to your computer and use it in GitHub Desktop.
Steps to Terminal Enlightenment on a Mac (tweaking your terminal for fun and profit)

Steps to Terminal Enlightenment on a Mac

Hopefully you'll Live Long and Prosper, so I don't mean terminal as in "you gonna die". This is a somewhat opinionated article on configuring a macOS machine with:

Consider this a "give me an awesome terminal on rails" exercise. If you want to tweak things after the fact, knock yourself out!

Since this has to do with enhancing your Terminal experience on a Mac, most procedures below are to be executed in a terminal. Until you get iTerm2 up and running, the first commands will be with the built-in Terminal app (located in the Utilities folder within the Applications folder).

Step Zero - Install Homebrew

This section applies only if you haven't already installed Homebrew. Hater's gonna hate, but arguably this is still the easiest package manager on the Mac. If you refuse to use Homebrew, you're on your own to figure out the forthcoming instructions.

First, make sure that you have command-line tools installed by executing:

xcode-select -p

If you get a path back (like /Applications/Xcode.app/Contents/Developer) then you're good to go. Otherwise:

xcode-select --install

In the dialog that pops up, click Install and click Agree for the license agreement.

Note: If you get a message that says "Can't install the software because it is not currently available from the Software Update server." then you will need to download it from here (look for Command Line Tools...).

Now, to actually install Homebrew, run this command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

To make sure it's all working correctly, execute:

brew doctor

Finally, just to make sure everything's up-to-date:

brew update

Install Git if You Haven't

Git isn't installed? Well, OK - do this:

brew install git

Optional: Change Path Order

Note: This section is only valid if you are running macOS prior to Catalina (10.15). Catalina and above (e.g., Big Sur, Monterey, Ventura) already has the path order "correctly" as described below.

If you have any packages that Homebrew installs that are newer/better versions of what comes pre-installed on macOS, you should probably change the path order. You can do it the way that the Homebrew docs suggest, or:

sudo nano /etc/paths

You'll see the path order looks like this:

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin

You'll want to move /usr/local/bin to the top of the list, so it looks like this:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Save your changes and quit.

Make Z-shell Your Default Shell

Note 1: This section is only valid if you are running macOS prior to Catalina (10.15). Catalina and above (e.g., Big Sur, Monterey, Ventura) already have Z-shell as the default shell.

Note 2: Here's a great article on Z-shell.

Let's update the Z-shell on your Mac with Homebrew, along with adding the completions extension:

brew install zsh

Now, let's actually change the default shell to the one that Homebrew just installed:

chsh -s /usr/local/bin/zsh

You may get a complaint about a non-standard shell. Deal with it. Deal Wit It

Open up a new terminal session (new window or tab), and you will probably see the following warnings (note: the following capture is with Oh-My-ZSH! already installed - YMMV):

[oh-my-zsh] Insecure completion-dependent directories detected:
drwxrwxr-x  52 gmeece  admin  1664 Sep  4 17:07 /usr/local/share

[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.

[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh]     compaudit | xargs chmod g-w,o-w

[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.

zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]? 

Enter y to proceed, and then execute this:

chmod go-w '/usr/local/share'

Finally, let's verify that Z-shell is our default shell by executing the following commands:

echo $0
# Should return: zsh
which zsh
# Should return: /usr/local/bin/zsh
zsh --version
# Should return a version higher than 5.1; e.g.:
# zsh 5.7.1 (x86_64-apple-darwin18.2.0)

If you're interested in additional (non-standard) Zsh plug-ins, here's a great list.

Install Oh My ZSH!

Oh My Zsh is an open source, community-driven framework for managing your Z-shell configuration. It adds all kinds of functionality to your terminal sessions, and provides a framework for theming what your terminal looks and acts like.

To install:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

A couple of helpful features are auto-suggestions and syntax highlighting, which you can install thusly:

brew install zsh-autosuggestions
brew install zsh-syntax-highlighting

Note: the following seems to be obsolete now... You will need to add the following lines to your .zshrc file:

source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh # ignore this!
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh # ignore this!

Install iTerm2

iTerm2 has so many features above and beyond the standard Mac terminal. Let's just say that once you try it, you'll almost certainly never want to go back. P.S. Here's a cheatsheet. You're welcome.

To install:

brew install iterm2 --cask

We'll come back later and fiddle with appearance and preferences. However, you can start using it now to get used to it (not too different from the regular Terminal at this point).

Install Nerd Fonts

Nerd Fonts are a superset of Powerline Fonts. Trust me on this one - these are the fonts you want.

Hack is my favorite overall, but you can install others as well (other favorites are BlexMono, MesloLGS, and Cascadia Mono). Note that there are many other fonts that work well for an IDE such as VS Code, but that's outside the scope of this Terminal setup. Here are instructions on how to install these fonts via Homebrew (first command only needs to be run once):

brew tap homebrew/cask-fonts
brew install --cask font-hack-nerd-font

To install other fonts is a small bit of work. The form is basically:

brew install --cask font-FONT-YOU-WANT-font

where FONT-YOU-WANT is the name found in this directory, minus the .rb and with the string -nerd- in it. So, for example, if I wanted to install Space Mono Nerd Font, you'd run:

brew install --cask font-space-mono-nerd-font

Refer to the Patched Fonts list for a current list.

PowerLevel9k

Note: The author as upgraded (moved on?) to PowerLevel10K which is a more advanced, faster work-alike. These instructions have not been updated to reflect that, but you can find most of what you need at the project repo or by Googling searching for tutorials on it.

PowerLevel9K does some pretty magic stuff for your prompt. The GitHub repo has lots of information about how it can be used to customize your prompt. Although you can install it via Homebrew, I'd recommend installing it this way:

git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

Select this theme in your ~/.zshrc:

ZSH_THEME="powerlevel9k/powerlevel9k"

Additionally you must set the POWERLEVEL9K_MODE before Oh-My-Zsh is loaded (look for source $ZSH/oh-my-zsh.sh in your ~/.zshrc).

Add:

POWERLEVEL9K_MODE='nerdfont-complete'

Summarizing Necessary .zshrc Entries

Along the way, we've added a number of things to this file. Here is an abridged version of what we should see so far (does not include other optional things you might want to do):

export ZSH=$HOME/.oh-my-zsh # <- Should already be there
SH_THEME="powerlevel10k/powerlevel10k" # Gotta be before sourcing oh-my-zsh
# Oh-My-Zsh plugin section would be here
source $ZSH/oh-my-zsh.sh

For a more generic version, here's an example. If you want a more fully-realized example, take a look at this gist.

Customizing iTerm2

There are quite a number of things you can customize with iTerm2, but we'll just cover a couple of them.

Font

  1. Type: ⌘,
  2. Click the Profiles icon👨.
  3. Click on the Text tab.
  4. Under the Font label, click and select one of the Nerd fonts you've installed - e.g., Hack Nerd Font - along with the style (e.g., Regular) and size (e.g., 18).

Adding Color Presets

This is totally optional, but it gives you a quick way to switch color themes which is a very subjective/personal

Download and Unzip

  1. Download this zipped directory: https://github.com/mbadolato/iTerm2-Color-Schemes/zipball/master
  2. Unzip this directory. It's easiest to just double-click it in the Downloads directory.

Import into iTerm2

  1. Type: ⌘,
  2. Click the Profiles icon👨.
  3. Click on the Colors tab.
  4. Click the Color Presets... ⌄ drop-down
  5. Select Import...
  6. Navigate to your ~/Downloads directory and find a directory that starts out with: mbadolato-iTerm2-Color-Schemes-... and navigate into the schemes sub-directory.
  7. Select all (or select only the ones that look good to you).
  8. Click Open

Now, try out the preset color schemes until you find one you can live with for awhile. My current favorite is Galaxy, but you do you. You can always tweak any of the colors, but be aware as soon as you do, you will need to save it if you want to be able to "jump" back to it again.

Bonus Stuff

  • EXA is a replacement for the ls command in Unix-derived operating systems. I like it enough to include a reference to it here. Installing it is easy: brew install exa. Once you've installed it, you can either just invoke it in place of ls or you can set up some aliases that will perform some of your common tasks for you. I've set up the following two for my most commonly-used directory list commands (see Aliases below):
alias ll="exa -lh"
alias la="exa -lah"
  • BAT is a syntax-aware replacement for CAT. Installation is simply via brew install bat. Once installed, if you want to take a quick glance at a file, just bat foo.py or whatever the filename is. Almost certainly, it will have a built-in syntax coloring for that filetype.

  • I create a separate aliases file to store all my aliases. You may certainly embed all of your aliases in your .zshrc file, but I find it easier to maintain them if they're in a separate file. I personally name the file .bash_aliases even while using Z-shell so that BAT (see above) can recognize it. I add this line to my .zshrc file:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Example aliases might include:

# Directory Shortcuts
alias home="cd ~/;exa -lh"
alias desk="cd ~/Desktop;exa -lh"

# Applications
alias nano=/opt/homebrew/bin/nano
alias finder="open ."
alias chrome='open -a "Google Chrome"'

# Editing Shortcuts
alias zed="nano ~/.zshrc"
alias alied="nano ~/.bash_aliases"

# Python
alias venv="source .venv/bin/activate"

# Git
alias gs="git status"
  • A newer version of Nano. Just brew install nano and then add an alias to nano to point to its Homebrew location; e.g., alias nano=/opt/homebrew/bin/nano. Once you've done that, make this version aware of syntax files by executing nano ~/.nanorc and adding this line:

    include "/opt/homebrew/Cellar/nano/*/share/nano/*.nanorc"

Feedback

If you find errors or have comments, contact the author: Greg Meece → glmeece [at the domain of...] gmail

@khizirsiddiqui
Copy link

Hi, thanks for the guide.
With Homebrew 3.5.6, I had to change the source commands as following in .zshrc

source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh

@GLMeece
Copy link
Author

GLMeece commented Sep 28, 2022

Thanks @khizirsiddiqui! I've updated the gist now. 😄

@Abdel175
Copy link

Abdel175 commented Nov 9, 2022

tried to install hack nerd font. But this command didn't work
$ brew cask install font-hack-nerd-font

Error: brew cask is no longer a brew command. Use brew <command> --cask instead.

@nemathsnv
Copy link

You have to use install before cask.
brew install --cask font-hack-nerd-font

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment