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

9.5 KiB

.bachrc

# If not running interactively, don't do anything
case $- in
   *i*) ;;
     *) return;;
esac

# Load fastfetch
fastfetch

# Check if starship is installed
if ! command -v starship &> /dev/null
then
    echo "Starship not found, installing..."
    # Install starship (using the recommended installation script)
    curl -sS https://starship.rs/install.sh | sh
    # You can also specify an installation directory, e.g., if you don't have sudo access:
    # curl -sS https://starship.rs/install.sh | sh -s -- -b ~/.local/bin
    echo "Starship installed."
fi

# Load starship prompt if starship is installed
if [ -x /usr/bin/starship ]; then
        __main() {
                local major="${BASH_VERSINFO[0]}"
                local minor="${BASH_VERSINFO[1]}"

                if ((major > 4)) || { ((major == 4)) && ((minor >= 1)); }; then
                        source <("/usr/bin/starship" init bash --print-full-init)
                else
                        source /dev/stdin <<<"$("/usr/bin/starship" init bash --print-full-init)"
                fi
        }
        __main
        unset -f __main
fi

# don't put duplicate lines or lines starting with space in the history.
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend
ISTSIZE=20000
HISTFILESIZE=10000

# check the window size after each command and, if necessary,
shopt -s checkwinsize

# Combine multiline commands into one in history
shopt -s cmdhist

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"


# get current branch in git repo
function parse_git_branch() {
        BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
        if [ ! "${BRANCH}" == "" ]
        then
                STAT=`parse_git_dirty`
                echo "[${BRANCH}${STAT}]"
        else
                echo ""
        fi
}

# Load Aliases and Finctions, instead of adding them here directly.
if [ -e $HOME/.bash_aliases ]; then
   source $HOME/.bash_aliases
fi

if [ -e $HOME/.bash_functions ]; then
   source $HOME/.bash_functions
fi


# enable programmable completion features (you don't need to enable this, if it's already enabled in /etc/bash.bashrc >
if ! shopt -oq posix; then
 if [ -f /usr/share/bash-completion/bash_completion ]; then
   . /usr/share/bash-completion/bash_completion
    elif [ -f /etc/bash_completion ]; then
   . /etc/bash_completion
 fi
fi

# Make Nano the default Editor
if [[ -n $SSH_CONNECTION ]]; then
  export EDITOR='nano'
else
  export EDITOR='nano'
fi

### FZF ###
# Enables the following keybindings:
# CTRL-t = fzf select
# CTRL-r = fzf history
# ALT-c  = fzf cd
eval "$(fzf --bash)"

# Added by LM Studio CLI (lms)
export PATH="$PATH:/home/miker/.lmstudio/bin"
# End of LM Studio CLI section

# Automatically preform ls when changing directory,
cd() {
    builtin cd "$@" && ls -lA
}


# Initialize starship
eval "$(starship init bash)"eval "$(zoxide init bash)"
. "$HOME/.cargo/env"

.bash_aliases

# ~/.bashrc: executed by bash(1) for non-login shells.

#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'
lias 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"

.bash_functions

# ~/.bashrc

function 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_u>
  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\]# \[\0>
  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\]$ \[\0>
  fi
}
xtract() {
    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
    }

# Get colors in manual pages
man() {
       env \
                LESS_TERMCAP_mb="$(printf '\e[1;31m')" \
                LESS_TERMCAP_md="$(printf '\e[1;31m')" \
                LESS_TERMCAP_me="$(printf '\e[0m')" \
                LESS_TERMCAP_se="$(printf '\e[0m')" \
                LESS_TERMCAP_so="$(printf '\e[1;44;33m')" \
                LESS_TERMCAP_ue="$(printf '\e[0m')" \
                LESS_TERMCAP_us="$(printf '\e[1;32m')" \
                man "$@"
}