regexghost-dotfiles

My dotfiles and scripts
git clone git@git.regexghost.com/regexghost-dotfiles.git
Log | Files | Refs | README

.bashrc (14217B)


      1 # regexghost's .bashrc file
      2 # Hopefully in a coherent order
      3 
      4 PROMPT_COMMAND=
      5 # If not running interactively, don't do anything
      6 [[ $- != *i* ]] && return
      7 
      8 # Load bash completion
      9 if ! shopt -oq posix; then
     10   if [ -f /usr/share/bash-completion/bash_completion ]; then
     11 	. /usr/share/bash-completion/bash_completion
     12   elif [ -f /etc/bash_completion ]; then
     13 	. /etc/bash_completion
     14   fi
     15 fi
     16 
     17 # Add my directory_bookmarks program to the prompt when relevant
     18 PS1='\[\033[1;30m\]\u\[\033[1;31m\]@\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;34m\]$(directory_bookmarks current)\[\033[1;31m\]\$\[\033[0m\] '
     19 
     20 ## Function to remove things which aren't useful from bash history
     21 trim_history () {
     22 	sed -i -r '/^(history|hs|qalc|vis|cava|nethogs|btop|htop|gitl|gitd|gits|x|rm|cd|c|exit|lsa|ls|l|q)$/d' ~/.bash_history
     23 	# Remove any usage of fasd z autojump command
     24 	sed -i '/^z .*/d' ~/.bash_history
     25 	sed -i '/^zz .*/d' ~/.bash_history
     26 	# Remove any usage of rm command
     27 	sed -i '/^rm .*/d' ~/.bash_history
     28 	sed -i '/^youtube .*/d' ~/.bash_history
     29 	sed -i '/^to .*/d' ~/.bash_history
     30 	
     31 	# Remove any usage of cd, ls and mpv when only going one folder deeper in file structure
     32 	sed -i -r '/^(cd|ls|mpv|mpv) [^\/\>\<|:&]*\/? ?$/d' ~/.bash_history
     33 	# Remove any usage of m, rs and pdf when not going into a different folder
     34 	sed -i -r '/^(m|rs|pdf) [^\/\>\<|:&]* ?$/d' ~/.bash_history
     35 	# Remove anything in all caps, as it will basically always be a mistype
     36 	sed -i '/^[A-Z0-9 ]*$/d' ~/.bash_history
     37 	# Remove all duplicates, keeping most recent
     38 	tac ~/.bash_history | awk '!x[$0]++' | tac > ~/.bash_history_no_dupes && command mv ~/.bash_history_no_dupes ~/.bash_history
     39 	#sed --in-place 's/[[:space:]]\+$//' .bash_history && awk -i inplace '!seen[$0]++' .bash_history
     40 }
     41 
     42 # Basic Aliases
     43 alias grep='grep -i --color=auto'
     44 alias greps='/usr/bin/grep --color=auto' # Case sensitive
     45 alias grepa='grep -i -I -A 5 -B 5 --color=auto'
     46 alias diff='diff --color'
     47 alias hs='history'
     48 alias n='nnn'
     49 alias py='python3'
     50 alias nf='fastfetch'
     51 alias sq='ncdu --color dark' # Not sure why this is "sq" but I'm used to it now
     52 alias bat='bat --theme=base16'
     53 alias mv='mv -i'
     54 alias cp='cp -r -i'
     55 alias cmatrix='cmatrix -u 6' # Cool fake hacker program
     56 alias duf='duf -hide special'
     57 alias gtop='sudo intel_gpu_top'
     58 alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"'
     59 alias rm='rm-trash-cli'
     60 alias cheat='cheat -c'
     61 alias zbr='zig build run'
     62 alias bluey='bluetui'
     63 alias fontsreload='sudo fc-cache -fv'
     64 alias rss='newsraft'
     65 alias trash-size='du ~/.local/share/Trash/files/ -s -h | cut -f 1'
     66 alias sync='echo "Syncing"; sync; echo "Done"; lsblk'
     67 
     68 # Aliases to Specific Commands
     69 
     70 alias x='chmod +x'
     71 alias copy='xclip -selection c'
     72 alias batl='find . -maxdepth 1 | sort | tail -n 1 | xargs bat --theme=base16'
     73 alias watchlc="watch 'ls | wc -l'"
     74 alias watchdu="watch 'du -s -h *'"
     75 alias lastyear='log -d $(date -d "-1 year" +"%y%m%d")' # Interacts with my log program
     76 alias fatmount='sudo mount -o rw,users,umask=000' # Mount FAT formatted drive correctly
     77 alias rmedir='find . -type d -empty -delete'
     78 alias balance='aacgain -r -m 1 *.m4a'
     79 alias vol='pactl get-sink-volume @DEFAULT_SINK@ | head -n 1 | cut -d "/" -f 2 | sed "s/ //g"'
     80 alias clearlogs='sudo journalctl --vacuum-time=2d'
     81 alias q='trim_history && exit'
     82 alias reload='. ~/.bashrc'
     83 
     84 # Git Aliases
     85 
     86 alias gitd='git diff'
     87 alias gitdc='git diff --word-diff-regex=.'
     88 alias gits='git status'
     89 alias gpl='git pull'
     90 alias gp='git push'
     91 alias gitl='git log --reverse'
     92 alias giturl='git config --get remote.origin.url'
     93 
     94 
     95 #### SUBSTITUTE - PACKAGE MANAGER
     96 alias install='sudo pacman -S'
     97 alias remove='sudo pacman -Rs'
     98 alias update='sudo pacman -Syu'
     99 alias search='pacman -Ss'
    100 alias paclog='cat /var/log/pacman.log | grep'
    101 
    102 function pacs() {
    103 	numberOfPackages="$(pacman -Q | wc -l)"
    104 	echo "${numberOfPackages} packages installed"
    105 }
    106 #### END SUBSTITUTE
    107 
    108 # Other random aliases
    109 
    110 alias as='echo "Use \as to run as command, disabled as too easy to type accidentally, creating unnecessary a.out file in home directory"'
    111 alias todo='$VISUAL ~/Documents/todo.md'
    112 alias durationr='media-file-duration . -r'
    113 alias durationi='media-file-duration . -i'
    114 
    115 ## Functions to basic programs
    116 
    117 # fzf -> editor
    118 function qfi () {
    119 	file="$(find ~/* | grep -E '.py$|.go$|.txt$|.md$|.java$|.js$|.html$|.css$|.c$|.cc$|.conf$|.lua$|.rs$|.sh$|.bash$|.csv$' | sed 's|'"$HOME"'|~|g' | fzf --bind 'ctrl-backspace:backward-kill-word' --bind 'ctrl-delete:kill-word' --bind 'ctrl-right:forward-word' --bind 'ctrl-left:backward-word')"
    120 	[[ "$file" == "" ]] && return
    121 	$VISUAL "$file"
    122 }
    123 
    124 alias m='micro -clipboard terminal'
    125 alias v='vim'
    126 
    127 function batf () {
    128 	result=$(fasd -fi $@)
    129 	[ "$result" == "" ] && return
    130 	bat --theme=base16 "$result"
    131 }
    132 
    133 function lsblk () {
    134 	if [[ "$1" == "-a" ]]; then
    135 		/usr/bin/lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,TYPE,FSTYPE,MOUNTPOINTS
    136 	else
    137 		/usr/bin/lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
    138 	fi
    139 }
    140 
    141 function do_pkill () {
    142 	pkill "$@"
    143 	[ "$?" == "0" ] || echo "Program not found"
    144 }
    145 
    146 alias pkill='do_pkill'
    147 
    148 function storage () {
    149 	root_line=$(df -h | grep ' /$' | tr -s " ")
    150 	used=$(echo "$root_line" | cut -d " " -f 3)
    151 	free=$(echo "$root_line" | cut -d " " -f 4)
    152 	echo "Used: ${used}"
    153 	echo "Free: ${free}"
    154 }
    155 
    156 function trash-empty() {
    157 	echo "Trash is $(trash-size)"
    158 	/usr/bin/trash-empty
    159 }
    160 
    161 # Only use this if the history in the current terminal is suddenly way shorter than it should be
    162 function restorebashhistory () {
    163 	history | sed 's/^[ ]*[0-9]*[ ]*//g' > /tmp/new_history
    164 	cat "$HOME/Downloads/.bash_history_backup" /tmp/new_history > /tmp/all_history
    165 	mv /tmp/all_history "$HOME/.bash_history"
    166 }
    167 
    168 function cal () {
    169 	MAGENTA_COLOUR='\033[0;35m\033[1m'
    170 	RED_COLOUR='\033[0;34m\033[1m'
    171 	BLUE_COLOUR='\033[0;36m\033[1m'
    172 	RESET_COLOUR='\033[0m'
    173 	
    174 	function suffix () {
    175 		if [[ "$1" == "1" ]] || [[ "$1" == "21" ]] || [[ "$1" == "31" ]]; then
    176 			echo "st"
    177 		elif [[ "$1" == "2" ]] || [[ "$1" == "22" ]]; then
    178 			echo "nd"
    179 		elif [[ "$1" == "3" ]] || [[ "$1" == "23" ]]; then
    180 			echo "rd"
    181 		else
    182 			echo "th"
    183 		fi
    184 	}
    185 
    186 	unbuffer cal -w -n 3 "$@" | sed '/^ *$/d'
    187 	
    188 	day="$(date +'%A')"
    189 	date="$(date +'%d')"
    190 	week="$(date +'%W')"
    191 	
    192 	echo -e "${RED_COLOUR}Day:${RESET_COLOUR}  ${day}"
    193 	echo -e "${BLUE_COLOUR}Date:${RESET_COLOUR} ${date}$(suffix $date)"
    194 	echo -e "${MAGENTA_COLOUR}Week:${RESET_COLOUR} ${week}"
    195 }
    196 
    197 function pyweb () {
    198 	$(sleep 0.5 && firefox "http://0.0.0.0:8000/") &
    199 	python3 -m http.server -d "$1"
    200 }
    201 
    202 ## Function to show time in various locations
    203 function t () {
    204 	MAGENTA_COLOUR='\033[0;35m\033[1m'
    205 	RESET_COLOUR='\033[0m'
    206 	curDateZone=$(date +"%a, %b %d (%Z)")
    207 	TZ="America/Los_Angeles" date +"  Los Angeles:    %H:%M:%S - %a, %b %d (%Z)"
    208 	TZ="America/New_York" date +"  New York:       %H:%M:%S - %a, %b %d (%Z)"
    209 	date -u +"  UTC:            %H:%M:%S - %a, %b %d (%Z)"
    210 	TZ="Europe/London" date +"  London:         %H:%M:%S - %a, %b %d (%Z)"
    211 	TZ="Europe/Paris" date +"  Paris:          %H:%M:%S - %a, %b %d (%Z)"
    212 	TZ="Asia/Seoul" date +"  Seoul:          %H:%M:%S - %a, %b %d (%Z)"
    213 	TZ="Australia/Sydney" date +"  Sydney:         %H:%M:%S - %a, %b %d (%Z)"
    214 }
    215 
    216 # External program openers
    217 
    218 #### SUBSTITUTE - IMAGE VIEWER
    219 # Open images in gwenview
    220 # The alias is called "rs" as I originally used Xfce with Ristretto
    221 rs () {
    222 	toOpen=$@
    223 	if [[ "$toOpen" == "" ]]; then
    224 		gwenview . & disown
    225 	else
    226 		gwenview "$@" & disown
    227 	fi
    228 }
    229 #### END SUBSTITUTE
    230 
    231 #### SUBSTITUTE - VIDEO PLAYER
    232 # Open video(s) with mpv
    233 mp () {
    234 	/usr/bin/mpv --really-quiet --save-position-on-quit "$@" & disown
    235 }
    236 #### END SUBSTITUTE
    237 
    238 #### SUBSTITUTE - PDF VIEWER
    239 # Open pdf files in Okular
    240 pdf () {
    241 	for arg; do
    242 		okular "$arg" & disown
    243 	done
    244 }
    245 #### END SUBSTITUTE
    246 
    247 # Power Management Functions
    248 
    249 #### SUBSTITUTE - POWER MANAGEMENT
    250 ## Shutdown with confirmation
    251 shutdown () {
    252 	read -p "Shutdown? (y/N) " yesOrNoShutdown
    253 	if [[ "$yesOrNoShutdown" == "y" ]]; then
    254 		tmux send-keys -t buffer_tmux.0 C-s
    255 		tmux send-keys -t buffer_tmux.0 C-q
    256 		tmux kill-session -t buffer_tmux
    257 		qdbus6 org.kde.LogoutPrompt /LogoutPrompt promptShutDown
    258 	fi
    259 }
    260 
    261 ## Reboot with confirmation
    262 reboot () {
    263 	read -p "Reboot? (y/N) " yesOrNoReboot
    264 	if [[ "$yesOrNoReboot" == "y" ]]; then
    265 		tmux send-keys -t buffer_tmux.0 C-s
    266 		tmux send-keys -t buffer_tmux.0 C-q
    267 		tmux kill-session -t buffer_tmux
    268 		qdbus6 org.kde.LogoutPrompt /LogoutPrompt promptReboot
    269 	fi
    270 }
    271 
    272 ## Hibernate to disk with confirmation
    273 hibernate () {
    274 	read -p "Hibernate? (y/N) " yesOrNoHibernate
    275 	[[ "$yesOrNoHibernate" == "y" ]] && qdbus6 org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement Hibernate
    276 }
    277 
    278 ## Hybrid-Sleep with confirmation, i.e. sleep to RAM and disk in case battery dies
    279 hybrid-sleep () {
    280 	read -p "Hybrid-Sleep? (y/N) " yesOrNoHybridSleep
    281 	[[ "$yesOrNoHybridSleep" == "y" ]] && systemctl hybrid-sleep
    282 }
    283 
    284 ## Sleep with confirmation (i.e. RAM only)
    285 qsleep () {
    286 	read -p "Sleep? (y/N) " yesOrNoQSleep
    287 	[[ "$yesOrNoQSleep" == "y" ]] && qdbus6 org.kde.Solid.PowerManagement /org/freedesktop/PowerManagement Suspend
    288 }
    289 
    290 ## Log Out with confirmation
    291 log-out () {
    292 	read -p "Log Out? (y/N) " yesOrNoLogOut
    293 	[[ "$yesOrNoLogOut" == "y" ]] && qdbus6 org.kde.LogoutPrompt /LogoutPrompt org.kde.LogoutPrompt.promptLogout
    294 }
    295 
    296 ## Lock screen with confirmation
    297 lock () {
    298 	read -p "Lock Screen? (y/N) " yesOrNoLock
    299 	[[ "$yesOrNoLock" == "y" ]] && qdbus6 org.kde.screensaver /ScreenSaver Lock
    300 }
    301 #### END SUBSTITUTE
    302 
    303 # yt-dlp helper function
    304 do_yt-dlp () {
    305 	local aria_args=()
    306 	local metadata_args=()
    307 	local cookies_args=()
    308 	local archive_args=()
    309 	local other_args=()
    310 	output_format_args=(-o "%(title)s.%(ext)s")
    311 	format_args=()
    312 	
    313 	while [[ $# -gt 0 ]]; do
    314 		case "$1" in
    315 			--aria)
    316 				aria_args+=("--external-downloader" "aria2c" "--external-downloader-args" "aria2c:-x 16 -j 16 -s 16 -k 1M")
    317 				shift
    318 				;;
    319 			--aria-limit)
    320 				download_limit="3"
    321 				num_re='^[0-9]+M*$'
    322 				if [[ "$2" =~ $num_re ]]; then
    323 					download_limit="${2//M}"
    324 					shift
    325 				fi
    326 				aria_args+=("--external-downloader" "aria2c" "--external-downloader-args" "aria2c:-x 16 -j 16 -s 16 -k 1M --max-overall-download-limit=${download_limit}M")
    327 				shift
    328 				;;
    329 			--all-metadata)
    330 				metadata_args+=("--embed-chapters" "--embed-thumbnail" "--embed-metadata")
    331 				shift
    332 				;;
    333 			--music)
    334 				output_format_args=(-o "%(title)s - %(channel)s - %(album)s.%(ext)s")
    335 				format_args=(-f 140)
    336 				shift
    337 				;;
    338 			--playlist-order)
    339 				output_format_args=(-o "%(playlist_index)s-%(title)s.%(ext)s")
    340 				shift
    341 				;;
    342 			--standard)
    343 				format_args=(-f "22/bestvideo[height<=720]+bestaudio")
    344 				shift
    345 				;;
    346 			--firefox-cookies)
    347 				cookies_args=("--cookies-from-browser" "firefox")
    348 				shift
    349 				;;
    350 			--archive)
    351 				archive_args=("--download-archive" "archive.txt")
    352 				shift
    353 				;;
    354 			--1080p)
    355 				format_args=("-f" "bestvideo[height<=1080][protocol=https][vcodec*=avc]+bestaudio[ext=m4a]")
    356 				shift
    357 				;;
    358 			--720p)
    359 				format_args=("-f" "bestvideo[height<=720][protocol=https][vcodec*=avc]+bestaudio[ext=m4a]")
    360 				shift
    361 				;;
    362 			--480p)
    363 				format_args=("-f" "bestvideo[height<=480][protocol=https][vcodec*=avc]+bestaudio[ext=m4a]")
    364 				shift
    365 				;;
    366 			--360p)
    367 				format_args=("-f" "bestvideo[height<=360][protocol=https][vcodec*=avc]+bestaudio[ext=m4a]")
    368 				shift
    369 				;;
    370 			*)
    371 				other_args+=("$1")
    372 				shift
    373 				;;
    374 		esac
    375 	done
    376 	
    377 	local all_args=("${output_format_args[@]}" "${aria_args[@]}" "${metadata_args[@]}" "${cookies_args[@]}" "${format_args[@]}" "${archive_args[@]}" "${other_args[@]}" "$@")
    378 	/usr/bin/yt-dlp "${all_args[@]}"
    379 }
    380 
    381 ## Alias to allow escaping with backslash
    382 alias yt-dlp='do_yt-dlp'
    383 
    384 function yt-playlist () {
    385 	quality_option="--720p"
    386 	if [[ "$1" == "--1080p" ]]; then
    387 		quality_option="--1080p"
    388 		shift
    389 	fi
    390 	do_yt-dlp --playlist-order --all-metadata --firefox-cookies --aria --archive "$quality_option" "$@"
    391 }
    392 
    393 function archivevideo() {
    394 	for url in "$@"; do
    395 		yt-dlp --cookies-from-browser firefox -o "%(title)s.%(ext)s" --embed-chapters --embed-thumbnail --embed-metadata -f "bestvideo[protocol=https][vcodec*=avc]+bestaudio[ext=m4a]" "$url"
    396 	done
    397 }
    398 
    399 function archiveplaylist() {
    400 	if [[ "$1" == "-t" ]]; then
    401 		yt-dlp --cookies-from-browser firefox --flat-playlist --skip-download -J "$2" | jq '{title, videos: [.entries[] | {title, channel, id}]}' | yq -t
    402 	else
    403 		yt-dlp --cookies-from-browser firefox -J --flat-playlist "$1" | jq '.entries[] | [.title,.channel,.url]| @csv'
    404 	fi
    405 }
    406 
    407 # Search commands
    408 
    409 alias hs='history'
    410 alias his='history | grep'
    411 
    412 ## Search root directory
    413 findr () {
    414 	find / -iname "$1" 2>&1 | grep -v 'Permission denied'
    415 }
    416 
    417 alias findh='find ~ -iname'
    418 alias fig='find . | sort | grep'
    419 alias psg='ps -aux | grep'
    420 
    421 # Load my other aliases and functions
    422 
    423 source "$HOME/.local/share/regexghost/terminal/autocompletion.bash"
    424 source "$HOME/.local/share/regexghost/terminal/ls_aliases.bash"
    425 [ -f ~/Programs/localStuff/aliases.sh ] && source ~/Programs/localStuff/aliases.sh
    426 
    427 # Set variables
    428 
    429 HISTSIZE=80000
    430 HISTFILESIZE=80000
    431 
    432 export HISTCONTROL=ignoreboth:erasedups
    433 export MICRO_TRUECOLOR=1
    434 export PASSWORD_STORE_CLIP_TIME=120
    435 
    436 # Quick file/directory access
    437 
    438 alias doc='cd ~/Documents/'
    439 alias dow='cd ~/Downloads/'
    440 alias mus='cd ~/Music/'
    441 alias vid='cd ~/Videos/'
    442 alias pic='cd ~/Pictures/'
    443 alias pro='cd ~/Programs/'
    444 alias wor='cd ~/Work/'
    445 alias loc='cd ~/.local/share/'
    446 alias bin='cd ~/.local/bin/'
    447 alias con='cd ~/.config/'
    448 
    449 function to {
    450 	cd "$(directory_bookmarks get "$1")"
    451 }
    452 alias bm='directory_bookmarks add'
    453 alias bmr='directory_bookmarks remove'
    454 alias bml='directory_bookmarks list'
    455 alias bmc='directory_bookmarks current'
    456 
    457 alias cdp='cd - > /dev/null'
    458 
    459 export _FASD_NOCASE=1
    460 eval "$(fasd --init auto)"
    461 
    462 function do_z () {
    463 	command="fasd_cd -d"
    464 	if [[ "$1" == "--choice" ]]; then
    465 		command="fasd_cd -d -i"
    466 		shift
    467 	fi
    468 
    469 	if [[ "$1" == ".." ]]; then
    470 		last_dir="$(cat /tmp/fasd_last_dir)"
    471 		pwd > /tmp/fasd_last_dir
    472 		cd "$last_dir"
    473 		return
    474 	fi
    475 	pwd > /tmp/fasd_last_dir
    476 	$command "$1"
    477 }
    478 
    479 function qf {
    480 	file="$(directory_bookmarks file get "$1")"
    481 	[[ "$file" == "" ]] && return
    482 	"${VISUAL:-${EDITOR}}" "$file"
    483 }
    484 
    485 alias qfa="directory_bookmarks file add"
    486 alias qfr="directory_bookmarks file remove"
    487 alias qfl="directory_bookmarks file list"
    488 
    489 unalias z
    490 unalias zz
    491 
    492 alias z='do_z'
    493 alias zz='do_z --choice'
    494 
    495 unalias a
    496 unalias s
    497 unalias sd
    498 unalias sf
    499 unalias d
    500 unalias f