dotfiles/.bashrc
2025-08-21 22:43:09 +10:00

194 lines
4.2 KiB
Bash

#!/usr/bin/env bash
### DEFAULTS
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
[ -f ~/.config/bash/git-prompt.sh ] && . ~/.config/bash/git-prompt.sh
[ -f ~/.config/bash/private.sh ] && . ~/.config/bash/private.sh
### NVM
[ -d "$HOME/.config/nvm" ] \
&& export NVM_DIR="$HOME/.config/nvm" \
|| export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
### FZF
export FZF_DEFAULT_COMMAND='ag --skip-vcs-ignores --ignore .git --ignore node_modules -g ""'
[ -f /usr/share/doc/fzf/examples/key-bindings.bash ] \
&& source /usr/share/doc/fzf/examples/key-bindings.bash
do_cd_fzf() {
local parent="$1"
cd "${parent}/$(find "${parent}" \
-maxdepth 1 \
-mindepth 1 \
-type d | \
xargs -I '{}' basename '{}' | \
fzf)"
}
do_edit_fzf() {
local dir="$1"
pushd "$dir" > /dev/null
local target="$(fzf)"
if [ "${target}" != "" ]; then
vim "${target}"
fi
popd > /dev/null
}
fcd() {
local file
local dir
file=$(fzf +m -q "$1") && dir=$(dirname "$file") && cd "$dir"
}
do_checkout() {
git for-each-ref --format='%(refname:short)' \
$([ "$1" == "--remote" ] \
&& printf '%s' 'refs/remotes' \
|| printf '%s' 'refs/heads' ) \
| fzf \
| xargs -r git $([ "$1" == "--remote" ] \
&& printf '%s' 'checkout --track' \
|| printf '%s' 'checkout')
}
### GO
export GOPATH=~/.local/go
export PATH="$GOPATH/bin:$PATH"
### Helpers
function uber_download {
lftp -e "pget -c -n128 \"$1\"; quit"
}
function do_sdcv {
sdcv -c -0 "$1" | less -r
}
function do_ppwd {
pwd | xclip -selection clipboard
}
### ALIASES
alias d=docker
alias open=xdg-open
alias vim=nvim
alias ggg=gitui
alias gst="git status"
alias gco="do_checkout"
alias gcm="do_checkout --remote"
alias src="do_cd_fzf ${HOME}/src"
### HISTORY
# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
# UNLIMITED ~~POWER~~ HISTORY
HISTSIZE=-1
HISTFILESIZE=-1
### Customizations
shopt -s checkwinsize
# Completions
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
function prompt_text {
if [ "$EUID" -eq 0 ]; then
printf '[***ROOT***] # '
else
printf '$ '
fi
}
function prompt_color {
if [ "$EUID" -eq 0 ]; then
printf '\e[31m'
else
printf '\e[38;5;24m'
fi
}
function shorten_path {
local current_path=`pwd`
local last=`echo $current_path | sed -e 's/[^\/]*\///g'`
local path=""
[[ $current_path == "$HOME"* ]] && read -ra path <<< "~"
current_path=${current_path/"$HOME"/''}
current_path=${current_path:1}
IFS="/" read -ra current_path <<< "$current_path"
for x in "${current_path[@]}"
do
if [ "$x" == "$last" ];
then
path="${path}/${x}"
else
path="${path}/${x:0:1}"
fi
done
echo $path
}
# terminal titles
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# some colors
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
export EDITOR=nvim
export PATH="~/.local/bin:$PATH"
export PS1='\[$(prompt_color)\][$(shorten_path)]\[\e[31m\]$(__git_ps1)\n\[$(prompt_color)\]$(prompt_text)\[\033[m\]'
export LS_COLORS='di=1;34:ln=1;31:so=37:pi=1;93:ex=35:bd=37:cd=37:su=37:sg=37:tw=32:ow=32'
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'