2024-01-07 17:31:47 +05:30
|
|
|
#!/usr/bin/env bash
|
2023-08-31 14:07:13 +05:30
|
|
|
# shellcheck disable=SC2139
|
|
|
|
|
2024-01-08 11:13:51 +05:30
|
|
|
# only run in an interactive shell
|
|
|
|
[[ $- == *i* ]] || return
|
|
|
|
|
2023-08-31 15:29:01 +05:30
|
|
|
unalias -a
|
2024-01-07 17:31:47 +05:30
|
|
|
|
2024-01-08 16:26:14 +05:30
|
|
|
shopt -s checkjobs
|
|
|
|
shopt -s checkwinsize
|
|
|
|
shopt -s extglob
|
|
|
|
shopt -s globstar
|
2024-01-08 15:54:34 +05:30
|
|
|
shopt -s histappend
|
|
|
|
HISTCONTROL='ignorespace:ignoredups:erasedups'
|
|
|
|
HISTFILESIZE=100000
|
2024-01-09 17:18:18 +05:30
|
|
|
HISTIGNORE="clear:history*:exit:date:* --help:* -help:* -h:whoami:ls:lah:lo"
|
2024-01-08 15:54:34 +05:30
|
|
|
HISTSIZE=10000
|
2024-03-19 17:01:45 +05:30
|
|
|
PS0_HORIZONTAL_RULE='++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
|
2024-02-19 14:49:15 +05:30
|
|
|
FILES_TO_SOURCE=()
|
2024-01-08 15:54:34 +05:30
|
|
|
|
2024-01-07 17:31:47 +05:30
|
|
|
function path_add() {
|
|
|
|
if [[ -d "$1" ]] && [[ ":$PATH:" != *:$1:* ]]; then
|
|
|
|
PATH="$PATH:$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ "$(uname -s)" == 'Linux' ]]; then
|
2023-08-31 14:07:13 +05:30
|
|
|
GNU_LS="$(command -v ls)"
|
|
|
|
GNU_GREP="$(command -v grep)"
|
|
|
|
|
2024-01-07 17:31:47 +05:30
|
|
|
alias ping='ping -W 0.1 -O'
|
|
|
|
alias mpv='mpv --geometry=60% --vo=gpu --hwdec=vaapi'
|
|
|
|
alias mpvrpi='mpv --geometry=60% --vo=x11'
|
2023-08-31 14:07:13 +05:30
|
|
|
|
|
|
|
if [[ "${XDG_SESSION_TYPE}" == 'x11' ]]; then
|
2024-01-07 17:31:47 +05:30
|
|
|
alias clearclipboard='xsel -bc'
|
|
|
|
alias pbcopy='xsel --clipboard --input'
|
2023-08-31 14:07:13 +05:30
|
|
|
elif [[ "${XDG_SESSION_TYPE}" == 'wayland' ]]; then
|
2024-01-07 17:31:47 +05:30
|
|
|
alias clearclipboard='wl-copy --clear'
|
|
|
|
alias pbcopy='wl-copy'
|
2023-08-31 14:07:13 +05:30
|
|
|
fi
|
|
|
|
|
2024-02-19 09:40:30 +05:30
|
|
|
|
2024-02-10 18:34:44 +05:30
|
|
|
# source the vars only if we are not on NixOS
|
2024-03-19 16:03:09 +05:30
|
|
|
if grep 'nixos' /etc/os-release > /dev/null; then
|
|
|
|
EDITOR=nvim
|
|
|
|
|
|
|
|
else
|
|
|
|
EDITOR=vim
|
2024-02-19 14:49:15 +05:30
|
|
|
FILES_TO_SOURCE+=("${HOME}/.nix-profile/etc/profile.d/hm-session-vars.sh")
|
2024-03-19 16:03:09 +05:30
|
|
|
|
2024-04-18 16:44:43 +05:30
|
|
|
if command -v nix > /dev/null; then
|
|
|
|
alias sudo='sudo --preserve-env=PATH env'
|
|
|
|
fi
|
|
|
|
|
2024-03-19 16:03:09 +05:30
|
|
|
if grep 'debian' /etc/os-release > /dev/null; then
|
|
|
|
export NEEDRESTART_MODE='a'
|
|
|
|
export DEBIAN_FRONTEND='noninteractive'
|
|
|
|
export APT_LISTCHANGES_FRONTEND='none'
|
2024-03-19 16:31:30 +05:30
|
|
|
|
|
|
|
if grep 'ID=debian' /etc/os-release > /dev/null; then
|
2024-03-27 13:13:37 +05:30
|
|
|
LANG="$(grep -v '^# ' /etc/locale.gen | grep -v '^$' | head -n 1 | sed -e 's/ /./')"
|
2024-03-19 16:31:30 +05:30
|
|
|
export LANG
|
|
|
|
fi
|
2024-03-19 16:03:09 +05:30
|
|
|
fi
|
2024-02-10 18:34:44 +05:30
|
|
|
fi
|
|
|
|
|
2024-01-07 17:31:47 +05:30
|
|
|
elif [[ "$(uname -s)" == 'Darwin' ]]; then
|
2023-08-31 14:07:13 +05:30
|
|
|
GNU_LS="$(command -v gls)"
|
|
|
|
GNU_GREP="$(command -v ggrep)"
|
2024-02-19 14:49:15 +05:30
|
|
|
FILES_TO_SOURCE+=("${HOME}/.nix-profile/etc/profile.d/hm-session-vars.sh")
|
|
|
|
FILES_TO_SOURCE+=("${HOME}/.local/share/nix-bash/bash_completion.sh")
|
2023-08-31 14:07:13 +05:30
|
|
|
|
2024-03-19 16:03:09 +05:30
|
|
|
EDITOR=vim
|
2024-01-07 17:31:47 +05:30
|
|
|
alias ktb='sudo pkill TouchBarServer; sudo killall ControlStrip'
|
|
|
|
alias mpv='mpv --vo=libmpv'
|
2024-01-26 16:22:00 +05:30
|
|
|
alias ownefivars="chmod +uw ${HOME}/Library/Containers/com.utmapp.UTM/Data/Documents/*.utm/Data/efi_vars.fd"
|
2024-04-18 16:44:43 +05:30
|
|
|
alias sudo='sudo --preserve-env=PATH env'
|
2023-08-31 14:07:13 +05:30
|
|
|
|
2024-01-07 17:31:47 +05:30
|
|
|
path_add '/usr/local/bin'
|
2023-08-31 14:07:13 +05:30
|
|
|
fi
|
2024-03-19 16:03:09 +05:30
|
|
|
export EDITOR
|
2023-08-31 14:07:13 +05:30
|
|
|
|
2024-02-13 18:36:14 +05:30
|
|
|
mkdir -p "${HOME}/.cargo/bin" && path_add "${HOME}/.cargo/bin"
|
|
|
|
mkdir -p "${HOME}/.local/bin" && path_add "${HOME}/.local/bin"
|
2024-02-07 20:37:26 +05:30
|
|
|
path_add '/sbin'
|
2024-01-07 17:31:47 +05:30
|
|
|
export PATH
|
2023-12-28 11:21:06 +05:30
|
|
|
|
2024-02-22 18:32:53 +05:30
|
|
|
if command -v rustc > /dev/null; then
|
2024-03-19 16:05:10 +05:30
|
|
|
# this actually needs to be a split for loop's iteration
|
|
|
|
# shellcheck disable=SC2207
|
2024-02-22 18:32:53 +05:30
|
|
|
rust_toolchains=($(rustup toolchain list | awk '{ print $1 }'))
|
|
|
|
PYTHONPATH=''
|
|
|
|
|
|
|
|
for toolchain in "${rust_toolchains[@]}"; do
|
|
|
|
toolchain_path="${HOME}/.rustup/toolchains/${toolchain}/lib/rustlib/etc"
|
|
|
|
if [[ -d "${toolchain_path}" ]] && [[ ":$PYTHONPATH:" != *:${toolchain_path}:* ]]; then
|
|
|
|
if [[ -z "${PYTHONPATH}" ]]; then
|
|
|
|
PYTHONPATH="${toolchain_path}"
|
|
|
|
else
|
|
|
|
PYTHONPATH="$PYTHONPATH:${toolchain_path}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
export PYTHONPATH
|
|
|
|
fi
|
|
|
|
|
2024-01-26 15:34:24 +05:30
|
|
|
function list_open_ports() {
|
|
|
|
if [[ -n "$1" ]]; then
|
|
|
|
nc -z -v "$1" 1-65535 2>&1 | grep -v 'Connection refused'
|
|
|
|
else
|
|
|
|
echo 'Please provide an [IP] address.'
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:08:10 +05:30
|
|
|
function tty_serial() {
|
|
|
|
if [[ -n "$1" ]]; then
|
|
|
|
if [[ -n "$2" ]]; then
|
|
|
|
picocom --quiet --baud "$2" "/dev/ttyUSB$1"
|
|
|
|
else
|
|
|
|
picocom --quiet --baud 115200 "/dev/ttyUSB$1"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
picocom --quiet --baud 115200 /dev/ttyUSB0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-02-19 14:49:15 +05:30
|
|
|
for fts in "${FILES_TO_SOURCE[@]}"; do
|
|
|
|
if [[ -f "${fts}" ]]; then
|
|
|
|
# shellcheck disable=SC1090
|
|
|
|
source "${fts}"
|
2024-03-19 16:31:51 +05:30
|
|
|
else
|
|
|
|
echo "WARNING: '${fts}' does not exist."
|
2024-02-19 14:49:15 +05:30
|
|
|
fi
|
|
|
|
done
|
2024-02-19 09:40:30 +05:30
|
|
|
|
2024-01-07 17:31:47 +05:30
|
|
|
# alias wrappers to call scripts
|
2024-02-26 21:28:40 +05:30
|
|
|
COMMON_SCRIPTS_DIR="${HOME}/.local/scripts"
|
|
|
|
SCRIPTS_DIR="${COMMON_SCRIPTS_DIR}/other-common-scripts"
|
|
|
|
EL_SCRIPTS_DIR="${COMMON_SCRIPTS_DIR}/el"
|
2024-01-07 17:31:47 +05:30
|
|
|
alias debextract="${SCRIPTS_DIR}/extract-deb-pkg.sh"
|
2024-02-26 21:28:40 +05:30
|
|
|
alias installrpmdeps="${EL_SCRIPTS_DIR}/install-rpm-package-build-deps.sh"
|
|
|
|
alias mockbuild="${EL_SCRIPTS_DIR}/mock-build.sh"
|
2024-01-07 17:31:47 +05:30
|
|
|
alias pysort="${SCRIPTS_DIR}/sort.py"
|
|
|
|
alias rpmextract="${SCRIPTS_DIR}/extract-rpm-pkg.sh"
|
|
|
|
alias showdiskusage="${SCRIPTS_DIR}/show-disk-usage.sh"
|
2024-02-26 21:28:40 +05:30
|
|
|
alias startvm="${SCRIPTS_DIR}/start-qemu-vm.sh"
|
2024-03-30 07:50:29 +05:30
|
|
|
alias sudosync="${SCRIPTS_DIR}/paranoid-flush.sh"
|
2024-01-07 17:31:47 +05:30
|
|
|
alias suslock="${HOME}/.local/scripts/window-manager/lock-and-suspend.sh"
|
|
|
|
alias syncsync="${SCRIPTS_DIR}/paranoid-flush.sh"
|
2023-10-19 15:06:23 +05:30
|
|
|
|
2024-01-07 17:31:47 +05:30
|
|
|
# actual aliases (generic ones)
|
|
|
|
RSYNC_OPTIONS='--verbose --recursive --size-only --human-readable --progress --stats --itemize-changes'
|
|
|
|
alias bottom='btm'
|
|
|
|
alias clear="clear && printf '\e[3J'"
|
2023-08-31 14:07:13 +05:30
|
|
|
alias dotfiles="git --git-dir=${HOME}/.dotfiles --work-tree=${HOME}"
|
2024-01-07 17:31:47 +05:30
|
|
|
alias download='aria2c --max-connection-per-server=16 --min-split-size=1M --file-allocation=none --continue=false --seed-time=0'
|
|
|
|
alias drivetemp='hdparm -CH'
|
2024-04-16 09:06:53 +05:30
|
|
|
alias mksshkey='ssh-keygen -t ed25519 -f'
|
2024-01-07 17:31:47 +05:30
|
|
|
alias mtr='mtr --show-ips --displaymode 0 -o "LDR AGJMXI"'
|
|
|
|
alias nixcheckconf="rsync --fsync ${RSYNC_OPTIONS} --dry-run --checksum ${HOME}/my-git-repos/pratham/prathams-nixos/nixos-configuration/ /etc/nixos/"
|
|
|
|
alias prettynixbuild='nix build --log-format internal-json -v . 2>&1 | nom --json'
|
2024-01-27 19:08:10 +05:30
|
|
|
alias serialterm="tty_serial"
|
2024-01-12 13:03:10 +05:30
|
|
|
alias sudo='sudo '
|
2024-01-07 17:31:47 +05:30
|
|
|
alias unxz='unxz --keep' # override 'unxz' with this to always keep the archive
|
2023-09-21 21:33:04 +05:30
|
|
|
alias update="source ${HOME}/.bashrc"
|
2024-03-19 17:32:53 +05:30
|
|
|
|
|
|
|
|
|
|
|
# yes, the order is | e -> vvim -> vim |
|
|
|
|
alias e="$(command -v nvim)"
|
2024-03-19 16:03:09 +05:30
|
|
|
alias vvim="$(command -v vim)"
|
2024-03-19 17:32:53 +05:30
|
|
|
alias vim="$(command -v nvim)"
|
2024-01-07 17:31:47 +05:30
|
|
|
|
2024-01-12 18:28:16 +05:30
|
|
|
# git
|
|
|
|
alias gadd='git add'
|
|
|
|
alias gdiff='git --no-pager diff'
|
|
|
|
alias gsdiff='git --no-pager diff --staged'
|
2024-01-13 19:18:24 +05:30
|
|
|
alias g0diff='git --no-pager diff --unified=0'
|
2024-04-16 07:32:58 +05:30
|
|
|
alias gclean='git clean -x -d'
|
2024-01-12 18:28:16 +05:30
|
|
|
alias gstat='git status'
|
|
|
|
alias gwt='git worktree'
|
2024-02-16 17:52:16 +05:30
|
|
|
alias gblame='git blame -w -C -C -C'
|
2024-01-12 18:45:50 +05:30
|
|
|
# git, but dotfiles
|
|
|
|
alias dotfiles="git --git-dir=${HOME}/.dotfiles --work-tree=${HOME}"
|
2024-02-13 21:40:53 +05:30
|
|
|
alias dotadd="git --git-dir=${HOME}/.dotfiles --work-tree=${HOME} add -f"
|
2024-01-12 18:45:50 +05:30
|
|
|
alias dotdiff="git --git-dir=${HOME}/.dotfiles --work-tree=${HOME} --no-pager diff"
|
|
|
|
alias dotsdiff="git --git-dir=${HOME}/.dotfiles --work-tree=${HOME} --no-pager diff --staged"
|
2024-01-13 19:18:24 +05:30
|
|
|
alias dot0diff="git --git-dir=${HOME}/.dotfiles --work-tree=${HOME} --no-pager diff --unified=0"
|
2024-01-12 18:45:50 +05:30
|
|
|
alias dotstat="git --git-dir=${HOME}/.dotfiles --work-tree=${HOME} status"
|
2024-01-12 18:28:16 +05:30
|
|
|
|
2024-01-07 17:31:47 +05:30
|
|
|
# rsync
|
|
|
|
alias custcp="rsync ${RSYNC_OPTIONS}"
|
|
|
|
alias fcustcp="rsync --fsync ${RSYNC_OPTIONS}"
|
|
|
|
|
|
|
|
# yt-dlp related aliases
|
2023-08-31 14:07:13 +05:30
|
|
|
alias playdl="yt-dlp --config-location ${HOME}/.config/yt-dlp/plst_config --external-downloader aria2c"
|
2024-01-07 17:31:47 +05:30
|
|
|
alias ytdown="yt-dlp --config-location ${HOME}/.config/yt-dlp/norm_config --external-downloader aria2c"
|
2023-08-31 14:07:13 +05:30
|
|
|
alias ytslow="yt-dlp --config-location ${HOME}/.config/yt-dlp/norm_config --no-part --concurrent-fragments 1 --limit-rate 4M"
|
2024-01-07 17:31:47 +05:30
|
|
|
|
|
|
|
# ripgrep
|
|
|
|
alias rgi='rg --hidden --ignore-case'
|
|
|
|
alias rgv='rg --hidden --invert-match'
|
2024-01-09 22:51:09 +05:30
|
|
|
alias rgiv='rg --hidden --invert-match --ignore-case'
|
|
|
|
alias rgvi='rg --hidden --invert-match --ignore-case'
|
2024-01-07 17:31:47 +05:30
|
|
|
|
|
|
|
# these only exist because I need to use 'g{ls,grep}' on macOS
|
|
|
|
alias grep="${GNU_GREP} --color=auto"
|
|
|
|
alias grepi="${GNU_GREP} --color=auto --ignore-case"
|
|
|
|
alias grepv="${GNU_GREP} --color=auto --invert-match"
|
|
|
|
alias grepiv="${GNU_GREP} --color=auto --invert-match --ignore-case"
|
|
|
|
alias grepvi="${GNU_GREP} --color=auto --invert-match --ignore-case"
|
2023-08-31 14:07:13 +05:30
|
|
|
alias l="${GNU_LS} --group-directories-first --color=auto -v"
|
2024-01-07 17:31:47 +05:30
|
|
|
alias lo="${GNU_LS} --group-directories-first --color=auto -1v"
|
2023-08-31 14:07:13 +05:30
|
|
|
alias ll="${GNU_LS} --group-directories-first --color=auto -1lv --time-style=long-iso"
|
|
|
|
alias la="${GNU_LS} --group-directories-first --color=auto -1v --time-style=long-iso --almost-all"
|
2024-01-07 17:31:47 +05:30
|
|
|
alias ldt="${GNU_LS} --group-directories-first --color=auto -1ltv --time-style=long-iso --almost-all"
|
2023-08-31 14:07:13 +05:30
|
|
|
alias llh="${GNU_LS} --group-directories-first --color=auto -1lv --time-style=long-iso --human-readable"
|
2024-04-16 09:04:58 +05:30
|
|
|
alias alh="${GNU_LS} --group-directories-first --color=auto -1lv --time-style=long-iso --human-readable --almost-all"
|
2023-08-31 14:07:13 +05:30
|
|
|
alias lah="${GNU_LS} --group-directories-first --color=auto -1lv --time-style=long-iso --human-readable --almost-all"
|
2024-04-11 13:55:44 +05:30
|
|
|
alias lha="${GNU_LS} --group-directories-first --color=auto -1lv --time-style=long-iso --human-readable --almost-all"
|
2023-08-31 14:07:13 +05:30
|
|
|
alias lsh="${GNU_LS} --group-directories-first --color=auto -1lv --time-style=long-iso --human-readable --almost-all"
|
|
|
|
|
|
|
|
if command -v batcat > /dev/null; then
|
|
|
|
alias bat="$(command -v batcat)"
|
|
|
|
fi
|
|
|
|
|
2024-04-10 15:21:07 +05:30
|
|
|
if command -v fdfind > /dev/null; then
|
|
|
|
alias fd="$(command -v fdfind)"
|
|
|
|
fi
|
|
|
|
|
2024-03-10 06:39:30 +05:30
|
|
|
function nixos_needsreboot() {
|
|
|
|
NIXOS_NEEDSREBOOT_FILE='/var/run/reboot-required'
|
|
|
|
if [[ -f "${NIXOS_NEEDSREBOOT_FILE}" ]]; then
|
2024-03-19 17:01:45 +05:30
|
|
|
echo -e "\n$(tput bold)${PS0_HORIZONTAL_RULE}\nNewer version of $(cat "${NIXOS_NEEDSREBOOT_FILE}") is available!\n${PS0_HORIZONTAL_RULE}$(tput sgr0)"
|
2024-03-10 06:39:30 +05:30
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-01-08 15:54:34 +05:30
|
|
|
# PS1 setup
|
2024-04-16 09:56:31 +05:30
|
|
|
PS0="\t|\n--------+" # display time in HH:MM:SS format
|
2024-03-19 16:59:27 +05:30
|
|
|
# the function needs to be called **everytime** and is in single quotes
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
PS0+='$(nixos_needsreboot)'"\n"
|
|
|
|
PS1="\n[\u@\h:\$PWD \$?]\$ "
|
2024-01-08 15:54:34 +05:30
|
|
|
export PS0 PS1
|
2024-01-07 17:31:47 +05:30
|
|
|
|
|
|
|
# zoxide setup
|
|
|
|
if command -v zoxide > /dev/null; then
|
|
|
|
export _ZO_ECHO=1 # always print matched dir before navigating to it
|
|
|
|
export _ZO_RESOLVE_SYMLINKS=1
|
|
|
|
eval "$(zoxide init bash)"
|
|
|
|
fi
|
2023-09-22 05:29:50 +05:30
|
|
|
|
|
|
|
# for direnv (should always be at the end)
|
|
|
|
if command -v direnv > /dev/null; then
|
|
|
|
eval "$(direnv hook bash)"
|
|
|
|
fi
|