top of page

How to give your zsh shell a productivity boost on macOS

Updated: Mar 12, 2023

In this little tutorial, I will show you how to give your zsh a significant productivity boost as a developer. There are four simple steps involved in this process.


Step 1 - Make configuring your zsh easier by installing Zim. An alternative often used zsh configuration tool is Oh My Zsh, but this tutorial does not cover that framework.


Step 2 - Install a few open sources fonts that improve the readability of your zsh.


Step 3 - Add a little flavor to your zsh by installing Spaceship.


Step 4 - Tune Spaceship and Zim to your liking.


These steps will make your prompt configurable to show lots of extra information and add a lot of shortcut commands that help you speed up during development. Let's look at how to take all of the steps necessary.


Step 1 - Install Zim

The first step in our process is to install Zim.


Step 1A - Go to https://zimfw.sh.


Step 1B - Download the Zim installation script and execute it like this:

$ curl -fsSL https://raw.githubusercontent.com/zimfw/install/master/install.zsh | zsh

Curl is by default available on macOS, so no need to worry about additional installation steps. This command downloads the install script using curl and makes that the input for zsh shell. The | (pipe) symbol takes the output from one command and uses it as the input for the following command.


Step 2 - Install Powerline fonts

The Powerline font sets are needed to handle a few special characters that Spaceship uses to, e.g., indicate that you are looking at a folder with git active.


Step 2A - To install these fonts, clone the fonts from GitHub to a convenient folder. Once you've installed them, you can also remove the folder again.

$ git clone https://github.com/powerline/fonts.git

Executing this command creates a fonts folder.


Step 2B - Next, change into the fonts folder and run the following command:

$ ./install.sh 

Executing this command installs quite a long list of different fonts that support the necessary characters for programming languages, like Rust, Python, Dotnet, Java, tools git, AWS, etc. Here is an example of changing into a folder under git version control with Rust code.


You have to change your Terminal configuration to use one of these fonts. You can do so by pressing ⌘-comma. That command opens the Terminal configuration and looks like this. Change the font to one of the Powerline fonts. The picture below shows that I used Anonumice Powerline with font size 18.

Now that we have configured our Terminal to use one of the Powerline fonts, we are ready to install Spaceship.


Step 3 - Install Spaceship


Step 3A - Go to https://spaceship-prompt.sh. This step created a file called .zimrc in your home folder. You can check this as follows:

$ cat ~/.zimrc

Step 3B - Add the following line to the .zimrc file you just created.

$ echo "zmodule spaceship-prompt/spaceship-prompt --name spaceship" >> ~/.zimrc

Step 3C - Run zimfw install.

$ zimfw install

The output of running this command looks like this when successful:

~
Done with install. Restart your terminal for any changes to take effect.

Your prompt will look as follows after you restart:

~
% 

Step 3D - Terminate your Terminal and restart for the above changes to take effect. You should already see a changed prompt after you continue.


Step 4 - Configure and tune Spaceship.

Now you are reading to configure and tune Spaceship to your liking.


Step 4A - Tune-up Spaceship. Add the following at the end of your .zshrc. I recommend putting a # in front of every line of tools you don't use. Removing unnecessary modules improves the performance of your shell.

SPACESHIP_PROMPT_ORDER=(
  time          # Time stamps section
  user          # Username section
  dir           # Current directory section
  host          # Hostname section
  git           # Git section (git_branch + git_status)
  hg            # Mercurial section (hg_branch  + hg_status)
  package       # Package version
  gradle        # Gradle section
  maven         # Maven section
  node          # Node.js section
  ruby          # Ruby section
  elixir        # Elixir section
  xcode         # Xcode section
  swift         # Swift section
  golang        # Go section
  php           # PHP section
  rust          # Rust section
  haskell       # Haskell Stack section
  julia         # Julia section
  docker        # Docker section
  aws           # Amazon Web Services section
  gcloud        # Google Cloud Platform section
  venv          # virtualenv section
  conda         # conda virtualenv section
  python        # Python section
  dotnet        # .NET section
  ember         # Ember.js section
  kubectl       # Kubectl context section
  terraform     # Terraform workspace section
  ibmcloud      # IBM Cloud section
  exec_time     # Execution time
  line_sep      # Line break
  battery       # Battery level and status
  jobs          # Background jobs indicator
  exit_code     # Exit code section
  char          # Prompt character
)

* Updated March 12, 2023 - in previous versions of Spaceship, there was a vi_mode, which has been removed, but can still be installed by following these instructions, and there was pyenv section which is replaced by a python section.


Step 4B - Some more tuning and learning about shortcuts.

Both SpaceShip and Zim provide you with many options for customization and commands.

  • Please have a look here at tuning your Spaceship configuration further.

  • And please have a look at this cheatsheet from Zim to learn about shortcut commands that Zim provides.

I always do one tune-up to put the zsh key bindings into Vim mode. You can do that by editing the .zshrc file and looking for the following lines. By default, the Zim installation will put the keymapping to emacs, as you can see below. Replace -v with -e to switch to Vim mode.

# Set editor default keymap to emacs (`-e`) or vi (`-v`)
bindkey -v

Changing the key binding to vim also changes your prompt to look like this:

~ 
[I] ➜ 

The [I] means you are in insert mode. You can exit the insert mode and switch to normal mode by pressing ESC to notice the difference. Pressing ESC also changes your prompt to look like this:

~ 
[N] ➜ 

And you will immediately see the difference.


The picture below shows all of the steps we took into effect. It shows that I am in folder guessing_game on branch master, that there are no files staged files, that the folder contains Rust code, and that I am using Rust version is v1.57.0.

Isn't that neat?

 

That's it. I hope you enjoy this tutorial. Feel free to drop me a line if you have any questions. Happy to help out where I can.

40 views0 comments

Comentarios


bottom of page