Files
.dotfiles/.bashrc
2025-11-19 10:39:51 +01:00

117 lines
2.9 KiB
Bash

#!/bin/bash
# shellcheck disable=SC1091,SC2155
#
# ~/.bashrc
# sourced upon launch of an interactive shell, which isn't a login shell has been executed
#
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
[[ -z "${PS1-}" ]] && return
# lazy loading of ssh agents
_lazy_ssh() {
if [[ -z $SSH_AUTH_SOCK ]]; then
eval "$(ssh-agent -s)" &>/dev/null
ssh-add "$HOME/.ssh/github" &>/dev/null
ssh-add "$HOME/.ssh/gitea" &>/dev/null
ssh-add "$HOME/.ssh/admin@homeserver" &>/dev/null
fi
}
# aliases so the function is called beforehand
alias ssh='_lazy_ssh; ssh'
alias sudo='_lazy_ssh; sudo'
# alias to colourise make output
_make() {
make "$@" 2> >(sed -E \
-e "s/^([Mm]akefile:[0-9]+:.*)/\x1b[33m\1\x1b[0m/" \
-e "s/^.*error.*$/\x1b[31m&\x1b[0m/I" >&2)
}
# lazily loads the git utilities, to prevent slowdowns
__lazy_git_ps1() {
if [ -z "$__GIT_PROMPT_SOURCED__" ]; then
if [ -d .git ] || git worktree list &>/dev/null; then
. /usr/share/bash-completion/completions/git &>/dev/null
. /usr/share/git/completion/git-prompt.sh &>/dev/null
export __GIT_PROMPT_SOURCED__=1
__git_ps1
fi
else __git_ps1; fi
return 0
}
# regenerates the PS1 prompt
__regenprompt() {
local err=$? # acquire the error code of the last executed command
local git=$(__lazy_git_ps1)
if [ $err -ne 0 ]; then
err="\033[s\033[$((COLUMNS - 4))G${err}\033[u"
else unset err; fi
PS1="\[\033[?25h\]" # show cursor
PS1="$PS1\[\033[01;35m\]\u@\h" # user@host
PS1="$PS1\[\033[00m\]:" # separator
PS1="$PS1\[\033[01;34m\]\w" # working directory
PS1="$PS1\[\033[01;93m\]$git" # git branch
PS1="$PS1\[\033[01;31m\]\[$err\]" # error code
PS1="$PS1\[\033[00m\]\$ " # shell sign
}
PROMPT_COMMAND=__regenprompt
# history settings
HISTSIZE=2048
HISTCONTROL=erasedups:ignoredups:ignorespace
HISTIGNORE='exit*:clear*:\:*:echo*'
#
# bash completion
#
. /home/user/.local/share/vcpkg/scripts/vcpkg_completion.bash &>/dev/null # fucking vcpkg
shopt -s checkwinsize # check the window size after each command (and if necessary, the values of LINES and COLUMNS)
shopt -s globstar # enable globstar (**/*)
#
# aliases
#
# aliases for colour
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias ls='eza -Abhg --colour=auto'
alias grep='rg'
alias diff='diff --color'
alias ip='ip -c'
# application aliases
alias ncdu='ncdu --color=dark -t 16'
alias bat='bat --wrap never --tabs 4 --theme gruvbox-dark'
alias make='_make -j'
# aliases to avoid mistakes
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -I'
alias :qa='exit'
alias qa='exit'
alias :q='exit'
alias q='exit'
alias py3='python3'
alias info='info --vi-keys'
alias batman='bat -l man'
# quality of life short-hands
alias ..='cd ..'
alias ....='cd ../..'
alias ......='cd ../../..'
alias ........='cd ../../../..'
alias lls="ls -l"
alias ll="ls -l"
# cute lil hyfetch :3
[[ $TERM == "xterm-kitty" ]] && fastfetch