Skip to content

zsh

XDG-compliant: ZDOTDIR=$HOME/.config/zsh. Plugins are git submodules.

Layout

zsh/.config/zsh/
├── .zshrc           # main rc, sourced after .zshenv
├── zsh-aliases     # alias definitions
├── host.zsh        # machine-specific (gitignored)
└── plugins/
    ├── fzf-tab/
    ├── gitstatus/
    ├── zsh-autosuggestions/
    └── zsh-syntax-highlighting/

.zshrc

# -----------------------------------------------------------------------------
# ********************************* Global ************************************
# -----------------------------------------------------------------------------

# Homebrew Initialize - adds brew and brew-installed packages to PATH
eval "$(/opt/homebrew/bin/brew shellenv)"

export XDG_CONFIG_HOME="$HOME/.config"
export ZDOTDIR="$XDG_CONFIG_HOME/zsh"

export TERMINAL_ART="$HOME/Pictures/TerminalArt/pngs"

# Default editor 
if [[ -n $SSH_CONNECTION ]]; then
  export EDITOR='vim'
else
  export EDITOR='nvim'
fi


# -----------------------------------------------------------------------------
# *********************** Machine-Specific Overlay ****************************
# -----------------------------------------------------------------------------

if [[ -f "$ZDOTDIR/host.zsh" ]]; then
  source "$ZDOTDIR/host.zsh"
fi


# -----------------------------------------------------------------------------
# ********************************* Plugins ***********************************
# -----------------------------------------------------------------------------

autoload -Uz compinit; compinit

source "$ZDOTDIR/plugins/fzf-tab/fzf-tab.plugin.zsh"
source "$ZDOTDIR/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh"
source "$ZDOTDIR/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
source "$ZDOTDIR/plugins/gitstatus/gitstatus.prompt.zsh"


# -----------------------------------------------------------------------------
# ********************************** Shell ************************************
# -----------------------------------------------------------------------------

setopt autocd # cahnge directories by simply tying the name


# -----------------------------------------------------------------------------
# ********************************* History ***********************************
# -----------------------------------------------------------------------------

setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_SAVE_NO_DUPS
setopt INC_APPEND_HISTORY

export HISTFILE="$ZDOTDIR/.zhistory"
export HISTSIZE=10000
export SAVEHIST=10000

# Sift through history matching up to current cursor position
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search

# Moves the cursor to the end of line after each match
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search


# -----------------------------------------------------------------------------
# ****************************** Key bindings *********************************
# -----------------------------------------------------------------------------

bindkey "^[[1;3A" beginning-of-line # Option + ARROW_UP
bindkey "^[[1;3B" end-of-line # Option + ARROW_DOWN
bindkey "^[[1;3C" forward-word # Option + ARROW_RIGHT - cursor forward 1 word
bindkey "^[[1;3D" backward-word # Option + ARROW_LEFT - cursor backward 1 word

bindkey "^Z" undo # CTRL+Z
bindkey "^Y" redo # CTRL+Y

bindkey "^[[A" up-line-or-beginning-search # ARROW_UP
bindkey "^[[B" down-line-or-beginning-search # ARROW_DOWN


# -----------------------------------------------------------------------------
# ********************************* Aliases ***********************************
# -----------------------------------------------------------------------------

source $ZDOTDIR/zsh-aliases


# -----------------------------------------------------------------------------
# *************************** Prompt & Fastfetch ******************************
# -----------------------------------------------------------------------------

setopt PROMPT_SUBST

PROMPT="%F{yellow}%~%f $ "
RPROMPT='$GITSTATUS_PROMPT %(0?||💀)'

