Skip to main content

.zshrc

.zshrc is a configuration file used by the Zsh shell. Zsh is a Unix shell that is similar to Bash, but offers more features and customization options.

The .zshrc file is executed every time a new instance of the Zsh shell is launched. It contains commands and settings that customize the behavior of the shell, such as environment variables, aliases, and shell options.

Some common examples of configurations that can be set in the .zshrc file include:

  • Setting the $PATH environment variable to include directories containing executables
  • Defining aliases for frequently used commands
  • Customizing the prompt to display additional information
  • Enabling or disabling shell options, such as history searching or tab completion

By editing the .zshrc file, users can tailor the behavior of the Zsh shell to their specific needs and preferences.

Suggested Configuration

# Shell options
setopt autocd
setopt extendedglob
setopt histignorealldups
setopt incappendhistory
setopt nobeep

export EDITOR="/opt/homebrew/bin/micro"

alias nano="micro"
alias top="bpytop"
alias ls="exa --icons"

#unset -f nvm node npm
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion

# place this after nvm initialization!
autoload -U add-zsh-hook

# Automatically load nvmrc when entering a directory with a .nvmrc file
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"

# if there is a .nvmrc file, use it
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

# if nvmrc version is not installed, install it
if [ "$nvmrc_node_version" = "N/A" ]; then
# if nvmrc has an unsupported version, then install the latest version
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
# if the nvmrc version doesn't match the current version, then use the nvmrc version
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
# if there isnt a .nvmrc file, and the node version is not the default, then revert to default
echo "Reverting to nvm default version"
nvm use default
fi
}

# Automatically load nvmrc when changing directories
add-zsh-hook chpwd load-nvmrc
# Automatically load nvmrc when opening shell
load-nvmrc

#kill a process in port (macOS)
function kill-port {
if [ "$1" != "" ] # if port is not empty
then
# kill process in port
kill -9 $(lsof -ni tcp:"$1" | awk 'FNR==2{print $2}')
else
# if port is empty print usage and exit
echo "Missing argument! Usage: kill-by-port $PORT"
fi
}