Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions bash/.config/bash/functions
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ EOF
# =====================
compress() { tar -czf "${1%/}.tar.gz" "${1%/}"; }

extract() {
if [ -z "$1" ]; then
echo "Usage: extract <archive>"
return 1
fi
if [ ! -f "$1" ]; then
echo "'$1' is not a file"
return 1
fi
case "$1" in
*.tar.gz|*.tgz) tar -xzf "$1" ;;
*.tar.bz2|*.tbz2) tar -xjf "$1" ;;
*.tar.xz) tar -xJf "$1" ;;
*.tar) tar -xf "$1" ;;
*.zip) unzip "$1" ;;
*.gz) gunzip "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.xz) unxz "$1" ;;
*) echo "Cannot extract '$1': unsupported format" ; return 1 ;;
esac
}
# Usage: extract <archive> – auto-detect and extract any common archive format

# =====================
# CSV Exploration Helper
# =====================
Expand All @@ -79,6 +102,26 @@ csvlens() {
}
# Usage: csvlens (fuzzy-pick CSV), csvlens file.csv (direct open)

# =====================
# Interactive Content Search
# =====================
rgf() {
if [ "$#" -eq 0 ]; then
echo "Usage: rgf <pattern> [rg options]"
return 1
fi
local result
result=$(
rg --color=always --line-number --no-heading "$@" 2>/dev/null |
fzf --ansi \
--delimiter ':' \
--preview 'batcat --color=always --style=numbers --highlight-line {2} {1}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
)
[ -n "$result" ] && echo "$result"
}
# Usage: rgf <pattern> – ripgrep search with fzf preview; outputs the selected file:line

# =====================
# APT & Package Management
# =====================
Expand Down Expand Up @@ -132,6 +175,9 @@ apt-fzf-purge() {
# =====================
# Shell & History Utilities
# =====================
mkd() { mkdir -p "$@" && cd "${!#}"; }
# Usage: mkd path/to/dir – create directory (and parents) then cd into it

zd() {
if [ $# -eq 0 ]; then
builtin cd ~ && return
Expand All @@ -150,6 +196,120 @@ __fzf_history__() {
fi
}
bind -x '"\C-r": __fzf_history__'

# =====================
# Interactive Process Management
# =====================
ps-fzf() {
local pid
pid=$(ps aux |
fzf --header-lines=1 \
--prompt='Kill process> ' \
--preview 'echo {}' \
--preview-window=down:3:wrap |
awk '{print $2}')
if [ -n "$pid" ]; then
read -r -p "Send SIGTERM to PID $pid? [y/N] " confirm
[[ "$confirm" =~ ^[Yy]$ ]] && kill "$pid" && echo "Sent SIGTERM to PID $pid" || echo "Aborted."
fi
}
# Usage: ps-fzf – interactively select a running process and send SIGTERM

# =====================
# Documentation & Help
# =====================
tldrf() {
if ! command -v tldr >/dev/null 2>&1; then
echo "tldr (tealdeer) is not installed."
return 1
fi
local cmd
cmd=$(tldr --list 2>/dev/null |
fzf --prompt='tldr> ' \
--preview='tldr {}' \
--preview-window='right:70%:wrap')
[ -n "$cmd" ] && tldr "$cmd"
}
# Usage: tldrf – fuzzy-pick a command to view its tldr (tealdeer) page

# =====================
# Git Log Browser
# =====================
glf() {
if ! git rev-parse --git-dir >/dev/null 2>&1; then
echo "Not inside a git repository."
return 1
fi
local commit
commit=$(
git log --oneline --color=always "$@" |
fzf --ansi \
--prompt='git log> ' \
--preview 'git show --stat --color=always {1}' \
--preview-window='right:60%:wrap'
) || return
[ -n "$commit" ] && git show "$(echo "$commit" | awk '{print $1}')"
}
# Usage: glf [git-log options] – browse git history; preview and show diff of selected commit

# =====================
# JSON Explorer
# =====================
jqf() {
local file
if [ "$#" -eq 0 ]; then
file=$(fdfind . . --type f --extension json 2>/dev/null | fzf --prompt='JSON file> ' --preview 'jq -C . {}')
[ -z "$file" ] && return
else
file="$1"
fi
if [ ! -f "$file" ]; then
echo "'$file' is not a file"
return 1
fi
jq -C . "$file" | batcat --language=json --style=plain --color=always
}
# Usage: jqf [file.json] – fuzzy-pick or open a JSON file, pretty-print with jq + batcat

# =====================
# Environment Variables
# =====================
envf() {
local var
var=$(env | sort |
fzf --prompt='env> ' \
--preview 'echo {}' \
--preview-window=down:3:wrap)
[ -n "$var" ] && echo "$var"
}
# Usage: envf – fuzzy-search current environment variables

# =====================
# GitHub PR Checkout
# =====================
ghpr() {
if ! command -v gh >/dev/null 2>&1; then
echo "gh CLI is not installed."
return 1
fi
local pr
pr=$(
gh pr list \
--json number,title,author,headRefName \
--jq '.[] | "\(.number)\t\(.title)\t@\(.author.login)\t\(.headRefName)"' |
fzf --prompt='Checkout PR> ' \
--with-nth=1,2,3 \
--delimiter='\t' \
--ansi \
--preview 'gh pr view {1}' \
--preview-window='right:60%:wrap' |
cut -f1
) || return 1
[ -z "$pr" ] && echo "No PR selected" && return 1
gh pr checkout "$pr"
}
# Usage: ghpr – fuzzy-pick an open pull request and check it out locally

# =====================
# GitHub Clone Helper
# =====================
Expand Down