# Fastfetch with random image
if [[ "$TERM" == "xterm-kitty" ]] && (( $+commands[fastfetch] )); then

  images=(
    "$TERMINAL_ART/JoaoAntunes-1.png 27 80"
    "$TERMINAL_ART/JoaoAntunes-2.png 27 50" 
    "$TERMINAL_ART/JoaoAntunes-3.png 27 50" 
  )
  random_index=$((RANDOM % ${#images[@]} + 1))
  img=(${(z)images[$random_index]})

  fastfetch --logo ${img[1]} --logo-type kitty-direct --logo-height ${img[2]} --logo-width ${img[3]}
fi


# -----------------------------------------------------------------------------
# ************************************ Misc ***********************************
# -----------------------------------------------------------------------------

# fzf
source /opt/homebrew/opt/fzf/shell/key-bindings.zsh
source /opt/homebrew/opt/fzf/shell/completion.zsh

# fzf-tab
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath'

if (( $+commands[bat] )); then
  zstyle ':fzf-tab:complete:ls:*' fzf-preview 'bat --color=always $realpath'
else
  # Shows the first 20 lines of the file as a safe fallback
  zstyle ':fzf-tab:complete:ls:*' fzf-preview 'head -n 20 $realpath'
fi

[[ -f "$HOME/.local/bin/env" ]] && . "$HOME/.local/bin/env"

Aliases

alias md="mkdir -p"
alias rd="rmdir"
alias ls="ls -aG"
alias l="ls -alG"
alias c="clear"
alias e="exit"
alias v="nvim"
alias zshrc="nvim ~/.config/zsh/.zshrc"
alias ..="cd .."
alias ...="cd ../.."

alias g='git'
alias gst='git status'
alias gd='git diff'
alias gdc='git diff --cached'
alias gl='git pull'
alias gup='git pull --rebase'
alias gp='git push'
alias gd='git diff'
alias gc='git commit -v'
alias gc!='git commit -v --amend'
alias gca='git commit -v -a'
alias gca!='git commit -v -a --amend'
alias gcmsg='git commit -m'
alias gco='git checkout'
alias gcm='git checkout master'
alias gr='git remote'
alias grv='git remote -v'
alias grmv='git remote rename'
alias grrm='git remote remove'
alias grset='git remote set-url'
alias grup='git remote update'
alias grb='git rebase'
alias grbi='git rebase -i'
alias grbc='git rebase --continue'
alias grba='git rebase --abort'
alias gb='git branch'
alias gba='git branch -a'
alias gcount='git shortlog -sn'
alias gcl='git config --list'
alias gcp='git cherry-pick'
alias glg='git log --stat --max-count=10'
alias glgg='git log --graph --max-count=10'
alias glgga='git log --graph --decorate --all'
alias glo='git log --oneline'
alias glog='git log --oneline --decorate --graph'
alias glol='git log --oneline --decorate --graph'
alias gss='git status -s'
alias ga='git add'
alias gaa='git add --all'
alias gm='git merge'
alias grh='git reset HEAD'
alias grhh='git reset HEAD --hard'
alias gclean='git reset --hard; and git clean -dfx'
alias gwc='git whatchanged -p --abbrev-commit --pretty=medium'
alias gpoat='git push origin --all; and git push origin --tags'
alias gmt='git mergetool --no-prompt'
alias gg='git gui citool'
alias gga='git gui citool --amend'
alias gk='gitk --all --branches'
alias gsts='git stash show --text'
alias gsta='git stash'
alias gstp='git stash pop'
alias gstd='git stash drop'
alias grt='cd (git rev-parse --show-toplevel; or echo ".")'
alias git-svn-dcommit-push='git svn dcommit; and git push github master:svntrunk'
alias gsr='git svn rebase'
alias gsd='git svn dcommit'
alias ggpull='git pull origin (current_branch)'
alias ggpur='git pull --rebase origin (current_branch)'
alias ggpush='git push origin (current_branch)'
alias ggpnp='git pull origin (current_branch); and git push origin (current_branch)'
alias gwip='git add -A; git ls-files --deleted -z | xargs -0 git rm; git commit -m "wip"'
alias gunwip='git log -n 1 | grep -q -c wip; and git reset HEAD~1'
alias gsw='git switch'
alias grs='git restore'

.zshenv (loaded first, lives in $HOME)

# Intentional - Must be in $HOME
# Entry point for zsh
# Ref: https://thevaluable.dev/zsh-install-configure-mouseless/

export ZDOTDIR="$HOME/.config/zsh"

Updating plugins

git submodule update --remote --merge