Skip to content

Instantly share code, notes, and snippets.

@dardo82
Last active February 9, 2024 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dardo82/a866c7f10729118f5400bc3f4c693370 to your computer and use it in GitHub Desktop.
Save dardo82/a866c7f10729118f5400bc3f4c693370 to your computer and use it in GitHub Desktop.
ZSH configs
#!/bin/zsh
# ZSH conf
# Set OMZ options.
export ZSH="$HOME/.oh-my-zsh"
zstyle ':omz:update' mode reminder
# Set history options.
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE="$[2**32]"
export SAVEHIST="$[2**32]"
setopt EXTENDED_HISTORY
setopt HIST_IGNORE_DUPS
setopt SHARE_HISTORY
setopt HIST_VERIFY
# Set shell options.
setopt NOTIFY
setopt EXTENDED_GLOB
setopt MAGIC_EQUAL_SUBST
setopt NUMERIC_GLOB_SORT
# Set completion options.
export CASE_SENSITIVE="true"
export COMPLETION_WAITING_DOTS="true"
zstyle ':completion:*:*:*:*:*' menu select
# Set plugins.
plugins=(zsh-autosuggestions zsh-syntax-highlighting)
# Set keys bindings.
export WORDCHARS=""
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[1;3C" vi-forward-blank-word
bindkey "^[[1;3D" vi-backward-blank-word
bindkey "^U" backward-kill-line
autoload backward-kill-word-match
zle -N backward-kill-space-word backward-kill-word-match
zstyle :zle:backward-kill-space-word word-style space
bindkey "^[w" backward-kill-space-word
bindkey "^[[3~" delete-char
bindkey " " magic-space
bindkey "^['" insert-composed-char
# Set default programs.
export LESSOPEN="|~/.lessfilter %s"
export PYGMENTIZE_STYLE="default"
export BROWSER="google-chrome"
export EDITOR="vim"
# Set language.
export LC_ALL="C.utf8"
# Set display.
export DISPLAY=":0"
# Set pipx directories.
export PIPX_BIN_DIR="/usr/local/bin/"
export PIPX_HOME="/usr/local/pipx/"
# Set go directories.
export GOPATH="/usr/local"
# Enable colored commands.
if [ -x $(which gdircolors) ]; then
test -r ~/.dircolors && eval "$(gdircolors -b ~/.dircolors)" || eval "$(gdircolors -b)"
alias cal='ccal'
alias diff='colordiff'
alias ls='ls --color'
alias grep='grep --color'
alias less='less -R --use-color'
export LESS_TERMCAP_mb=$'\E[1;31m' # begin blink
export LESS_TERMCAP_md=$'\E[1;36m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'\E[01;33m' # begin reverse video
export LESS_TERMCAP_se=$'\E[0m' # reset reverse video
export LESS_TERMCAP_us=$'\E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'\E[0m' # reset underline
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
fi
# Set command prompt.
case "$TERM" in
linux|*color*) PC=1;;
esac
if [ $PC ]; then precmd() {
TL_PS1="%b%F{%(#.blue.green)}┌(%B%F{%(#.red.blue)}${USER%@*}%(#.:.@)$HOST%b%F{%(#.blue.green)})─[%B%F{reset}%~%b%F{%(#.blue.green)}]"
TR_PS1="%b%F{%(#.blue.green)}[%B%F{reset}%T%b%F{%(#.blue.green)}]─(%B%F{%(#.red.blue)}%D{%d/%m}%b%F{%(#.blue.green)})"
BL_PS1="%b%F{%(#.blue.green)}└>%B%F{%(#.red.blue)}$%b%F{reset} "
BR_PS1=$'%(?.. %? %B%F{red}⨯%b%F{reset})%(1j. %j %B%F{yellow}⚙%b%F{reset}.)'
TR_PS1_L="${#${(%)TR_PS1}//$'\e'\[[0-9]##m}"
TL_PS1_L="${#${(%)TL_PS1}//$'\e'\[[0-9]##m}"
PAD_PS1="${(r:$[COLUMNS-$TL_PS1_L-$TR_PS1_L-2]::─:)}"
PROMPT="$TL_PS1─$PAD_PS1─$TR_PS1"$'\n'"$BL_PS1"
RPROMPT="${(%)BR_PS1}"
} fi
# Define aliases.
alias hbu="home-backup"
alias hl='highlight -O ansi'
alias sued="sudo $EDITOR"
alias rb="sudo reboot"
alias gh-sr="gh search repos -L 1 -q '.[].fullName' --json fullName"
alias psn="port search --name"
alias spi="sudo port install"
alias tmu-md="tmutil machinedirectory"
alias beep="sudo sh -c \"echo '\a' > /dev/console\""
# Define functions.
mpv-yt-lw() {ID=$(yt-dlp -v --get-id --playlist-items ${1:-1} --cookies-from-browser chrome+gnomekeyring :ythistory); mpv -v --ytdl-format=135+140 ytdl://$ID}
lsl-gist() {gh gist list -L 100 | gawk -v RE=$1 '{if ($0~RE) {print $1; exit}}'}
ull-gist() {gh gist edit -a $1 $(lsl-gist $2)}
catl-gist() {gh gist view $(lsl-gist $1)}
viml-gist() {gh gist edit $(lsl-gist $1)}
dll-gist() {gh gist clone $(lsl-gist $1)}
dl-gist-rev() {curl -s $(curl -s $(gh api /gists/$(lsl-gist $1)/commits | jq -r '.[].url' | sed -n ${3:-1}p) | jq -r --arg name $2 '.files|.[$name].raw_url')}
acmes() {fc -l -n -${2:-0} >> $1; ${EDITOR:-vi} $1}
acmes-shrc() {acmes $HOME/.${SHELL/*\/}rc ${1:-0}}
pip-dir() {echo $(pip show $1 | awk -v FS='[: ]' '/Loc/{print $3}')/${1//-/_}}
gh-url() {GHUC="githubusercontent"; RAW="https://raw.$GHUC.com"; echo $RAW/$1/master/$2}
gh-ls() {OPT="recursive=1"; REPO="$1"; BRANCH=$(gh api "repos/$REPO" --jq ".default_branch"); \
gh api -q ".tree[].path" "repos/$REPO/git/trees/$BRANCH?$OPT" | grep $2}
gh-dl() {REPO=$(gh-sr $1); curl -O -w %{url} $(gh-url $REPO $(gh-ls $REPO $2))}
ports () {
SERVICES=(${(f)"$(</etc/services)"}); MATCHES=(${(M)SERVICES:#*$1*})
for i in {1..$#LDAP}; do
PORTS=(${(ps:\t:)MATCHES[$i]}); PNUMS[$i]=${PORTS[2]%/*}
done; echo ${(u)PNUMS}}
epoch() {date -d "@ $1"
}
#!/usr/bin/env zsh
# ZSH configuration
# Set shell options.
export CORRECT_IGNORE="_*"
setopt RC_EXPAND_PARAM
setopt CORRECT
setopt NOTIFY
setopt MAGIC_EQUAL_SUBST
setopt NUMERIC_GLOB_SORT
setopt EXTENDED_GLOB
setopt NO_CASE_GLOB
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_MINUS
setopt AUTO_PUSHD
setopt AUTO_CD
# Set history options.
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE="$[2**16]"
export SAVEHIST="$[2**15]"
setopt INC_APPEND_HISTORY
setopt HIST_IGNORE_DUPS
setopt EXTENDED_HISTORY
setopt SHARE_HISTORY
setopt HIST_VERIFY
setopt HIST_BEEP
# Enable McFly.
export MCFLY_FUZZY=true
export MCFLY_RESULTS=20
export MCFLY_INTERFACE_VIEW=BOTTOM
export MCFLY_RESULTS_SORT=LAST_RUN
eval "$(mcfly init zsh)"
# Get better help.
export HELPDIR="/usr/share/zsh/$ZSH_VERSION/help"
unalias run-help
autoload run-help
autoload run-help-sudo
autoload run-help-git
# Set completion options.
export CASE_SENSITIVE="true"
export COMPLETION_WAITING_DOTS="true"
autoload -Uz compinit; compinit
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' # Case insensitive tab completion
zstyle ':completion:*' rehash true # automatically find new executables in path
zstyle ':completion:*' completer _expand _complete _ignored _approximate
zstyle ':completion:*' menu select
zstyle ':completion:*' select-prompt '%SScrolling active: current selection at %p%s'
zstyle ':completion:*:descriptions' format '%U%F{cyan}%d%f%u'
# Speed up completions.
zstyle ':completion:*' accept-exact '*(N)'
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.cache/zcache
# Automatically load bash completion functions.
autoload -U +X bashcompinit && bashcompinit
# Enable plugins.
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/share/zsh/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh
# Enable FuZzy Find.
source /usr/share/fzf/key-bindings.zsh
source /usr/share/fzf/completion.zsh
# Enable command-not-found support,you must have package pkgfile installed.
# https://wiki.archlinux.org/index.php/Pkgfile#.22Command_not_found.22_hook
[[ -e /usr/share/doc/pkgfile/command-not-found.zsh ]] && source /usr/share/doc/pkgfile/command-not-found.zsh
# Advanced command-not-found hook
[[ -e /usr/share/doc/find-the-command/ftc.zsh ]] && source /usr/share/doc/find-the-command/ftc.zsh
# Set keys bindings.
export WORDCHARS=""
bindkey -e # Set emacs style.
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[1;3C" vi-forward-blank-word
bindkey "^[[1;3D" vi-backward-blank-word
bindkey "^?" backward-delete-char
bindkey "^[[3~" delete-char
bindkey " " magic-space
autoload -Uz insert-composed-char
zle -N insert-composed-char
bindkey "^['" insert-composed-char
autoload -Uz backward-kill-word-match
zle -N backward-kill-space-word backward-kill-word-match
zstyle :zle:backward-kill-space-word word-style space
bindkey "^[w" backward-kill-space-word
typeset -g -A key
if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
autoload -Uz add-zle-hook-widget
function zle_application_mode_start { echoti smkx }
function zle_application_mode_stop { echoti rmkx }
add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi
# # [PageUp] - Up a line of history
# if [[ -n "${terminfo[kpp]}" ]]; then
# bindkey -M emacs "${terminfo[kpp]}" up-line-or-history
# bindkey -M viins "${terminfo[kpp]}" up-line-or-history
# bindkey -M vicmd "${terminfo[kpp]}" up-line-or-history
# fi
# # [PageDown] - Down a line of history
# if [[ -n "${terminfo[knp]}" ]]; then
# bindkey -M emacs "${terminfo[knp]}" down-line-or-history
# bindkey -M viins "${terminfo[knp]}" down-line-or-history
# bindkey -M vicmd "${terminfo[knp]}" down-line-or-history
# fi
# # Start typing + [Up-Arrow] - fuzzy find history forward
# if [[ -n "${terminfo[kcuu1]}" ]]; then
# autoload -U up-line-or-beginning-search
# zle -N up-line-or-beginning-search
#
# bindkey -M emacs "${terminfo[kcuu1]}" up-line-or-beginning-search
# bindkey -M viins "${terminfo[kcuu1]}" up-line-or-beginning-search
# bindkey -M vicmd "${terminfo[kcuu1]}" up-line-or-beginning-search
# fi
# # Start typing + [Down-Arrow] - fuzzy find history backward
# if [[ -n "${terminfo[kcud1]}" ]]; then
# autoload -U down-line-or-beginning-search
# zle -N down-line-or-beginning-search
#
# bindkey -M emacs "${terminfo[kcud1]}" down-line-or-beginning-search
# bindkey -M viins "${terminfo[kcud1]}" down-line-or-beginning-search
# bindkey -M vicmd "${terminfo[kcud1]}" down-line-or-beginning-search
# fi
# [Home] - Go to the beginning of the line.
if [[ -n "${terminfo[khome]}" ]]; then
bindkey -M emacs "${terminfo[khome]}" beginning-of-line
bindkey -M viins "${terminfo[khome]}" beginning-of-line
bindkey -M vicmd "${terminfo[khome]}" beginning-of-line
fi
# [End] - Go to the end of the line.
if [[ -n "${terminfo[kend]}" ]]; then
bindkey -M emacs "${terminfo[kend]}" end-of-line
bindkey -M viins "${terminfo[kend]}" end-of-line
bindkey -M vicmd "${terminfo[kend]}" end-of-line
fi
# [Shift-Tab] - Move backwards through the completion menu.
if [[ -n "${terminfo[kcbt]}" ]]; then
bindkey -M emacs "${terminfo[kcbt]}" reverse-menu-complete
bindkey -M viins "${terminfo[kcbt]}" reverse-menu-complete
bindkey -M vicmd "${terminfo[kcbt]}" reverse-menu-complete
fi
# # [Backspace] - Delete backward.
# bindkey -M emacs '^?' backward-delete-char
# bindkey -M viins '^?' backward-delete-char
# bindkey -M vicmd '^?' backward-delete-char
# # [Delete] - Delete forward.
# if [[ -n "${terminfo[kdch1]}" ]]; then
# bindkey -M emacs "${terminfo[kdch1]}" delete-char
# bindkey -M viins "${terminfo[kdch1]}" delete-char
# bindkey -M vicmd "${terminfo[kdch1]}" delete-char
# else
# bindkey -M emacs "^[[3~" delete-char
# bindkey -M viins "^[[3~" delete-char
# bindkey -M vicmd "^[[3~" delete-char
#
# bindkey -M emacs "^[3;5~" delete-char
# bindkey -M viins "^[3;5~" delete-char
# bindkey -M vicmd "^[3;5~" delete-char
# fi
# [Control-Left] - Go back a word.
key[Control-Left]="${terminfo[kLFT5]}"
if [[ -n "${key[Control-Left]}" ]]; then
bindkey -M emacs "${key[Control-Left]}" backward-word
bindkey -M viins "${key[Control-Left]}" backward-word
bindkey -M vicmd "${key[Control-Left]}" backward-word
fi
# [Control-Right] - Go forward a word.
key[Control-Right]="${terminfo[kRIT5]}"
if [[ -n "${key[Control-Right]}" ]]; then
bindkey -M emacs "${key[Control-Right]}" forward-word
bindkey -M viins "${key[Control-Right]}" forward-word
bindkey -M vicmd "${key[Control-Right]}" forward-word
fi
# [Alt-Left] - Go back a word.
key[Alt-Left]="${terminfo[kLFT3]}"
if [[ -n "${key[Alt-Left]}" ]]; then
bindkey -M emacs "${key[Alt-Left]}" backward-word
bindkey -M viins "${key[Alt-Left]}" backward-word
bindkey -M vicmd "${key[Alt-Left]}" backward-word
fi
# [Alt-Right] - Go forward a word.
key[Alt-Right]="${terminfo[kRIT3]}"
if [[ -n "${key[Alt-Right]}" ]]; then
bindkey -M emacs "${key[Alt-Right]}" forward-word
bindkey -M viins "${key[Alt-Right]}" forward-word
bindkey -M vicmd "${key[Alt-Right]}" forward-word
fi
# Set PATH.
if [ -d "$HOME/.local/bin" ]; then
export PATH=$HOME/.local/bin:$PATH
fi
# Set pipx directories.
export PIPX_BIN_DIR="/usr/local/bin/"
export PIPX_HOME="/usr/local/pipx/"
# Set go directories.
export GOPATH="/usr/local"
# Set default programs.
export BROWSER="chrome"
export EDITOR="vim"
export PAGER="less"
# Set command prompt.
case "$TERM" in
linux|*color*) PC=1;;
esac
if [ $PC ]; then set_prompt() {
TL_PS1="%b%F{%(#.blue.green)}┌(%B%F{%(#.red.blue)}%n%(#.:.@)%m%b%F{%(#.blue.green)})─[%B%F{reset}%~%b%F{%(#.blue.green)}]"
TR_PS1="%b%F{%(#.blue.green)}[%B%F{reset}%D{%H:%M}%b%F{%(#.blue.green)}]─(%B%F{%(#.red.blue)}%D{%d/%m}%b%F{%(#.blue.green)})"
BL_PS1="%b%F{%(#.blue.green)}└>%B%F{%(#.red.blue)}$%b%F{reset} "
BR_PS1=$'%(?.. %? %B%F{red}⨯%b%F{reset})%(1j. %j %B%F{yellow}⚙%b%F{reset}.)'
TR_PS1_L="${#${(%)TR_PS1}//$'\e'\[[0-9]##m}"
TL_PS1_L="${#${(%)TL_PS1}//$'\e'\[[0-9]##m}"
PAD_PS1="${(r:$[COLUMNS-$TL_PS1_L-$TR_PS1_L-2]::─:)}"
PROMPT="$TL_PS1─$PAD_PS1─$TR_PS1"$'\n'"$BL_PS1"
RPROMPT="${(%)BR_PS1}"
} fi
precmd_functions+=(set_prompt)
# Set misc variables.
export GROUP=$(id -gn)
local H="history"
export ZDOT="$HOME"
export SH="${SHELL##*/}"
export SHRC="$ZDOT/.${SH}rc"
export IP=$(ip -br addr | awk '$2=="UP"{print $3}')
export WIFI=$(nmcli connection | awk '/wifi/{print $1}')
# Set terminal window title.
echo -e -n "\033]0; $TERM - $SH \007"
# Enable colored commands.
if [ -x $(which dircolors) ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
fi
alias ls='ls --color'
alias jq='jq --color-output'
alias diff='diff --color -B -E -w'
alias grep='grep --color'
export LESS_TERMCAP_mb=$'\E[1;31m' # begin blink
export LESS_TERMCAP_md=$'\E[1;36m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'\E[01;33m' # begin reverse video
export LESS_TERMCAP_se=$'\E[0m' # reset reverse video
export LESS_TERMCAP_us=$'\E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'\E[0m' # reset underline
export LESS="-F -J -M -R -S -X -a -e -i -j.5 -s \
--file-size --incsearch --save-marks --mouse \
--status-line --use-color --search-options=-EFKW"
export MANPAGER="sh -c 'col -b -x | bat -l man -p'"
export MANROFFOPT=" -c"
export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=yellow,bold"
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
# Set aliases.
alias fl="ls -l"
alias fr="rm -v"
alias dm="mkdir -v"
alias dr="rmdir -v"
alias pkgs="paru -S -s"
alias pkgi="paru -S"
alias pkgu="paru -S -y -u"
alias pkgr="paru -R"
alias pkgl="paru -Q -l"
alias pkgi="paru -Q -i"
alias pkgo="paru -Q -o"
alias pkgb="paru -G -p"
alias sb="systemctl suspend"
alias rb="sudo systemctl reboot"
alias cmx="chmod -v +x"
alias hb="echo \#\!$(which env) $SH"
# Define functions.
ghg-list() {gh gist list -L 1000 | awk /$1/'{print $1; exit}'}
ghg-flist() {gh gist view $(ghg-list $1) --files}
ghg-fview() {gh gist view $(ghg-list $1) -f $2 --raw}
ghg-fedit() {gh gist edit $(ghg-list $1) -f $2 $3}
ghg-clone() {gh gist clone $(ghg-list $1)}
ghg-fpush() {ghg-fedit $1 ${2##*/}@$HOST $2}
ghg-scpush() {ghg-fpush "${(U)SH} conf" $SHRC}; alias shrc-bu="ghg-scpush"
ghg-shpush() {ghg-fpush "${(U)SH} $H" $ZDOT/.${SH}_$H}; alias hist-bu="ghg-shpush"
ghg-frpull() {curl -s $(gh api --jq ".[$3].url" "/gists/$(ghg-list $1)/commits") | jq -r ".files | .[\"$2\"].content" > "$2,$3"}
ghr-fpull() {curl -o ${3:-${2##*/}@$HOST} $(gh api repos/$(ghr-search $1)/contents/$2 --jq '.download_url')}
ghr-search() {gh search repos -L 1 $1 | awk '{print $1; exit}'}
ghr-view() {gh repo view $(ghr-search $1)}
cant() {if [ -f $2 ] && [ ! $1 $2 ]; then export DO="sudo"; fi}
view() {cant -r $1; [[ ! -f $1 ]] && OPT="-p"; $DO bat $OPT $1; unset DO OPT}
edit() {cant -w $1; $DO ${EDITOR:-vi} $1; unset DO}
help() {$1 --help |& view -lhelp}; def() {whence -f $1 | view -lsh}
ashes() {[[ $1 =~ ^[1-9][0-9]*$ ]] && $H -n -$1 >> $2; edit $2}
esc() {edit $SHRC}; she() {edit "$ZDOT/.${SH}_$H"}
mpv-yth() {VID=$(yt-dlp -v --get-id --playlist-items ${1:-1} --cookies-from-browser chrome+gnomekeyring :ythistory); mpv -v --ytdl-format=135+140 ytdl://$VID}
pip-dir() {echo $(pip show $1 | awk -v FS='[: ]' /Loc/'{print $3}')/${1//-/_}}
ports() {awk -v FS='[/ ]+' /$1/'{print $2; exit}' /etc/services}
rs-bu() {eval rsync -v -a -P $1 $2 -f\ \'-\ \*\*${*:2}\*\*\'}
epoch() {date -d "@ $1"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment