Skip to content

Instantly share code, notes, and snippets.

@ekipan
Last active April 9, 2026 23:25
Show Gist options
  • Select an option

  • Save ekipan/1c2e5a6164fbe7601b32e089d889994c to your computer and use it in GitHub Desktop.

Select an option

Save ekipan/1c2e5a6164fbe7601b32e089d889994c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# some excerpts from my bashrc. tested in 5.2.
# CC0 or MIT, I don't care enough.
# shellcheck enable=all disable=2312,2250,2120 # disables:
# cmd||true, ${v}, unused args, though a few more still trigger.
### helpers
e() { echo >&2 "$@"; "$@"; } # to stay aware of my aliases/functions
k() { cut -c"$2"-$((8*${1:-2}+${2:-1}-2)) | column -c"$COLUMNS"; }
# k [TABS] [STARTCOL] eg: ls /nix/store | sort -t- -k2 | k 2 34
abbr() { sed -E "s|(/\.?[^/])[^/]+|\1|g;s|[^/]*$||" <<<"$1"; } # see ps1
hl() { bat -Ppl "${1:-help}"; } # highlight eg: find --help | hl
iftty() { if [[ -t "$1" ]]; then "${@:2}"; else cat; fi; }
# eg: cmd | iftty 1 nl # only number lines if going to console.
isort() { tee >(awk '{printf"\r"NR}' >&2; echo >&2) | sort "$@"; }
# show linecount while waiting, see bigx() below, it's racy but eh.
readxy() { IFS=[\; read -dR -p$'\e[6n' -rst"${3-.1}" _ "$2" "$1"; }
# eg: readxy x y # read cursor position \e[6n -> \e[$y;$xR
### small guys, I constantly use h, opts, bigx.
h() { iftty 0 "$@" --help |& awk '/\S/;/gnu\.org/{exit}' | hl; }
# eg: h find # condensed and highlighted, remove the big gnu footer
l() { ls -A "$@" | k; } # for quick browsing
bigx() { du -xhd2 -t10M "${@:2}" | isort -h | tail -n "$1"; }
# eg: alias big='bigx 10'; big /nix; big ~/.local/share/Steam/steamapps
nixdry() { e nix-shell --dry-run -p "$@" |& awk '!f;/fetched/{f=1}'; }
# eg: nixdry fastfetch # estimate software download size
opts() { iftty 0 "$@" --help |& awk 'NR<4||/^\s*-/' | hl; }
# eg: opts find # just the flags, please
### functions I use often:
gits() { : 'gits [DIR] [CMD] [OPTS..] # touch all repos in DIR'
local d=${1:-$HOME/code}; (($#<2)) && set -- _ status --short
find "$d" -maxdepth 2 -type d -name .git | while read -r d
do e git -C "$(dirname "$d")" "${@:2}" || break; echo; done
} # eg: gits . diff --stat # or: gits . push
prompt() { : 'prompt [PROMPT_COMMAND] [PS1] [PS2] [PS4] # set vars'
PROMPT_COMMAND=${1-'PS1=$(ps1)'} # $(subshell) for [1]
PS1=${2-'\$ '} PS2=${3-'> '} PS4=${4-'+$? '}
} # eg: prompt '' # disable PROMPT_COMMAND for '$ ' examples.
# shellcheck disable=1087 # $e[...\] false positives
ps1() { # based on __steamos_prompt_command in /etc/bash.bashrc
set +euvx -- $? "${PIPESTATUS[@]}" # [1] +vx less debug noise
local IFS h l m n r x e='\[\e' z='\[\e[m\]'; readxy x _
# 30fg 40bg +60bri 0blk 1red 2grn 3yel 4blu 5mag 6cya 7whi
((x>1)) && m="$e[;45m\] $z\n" # mark missing newline
(($1)) && IFS='|' r="$e[;1;91m\]${*:2}$z" # retcode
h=${SSH_CONNECTION:+'@\h'} # host if remote
w='\w' w=$(abbr "${w@P}") # abbreviate /abc/.def/ghi to /a/.d/
n=${IN_NIX_SHELL:+n} # nix-shell marker
l='\$\$\$'$SHLVL l=${l::2*SHLVL} # $'s nesting level
echo "$m$r$e[;1;93m\](\u$h $w\W)$n$l$z "
} # output eg: 1(joe@box ~/^/$/.e/x/ample)n$$ _
# the $m marker only inserts a newline if the last command didn't
# so I can expect my prompts to be on the margin of my scrollback
### in my bashrc but maybe less generally useful:
clashes() { : 'clashes [CMDS..] # print overloads'
local t c; for c in ${@:-$(compgen -aA function)}; do
t=($(type -at "$c")); ((${#t[@]}>1)) && echo -n "$c "
done; echo
} # eg: clashes $(compgen -c) # check all commands
soss() { : 'soss [1-5] # steamos-session-select, or list'
[[ $1 =~ ^[1-5]$ ]] && set -- "$(soss | sed -n "$1p")"
(($#)) && e /usr/bin/ste*ct "$1" --no-restart ||
rg '([-\w]+)\)$' -or '$1' /usr/bin/ste*ct | iftty 1 nl
} # eg: soss 1 # switch to plasma-wayland-persistent
q() { : 'q [N] # line N or random from ~/quotes: AUTHOR|QUOTE'
local a q aq f=~/quotes nbsp=$'\u00a0' dash=$'\u2014'
aq=$((($#)) && sed -n "$1p" "$f" || shuf -n1 "$f")
IFS='|' read -r a q <<<"$aq"
fold -sw "$COLUMNS" <<<"$q $dash$nbsp${a// /$nbsp}"
} # in my bashrc: (readxy _ y; ((y>1))) || q # for new tabs
# I took ~/quotes data from here, though I've manually edited:
# https://github.com/uvallasciani/matecito-zsh/tree/main/phrases/en
# https://www.reddit.com/r/zsh/comments/1rs6gcn/
### useful, but I'm unsure they deserve a place in my bashrc:
pal() { : 'pal; pal 234; pal 1 2 3 # palette, swatch, or grid.'
local x y; case $# in
1) printf "\e[38;5;%sm%3s " "$1" "$1" ;;
3) for y in $(seq 0 "$3"); do for x in $(seq 0 "$2")
do pal $(($1+y*($2+1)+x)); done; printf "\e[m\n"; done ;;
*) pal 0 7 1; pal 16 17 11; pal 232 11 1 ;;
esac
} # note: 3) takes upper bounds: pal 1 2 3 is 3 cols 4 rows.
# probably better as a separate script
path() { # path add [DIR]; path rm [DIR]; path dedup; path
case $1 in # note: cannot use '\n' in dirs, removes empty.
add) PATH="$2:$PATH"; path dedup; [[ -d "$2" ]] ;;
rm) PATH=$(path get | grep -vxF "$2" | paste -sd:) ;;
dedup) PATH=$(path get | awk '/./&&!a[$0]++' | paste -sd:) ;;
get) tr : '\n' <<<"$PATH" ;; *) path dedup; path get ;;
esac || echo >&2 "path $1, but missing: $2"
} # eg: path add ~/.local/bin # prepend to PATH and dedup.
# I only used this once, so I took it back out of my bashrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment