Speed up opening new zsh sessions

I noticed that it got slower than before to open new zsh session. I use zsh on tmux. I shorten the time to open new session with steps following.

Installed starship

I used to use oh-my-zsh with spaceship-prompt. I changed spaceship-prompt to starship.

Installed with Homeberw

brew install starship

And added init command to ~/.zshrc

# ~/.zshrc

eval "$(starship init zsh)"

Quit using Oh My Zsh

After I installed starship, I found it satisfies what I want. I want to see which git branch I’m on, which versions of language I’m using. Starship covered them.

Omit $(…) from .zshrc

To run commands in .zshrc is not very fast. For example, $(brew --prefix). It is not always very slow but some commands are slow. I expanded direct paths and hardcoded them.

Copied asdf init script to .zshrc

It’s not very good idea, but initializing asdf doesn’t seem fast. I copied the code from asdf.sh to my .zshrc. If it won’t work in the future anymore, I will copy it again or omit these lines. Anyway, it works for now.

ASDF_DIR=/usr/local/opt/asdf
ASDF_BIN=$ASDF_DIR/bin
ASDF_USER_SHIMS=$HOME/.asdf/shims

## add to front of $PATH
export PATH=$HOME/.asdf/shims:$ASDF_BIN:$PATH

# shellcheck source=lib/asdf.sh
# Load the asdf wrapper function
#. "${ASDF_DIR}/lib/asdf.sh"
#
# # shellcheck source=lib/asdf.sh
# # Load the asdf wrapper function
asdf() {
  local command
  command="$1"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
    "shell")
      # commands that need to export variables
      eval "$(asdf export-shell-version sh "$@")" # asdf_allow: eval
      ;;
    *)
      # forward other commands to asdf script
      command asdf "$command" "$@"
      ;;

  esac
}

Omit double quotations from .zshrc

I’m actually not sure what the reason is. But somehow PATH="...:$PATH" are sometimes slow. Probably because my $PATH is fat, so expanding them take long. I omit " as many as possible.

Modified .tmux.conf

When I started using tmux, I copied someone’s .tmux.conf. It worked well, so I just kept using it. But whenever I opened a new pane or window, it takes some time.

There was such a line in order to load shell configuration. Actually it was not necessary. Most times, I don’t update .zshrc, so I don’t need to load configuration each time.

I deleted this line.

set -g default-command "${SHELL} -l"

Conclusion

Hope it helps someone.

Contents