Skip to content

Instantly share code, notes, and snippets.

@petronetto
Last active September 7, 2023 10:38
Show Gist options
  • Save petronetto/a8e84265e75d74f87102940b64f5fad9 to your computer and use it in GitHub Desktop.
Save petronetto/a8e84265e75d74f87102940b64f5fad9 to your computer and use it in GitHub Desktop.
ZSH Config
#!/usr/bin/env zsh
color_red=$(tput setaf 1)
color_green=$(tput setaf 2)
color_reset=$(tput sgr0)
#---------------------------------------------------------------------------------------------------------------
# Docker
#---------------------------------------------------------------------------------------------------------------
alias dc='docker compose'
alias dc-up='docker compose up -d'
alias dc-stop='docker compose stop'
alias docker-init='open --background -a Docker'
alias docker-ip='docker inspect -f "{{.Name}} - {{.NetworkSettings.IPAddress }}" $(docker ps -aq)'
alias docker-rm='docker stop $(docker ps -aq) && docker rm $(docker ps -aq)'
alias docker-rm-stoped='docker stop $(docker ps -aq -f status=exited) && docker rm $(docker ps -aq -f status=exited)'
alias docker-start-all='docker start $(docker ps -aq)'
alias docker-restart='docker restart $(docker ps -aq)'
alias docker-stop-all='docker stop $(docker ps -aq)'
alias docker-rmi='docker rmi $(docker images -aq) -f'
alias docker-rm-net='docker network rm $(docker network ls -q)'
alias docker-rm-vol='docker volume rm $(docker volume ls -q)'
alias docker-clean='docker system prune --all -f'
#---------------------------------------------------------------------------------------------------------------
# Git
#---------------------------------------------------------------------------------------------------------------
alias clone="git clone"
alias gmaster="git checkout master"
alias git-clean-branchs="git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d"
alias gfa="git fetch --all"
alias gpull="git fetch -p --all && git pull"
alias gpush="git push 2>&1 | grep \"git push\" | sh"
alias gundo="git color_reset --hard HEAD~1"
alias ggraph="git log --graph --oneline --decorate --all"
#---------------------------------------------------------------------------------------------------------------
# PHP
#---------------------------------------------------------------------------------------------------------------
alias compd='comp dumpautoload -oan'
alias compi='comp install --ignore-platform-reqs'
alias compreq='comp req --ignore-platform-reqs'
alias comprem='comp req --ignore-platform-reqs'
alias pa='php artisan'
alias lsail='[ -f sail ] && sh sail || sh vendor/bin/sail'
#---------------------------------------------------------------------------------------------------------------
# Utils
#---------------------------------------------------------------------------------------------------------------
alias -g N="1>/dev/null 2>/dev/null" # No Output
alias -g B="1>/dev/null 2>/dev/null &" # Background
alias -g A="2>&1" # All (merge stdout/err)
alias -g T="| tee -a /dev/stderr" # Tee to stderr
alias -g S="| sort | uniq" # Sort
alias -g U="| sort | uniq -c | sort -nr" # Count
alias -g UU="| sort | uniq -c | sort -n | sed -E 's/^ +[0-9]+ //g'" # No Number
alias -g L="| awk '{ print length, $0 }' | sort -n | uniq | cut -d' ' -f2" # Sort by Length
alias prev="fzf --preview 'bat --style=numbers --color=always --line-range :500 {}'"
alias ..='cd ..'
alias cls="clear"
alias tailf="tail -f"
alias cat="bat" # https://github.com/sharkdp/bat
alias cd="z" # replace cd with Zoxide command - https://github.com/ajeetdsouza/zoxide
# ls https://github.com/lsd-rs/lsd
alias ls="lsd"
alias la="ls -a"
alias ll="ls -ll"
alias pwd="pwd | tee >(pbcopy)"
alias reload='exec "$SHELL" -l'
#---------------------------------------------------------------------------------------------------------------
# Functions
#---------------------------------------------------------------------------------------------------------------
function docker-rebuild() {
if [[ $1 != "" ]]; then
docker compose up -d --force-recreate --no-deps --build "$1"
else
echo "${color_red}ERROR!${color_reset} ${color_green}You must provide the contaner name${color_reset}"
fi
}
# Composer
comp() {
docker run --rm --interactive --tty \
--volume $(pwd):/app \
--volume ${COMPOSER_HOME:-$HOME/.composer}:/tmp \
--user $(id -u):$(id -g) \
composer:2.6.2 "$@"
}
# mkdir then cd
function mkdir() {
/bin/mkdir "$1"
cd "$1" || return
}
function docker-enter() {
if [[ $1 != "" ]]; then
bash -c "clear && docker exec -it $1 bash"
else
echo "${color_red}ERROR!${color_reset} ${color_green}You must provide the contaner name${color_reset}"
fi
}
# Use: fzf to navigate in the git branches
function gch() {
git checkout "$(git branch --all | fzf | tr -d '[:space:]')"
}
function git-create() {
curl -u 'petronetto' https://api.github.com/user/repos -d "{\"name\":\"$1\"}"
}
function gcheck() {
cmd="git checkout"
if [[ $2 = "--create" ]] || [[ $2 = "-c" ]]; then
cmd="$cmd -b"
fi
if [[ $1 != "" ]]; then
eval "$cmd $1"
else
echo "${color_red}ERROR!${color_reset} ${color_green}You must provide the branch name${color_reset}"
fi
}
# Delete files
function rm-all() {
if [[ $1 != "" ]]; then
find . -name "$1" -exec rm -rf {} \;
else
echo "ERROR! Type a file name"
fi
}
function pyclean () {
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
}
function now() {
date '+%e, %B (%m) %Y - %H:%M'
}
# diff with clipboard
function diffp() {
diff <(cat $1) <(pbp)
}
function month() {
if [[ "$1" =~ ^[0-9]+$ ]]; then
case "$1" in
1) echo "January";;
2) echo "February";;
3) echo "March";;
4) echo "April";;
5) echo "May";;
6) echo "June";;
7) echo "July";;
8) echo "August";;
9) echo "September";;
10) echo "October";;
11) echo "November";;
12) echo "December";;
*) echo "Invalid month number";;
esac
else
case "$(echo "$1" | tr '[:upper:]' '[:lower:]')" in
january) echo 1;;
february) echo 2;;
march) echo 3;;
april) echo 4;;
may) echo 5;;
june) echo 6;;
july) echo 7;;
august) echo 8;;
september) echo 9;;
october) echo 10;;
november) echo 11;;
december) echo 12;;
*) echo "Invalid month name";;
esac
fi
}
#!/usr/bin/env zsh
##############################################################################################
# 888~-_ d8 d8 d8 #
# 888 \ e88~~8e _d88__ 888-~\ e88~-_ 888-~88e e88~~8e _d88__ _d88__ e88~-_ #
# 888 | d888 88b 888 888 d888 i 888 888 d888 88b 888 888 d888 i #
# 888 / 8888__888 888 888 8888 | 888 888 8888__888 888 888 8888 | #
# 888_-~ Y888 , 888 888 Y888 ' 888 888 Y888 , 888 888 Y888 ' #
# 888 "88___/ "88_/ 888 "88_-~ 888 888 "88___/ "88_/ "88_/ "88_-~ #
##############################################################################################
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
if command -v zoxide &> /dev/null; then
eval "$(zoxide init zsh)"
fi
# History command configuration
setopt EXTENDED_HISTORY # record timestamp of command in HISTFILE
setopt HIST_EXPIRE_DUPS_FIRST # delete duplicates first when HISTFILE size exceeds HISTSIZE
setopt HIST_IGNORE_ALL_DUPS # ignore duplicated commands history list
setopt HIST_IGNORE_SPACE # ignore commands that start with space
setopt HIST_VERIFY # show command with history expansion to user before running it
setopt SHARE_HISTORY # share command history data
# beeping is annoying
unsetopt BEEP
# asdf version manager
source $(brew --prefix asdf)/libexec/asdf.sh
# initialise completions with ZSH's compinit
autoload -Uz compinit && compinit
# https://zdharma-continuum.github.io/zinit/wiki/
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
[ ! -d $ZINIT_HOME ] && mkdir -p "$(dirname $ZINIT_HOME)"
[ ! -d $ZINIT_HOME/.git ] && git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
source "${ZINIT_HOME}/zinit.zsh"
zinit ice depth=1; zinit light romkatv/powerlevel10k
# Bundles from the oh-my-zsh
zinit snippet OMZP::branch
zinit snippet OMZP::command-not-found
zinit snippet OMZP::composer
zinit snippet OMZP::gitignore
zinit snippet OMZP::zsh-interactive-cd
# Extra zsh completions
zinit light zsh-users/zsh-completions
zinit light jessarcher/zsh-artisan
zinit ice as"completion"
zinit snippet https://github.com/docker/cli/blob/master/contrib/completion/zsh/_docker
# Syntax highlighting
zinit light zsh-users/zsh-syntax-highlighting
# zsh-autosuggestions
zinit ice wait lucid atload"!_zsh_autosuggest_start"
zinit load zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-history-substring-search
zinit light MichaelAquilina/zsh-you-should-use
zinit light zdharma-continuum/history-search-multi-word # Ctrl+R searches for each word in the line
# Plugin configuration
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(
'bracketed-paste'
'history-search-forward'
'history-search-backward'
'history-beginning-search-forward'
'history-beginning-search-backward'
'history-substring-search-up'
'history-substring-search-down'
'up-line-or-beginning-search'
'down-line-or-beginning-search'
'up-line-or-history'
'down-line-or-history'
'accept-line'
'copy-earlier-word'
'menu-select'
)
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=green,fg=white,bold'
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
zstyle :plugin:history-search-multi-word reset-prompt-protect 1 # protect prompt from being cleared by Ctrl+R
# Aliases
[[ ! -f "$HOME/.zsh_aliases" ]] || source "$HOME/.zsh_aliases"
[[ ! -f "${HOME}/.iterm2_shell_integration.zsh" ]] || source "${HOME}/.iterm2_shell_integration.zsh"
# Generated for envman. Do not edit.
[[ ! -f "$HOME/.config/envman/load.sh" ]] || source "$HOME/.config/envman/load.sh"
[[ ! -f "$HOME/.fzf.zsh" ]] || source "$HOME/.fzf.zsh"
# To customize prompt, run `p10k configure` or edit $HOME/.p10k.zsh.
[[ ! -f "$HOME/.p10k.zsh" ]] || source "$HOME/.p10k.zsh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment