Skip to content

Instantly share code, notes, and snippets.

@LukasBombach
Last active January 5, 2024 09:06
Show Gist options
  • Select an option

  • Save LukasBombach/00662c40faaaa0d2cc46ed5f759549a9 to your computer and use it in GitHub Desktop.

Select an option

Save LukasBombach/00662c40faaaa0d2cc46ed5f759549a9 to your computer and use it in GitHub Desktop.
# My oh my zsh aliases and functions
# Configure oh my zsh
alias zshrc="code ~/.zshrc"
alias pkg="code ./package.json"
alias dev="run dev"
alias build="run build"
alias grstf="git checkout upstream/main --"
# Clone a repo into ~/Projects and open code
function np() {
if ! test -d "$HOME/Projects/$(basename "$1" .git)" ; then
git clone "$1" "$HOME/Projects/$(basename "$1" .git)"
else
code "$HOME/Projects/$(basename "$1" .git)"
fi
}
# Kill processes at a given port
function killport() {
lsof -i TCP:$1 | grep LISTEN | awk '{print $2}' | xargs kill -9
echo "Port" $1 "found and killed."
}
# Run a command either by yarn or pnpm depending on the package manager in use
# I am using yarn as a default because I like it more than npm
function run() {
local command="${1:-install}"
if test -f "yarn.lock"; then
echo "\n> yarn $command\n"
yarn $command
elif test -f "pnpm-lock.yaml"; then
echo "\n> pnpm $command\n"
pnpm $command
else
echo "\n> yarn $command\n"
yarn $command
fi
}
# Merge main from the upstream to the current branch and push to origin
function gmu() {
echo ""
echo "> git pull origin $(current_branch)"
git pull origin $(current_branch)
echo ""
echo "> git fetch upstream"
git fetch upstream
echo ""
echo "> git merge upstream/main --no-edit"
git merge upstream/main --no-edit
echo ""
echo "> git push origin $(current_branch)"
git push origin $(current_branch)
echo ""
}
# nvm with automatic node version switch based on the .nvmrc of a directory
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
autoload -U add-zsh-hook
load-nvmrc() {
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
nvm use
fi
elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment