Files
Compose-Files/Backups/Miker/.trash/zshrc - Zsh aliases and functions.md
T
2026-07-20 09:23:17 -04:00

11 KiB

.zshrc

# If you're using fastfetch with Powerlevel10k's instant prompt feature, it's a known source of incompatibility. fastfetch command in your .zshrc file is placed before the initialization of p10k-instant-prompt
fastfetch

############################# CHECK IF INSTALLED, IF NOT THEN INSTALL ###########################
if [ ! -f "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k/powerlevel10k.zsh-theme" ]; then
  echo "Powerlevel10k not found. Installing..."
  git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
  echo "Powerlevel10k installed."
fi

# Check if Starship is installed, if not, install it
if ! command -v starship &> /dev/null; then
  echo "Starship not found, installing..."
  sudo apt update
  sudo apt install starship -y
fi

# Check if Oh My Zsh is installed
if [ ! -d "$HOME/.oh-my-zsh" ]; then
  # Oh My Zsh is not installed, proceed with installation
  echo "Oh My Zsh not found. Installing..."
  sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
else
  # Oh My Zsh is installed, source it
  source $HOME/.oh-my-zsh/oh-my-zsh.sh
fi

########################## EXPORT PATHS #########################################################
export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH
export ZSH="$HOME/.oh-my-zsh"
# You may need to manually set your language environment
export LANG=en_US.UTF-8
# Added by LM Studio CLI (lms)
export PATH="$PATH:/home/miker/.lmstudio/bin"

######################### HISTORY CONTROLS #######################################################
HISTCONTROL=ignoreboth
HISTSIZE=100000
HISTFILESIZE=100000
HISTFILE=~/.zsh_history
setopt INC_APPEND_HISTORY
setopt hist_ignore_all_dups # remove older duplicate entries from history
setopt hist_reduce_blanks # remove superfluous blanks from history items
setopt inc_append_history # save history entries as soon as they are entered
setopt share_history # share history between different instances of the shell
setopt auto_cd # cd by typing directory name if it's not a command
setopt correct_all # autocorrect commands
setopt auto_list # automatically list choices on ambiguous completion
setopt auto_menu # automatically use menu completion
setopt always_to_end # move cursor to end if word had one match

########################### POWERLEVEL & ZSH & OH-MY-ZSH SETTINGS ################################
# 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

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# Set the zsh theme to powerlevel10k
ZSH_THEME="powerlevel10k/powerlevel10k"

# update automatically without asking
zstyle ':omz:update' mode auto

# Uncomment the following line to change how often to auto-update (in days).
zstyle ':omz:update' frequency 13

# Uncomment the following line to enable command auto-correction.
ENABLE_CORRECTION="true"

# Add wisely, as too many plugins slow down shell startup.
plugins=(
  git
  zsh-autosuggestions
  zsh-bat
  ansible
  colored-man-pages
  terraform
  systemadmin
  aliases
  alias-finder
  zoxide
  ubuntu
  ssh
  kate
  fzf
  zsh-syntax-highlighting
)

# Cache completions aggressively
autoload -Uz compinit
if [ "$(date +'%j')" != "$(stat -f '%Sm' -t '%j' ~/.zcompdump 2>/dev/null)" ]; then
    compinit
else
    compinit -C
fi

# Load oh my zsh
source $ZSH/oh-my-zsh.sh
source ~/powerlevel10k/powerlevel10k.zsh-theme

################################ SETTING EDITOR ###################################################
if [[ -n $SSH_CONNECTION ]]; then
  export EDITOR='nano'
else
  export EDITOR='nano'
fi

################################## GITHUB EXPORT ###################################################
export GITHUB_USERNAME="mmcferidge1969"
export GITHUB_EMAIL="mmcfetridg@aol.com"

################################## CD INTO DIR AND LIST ############################################
cd() {
    builtin cd "$@" && ls -lA
}

################################# GIT GLOBAL SETTINGS ################################################
git config --global user.name "mmcfetridge1969"
git config --global user.email "mmcfetridg@aol.com"

# Git Branch Function
parse_git_branch() {
  local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  if [[ -n "$branch" ]]; then
    local dirty=$(git status --porcelain 2>/dev/null | grep -q '^[A-Z]' && echo "*" || true)
    echo "[$branch$dirty]"
  else
    echo ""
  fi
}

########################## SOURCE FILES ##########################################################
source ~/.zsh_aliases
source ~/.zsh_functions

################################# INITIALIZE APPS ###################################################
eval "$(starship init zsh)"
# Set up fzf key bindings and fuzzy completion
source <(fzf --zsh)

.zsh_aliases

#EZA
alias ls='eza --all --long --group --group-directories-first --icons --header --time-style long-iso'
alias l='eza -lahF --color=auto --icons --sort=size --group-directories-first'
alias lss='eza -hF --color=auto --icons --sort=size --group-directories-first'
alias la='eza -ahF --color=auto --icons --sort=size --group-directories-first'
alias lst='eza -lahT --color=auto --icons --sort=size --group-directories-first'
alias lt='eza -aT --icons --group-directories-first --color=auto --sort=size'

# FZF
alias finddoc='fzf --style full --preview="bat --color=always {}"'

# Clear the screen
alias c='clear'

## a quick way to get out of current directory ##
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'

## Colorize the grep command output for ease of use (good for log files)##
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'

#Grabs the disk usage in the current directory
alias used='du -sch .[!.]* * 2>/dev/null | sort -h'

#Gets the total disk usage on your machine
alias totalusage='df -hl --total | grep total'

#Shows the individual partition usages without the temporary memory values
alias partusage='df -hlT --exclude-type=tmpfs --exclude-type=devtmpfs'

