Skip to content

Instantly share code, notes, and snippets.

@Magnuti
Last active November 20, 2024 10:01
Show Gist options
  • Save Magnuti/16b5941b5b70700d65f0917ce19e868b to your computer and use it in GitHub Desktop.
Save Magnuti/16b5941b5b70700d65f0917ce19e868b to your computer and use it in GitHub Desktop.
Useful .zshrc settings

Autocomplete

autoload -Uz compinit && compinit

Ignore duplicate commands in history

# Ignore duplicate commands in history
setopt histignoredups

Git branch name in path

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_GIT=$'%F{39}'
COLOR_DEF=$'%f'
setopt PROMPT_SUBST
export PROMPT='%~${COLOR_GIT} $(parse_git_branch)${COLOR_DEF}$ '

Kill process on port

function kill_on_port() {
    if [ $# -eq 0 ]
    then
        echo "Please provide a port number"
        return
    fi

    if [ -z "$1" ]
    then
        echo "No port supplied"
        return
    fi

    kill -9 $(lsof -ti:$1)
}

Automatically use Node version from .nvmrc file on path change

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

useNvmrcIfExist() {
    if [[ -f ".nvmrc" ]] {
        nvm use
    }
}

# Automatically call useNvmrcIfExist() when chaning directory
autoload -U add-zsh-hook
add-zsh-hook chpwd useNvmrcIfExist
useNvmrcIfExist # Call it once when opening terminal

Useful alias

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