#Gives you what is using the most space. Both directories and files. Varies on current directory
alias most='du -hsx * | sort -rh | head -10'

# View only mounted Drives
alias mnt="mount | awk -F' ' '{ printf \"%s\t%s\n\",\$1,\$3; }' | column -t | egrep ^/dev/ | sort"

# handy short cuts #
alias h='history'
alias j='jobs -l'
alias path='echo -e ${PATH//:/\\n}'

# Set nano as default
alias n=nano
alias sn='sudo nano'
alias edit=nano

# Show open ports - Use netstat command to quickly list all TCP/UDP port on the server:
alias ports='netstat -tulanp'
# reboot / halt / poweroff
alias reboot='sudo /sbin/reboot'
alias poweroff='sudo /sbin/poweroff'
alias halt='sudo /sbin/halt'
alias shutdown='sudo /sbin/shutdown'

## set some other defaults ##
alias df='df -H'
alias du='du -ch'

# Edit the bashrc and re-source it when saved and closed.
alias bashrc="nano ~/.bashrc && source ~/.bashrc"

# ps
alias psa="ps auxf"
alias psgrep="ps aux | grep -v grep | grep -i -e VSZ -e"
alias psmem='ps auxf | sort -nr -k 4'
alias pscpu='ps auxf | sort -nr -k 3'

# git
alias addup='git add -u'
alias addall='git add .'
alias branch='git branch'
alias checkout='git checkout'
alias clone='git clone'
alias commit='git commit -m'
alias fetch='git fetch'
alias pull='git pull origin'
alias push='git push origin'
alias stat='git status'  # 'status' is protected name so using 'stat' instead
alias tag='git tag'
alias newtag='git tag -a'

# get error messages from journalctl
alias jctl="journalctl -p 3 -xb"

zsh_functions

unction git_branch() {
  git_branch=$(git branch --no-color 2>/dev/null | grep \* | sed 's/* //')

  if [ $git_branch ]
  then
    white="\001\033[0;37m\002"
    yellow="\001\033[0;33m\002"
    blue="\001\033[0;34m\002"
    red="\001\033[0;31m\002"

    git_status=$(git status --porcelain 2>/dev/null)
    git_count_t=$(for i in "$git_status"; do echo "$i"; done | grep -v '^?? ' | sed '/^$/d' | wc -l | sed "s/ //g")
    git_count_color_t=$(if [ $git_count_t = "0" ]; then echo -e "$blue"; else echo -e "$red"; fi)
    git_count_ut=$(for i in "$git_status"; do echo "$i"; done | grep '^?? ' | sed '/^$/d' | wc -l | sed "s/ //g")
    git_count_color_ut=$(if [ $git_count_ut = "0" ]; then echo -e "$blue"; else echo -e "$red"; fi)

    echo -e "$white:$yellow${git_branch}$white:$git_count_color_t${git_count_t}$white:$git_count_color_ut${git_count_ut}"
  fi
}

function pushall() {
    sudo git add .
    sudo git commit --all
    sudo git push --all
}

short_pwd() {
  cwd=$(pwd | sed "s#${HOME}#~#g" | perl -F/ -ane 'print join( "/", map { $i++ < @F - 1 ?  substr $_,0,1 : $_ } @F)')
  echo -n $cwd
}

set_bash_prompt() {
  if [ "$UID" = 0 ]; then
    #root
    PS1="\[\033[0;31m\]\u\[\033[0;37m\]@\h\[\033[1;37m\] \[\033[0;31m\]\$(short_pwd)\$(git_branch)\[\033[0;36m\]# \[\033[0m\]"
  else
    #non-root
    PS1="\[\033[0;32m\]\u\[\033[0;37m\]@\h\[\033[1;37m\] \[\033[0;32m\]\$(short_pwd)\$(git_branch)\[\033[0;36m\]$ \[\033[0m\]"
  fi
}
# Extracts pretty much anything you throw at it granted you have the program for it.
extract() {
    if [ -z ${1} ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        echo "Usage: extract <archive> [directory]"
        echo "Example: extract presentation.zip."
        echo "Valid archive types are:"
        echo "tar.bz2, tar.gz, tar.xz, tar, bz2, gz, tbz2,"
        echo "tbz, tgz, lzo, rar, zip, 7z, xz, txz, lzma and tlz"
    else
        case "$1" in
            *.tar.bz2|*.tbz2|*.tbz)         tar xvjf "$1" ;;
            *.tgz)                          tar zxvf "$1" ;;
            *.tar.gz)                       tar xvzf "$1" ;;
            *.tar.xz)                       tar xvJf "$1" ;;
            *.tar)                          tar xvf "$1" ;;
            *.rar)                          7z x "$1" ;;
            *.zip)                          unzip "$1" ;;
            *.7z)                           7z x "$1" ;;
            *.lzo)                          lzop -d  "$1" ;;
            *.gz)                           gunzip "$1" ;;
            *.bz2)                          bunzip2 "$1" ;;
            *.Z)                            uncompress "$1" ;;
            *.xz|*.txz|*.lzma|*.tlz)        xz -d "$1" ;;
            *) echo "Sorry, '$1' could not be decompressed." ;;
        esac
    fi
}

# Goes up many dirs as the number passed as argument, if none goes up by 1 by default
up(){
  local d=""
  limit=$1
  for ((i=1 ; i <= limit ; i++))
    do
      d=$d/..
    done
  d=$(echo $d | sed 's/^\///')
  if [ -z "$d" ]; then
    d=..
  fi
  cd $d
}

# Creates a directory and immediately changes into it.
    mkcd() {
        mkdir -p "$1" && cd "$1"
    }

# Displays your external IP address
    myip() {
        curl -s ifconfig.me
    }