feat: add Dotbot

This commit is contained in:
Stefan Imhoff
2024-08-09 11:57:28 +02:00
parent a41290c297
commit 0437240919
248 changed files with 1205 additions and 44 deletions

View File

@@ -0,0 +1,18 @@
# Delete all node_modules folders in a folder and subfolders
delete_node_modules() {
echo "This will delete all node_modules directories in the current directory and its subdirectories."
echo "Are you sure you want to continue? (y/N)"
read -r response
case "$response" in
[yY][eE][sS]|[yY])
echo "Searching for node_modules directories..."
find . -name "node_modules" -type d -print -exec rm -rf {} +
echo "Deletion complete."
;;
*)
echo "Operation cancelled."
;;
esac
}

6
functions/fcd.sh Executable file
View File

@@ -0,0 +1,6 @@
# fcd - cd into directory
fcd() {
local dir
dir=$(find . -type d | sed '1d; s|^\./||' | fzf --preview 'tree -C {} | head -50') && cd "$dir"
}

22
functions/fcdh.sh Executable file
View File

@@ -0,0 +1,22 @@
# fhcd Jump to home directory and search for directories
fhcd() {
# Change to home directory
cd "$HOME" || return
# Change to subdirectory if provided
if [ -n "$1" ] && [ -d "$1" ]; then
cd "$1" || return
fi
# Find directories and use fzf for selection
local dir
dir=$(find . -type d | sed '1d; s|^\./||' | fzf --preview 'tree -C {} | head -50')
# Change to selected directory if one was chosen
if [ -n "$dir" ]; then
cd "$dir" || return
else
echo "No directory selected. Staying in current directory."
fi
}

32
functions/fco.sh Executable file
View File

@@ -0,0 +1,32 @@
# fco - checkout git branch/tag
fco() {
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Not in a git repository"
return 1
fi
local tags branches target
# Get tags
tags=$(git tag | awk '{print "\033[31;1mtag\033[m\t" $1}') || return
# Get branches
branches=$(git branch --all | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
sort -u | awk '{print "\033[34;1mbranch\033[m\t" $1}') || return
# Combine tags and branches
target=$( (echo "$tags"; echo "$branches") |
fzf-tmux --no-hscroll --ansi +m -d "\t" -n 2 \
--preview 'git log -n 50 --color=always --date=short --pretty="format:%C(auto)%cd %h%d %s" $(echo {} | awk "{print \$2}")' \
--preview-window right:60%) || return
# Extract the branch or tag name and checkout
local branch_or_tag
branch_or_tag=$(echo "$target" | awk '{print $2}')
echo "Checking out: $branch_or_tag"
git checkout "$branch_or_tag"
}

28
functions/fcoc.sh Executable file
View File

@@ -0,0 +1,28 @@
# fcoc - checkout git commit
fcoc() {
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Not in a git repository"
return 1
fi
local commits commit
# Get commits
commits=$(git log --pretty=oneline --abbrev-commit --reverse) || return
# Use fzf to select a commit
commit=$(echo "$commits" | fzf --tac +s +m -e --preview 'git show --color=always {1}') || return
# Extract the commit hash
commit_hash=$(echo "$commit" | awk '{print $1}')
# Checkout the selected commit
if [ -n "$commit_hash" ]; then
echo "Checking out commit: $commit_hash"
git checkout "$commit_hash"
else
echo "No commit selected."
fi
}

27
functions/fdr.sh Executable file
View File

@@ -0,0 +1,27 @@
# fdr - cd to selected parent directory
fdr() {
get_parent_dirs() {
if [ -d "$1" ]; then
echo "$1"
else
return
fi
if [ "$1" = "/" ]; then
return
else
get_parent_dirs "$(dirname "$1")"
fi
}
# Use command substitution to get the selected directory
DIR=$(get_parent_dirs "$(realpath "${1:-$(pwd)}")" | tac | fzf-tmux)
# Change to the selected directory if one was chosen
if [ -n "$DIR" ]; then
cd "$DIR" || return
else
echo "No directory selected."
fi
}

24
functions/fgh.sh Executable file
View File

@@ -0,0 +1,24 @@
# Find in files with ripgrep and fzf and open on that line
# -> Works together with Vim Plugin bogado/file-line
frg() {
pattern="${1:-.}"
file_glob="${2:-*}"
result=$(rg "$pattern" --line-number --glob "$file_glob" | fzf --delimiter : --preview 'bat --style=numbers --color=always --highlight-line {2} {1}' --preview-window '+{2}-/2')
if [ -n "$result" ]; then
file=$(echo "$result" | cut -d: -f1)
line=$(echo "$result" | cut -d: -f2)
if command -v nvim >/dev/null 2>&1; then
nvim "+${line}" "$file"
elif command -v vim >/dev/null 2>&1; then
vim "+${line}" "$file"
else
echo "Neither neovim nor vim is installed."
fi
else
echo "No file selected."
fi
}

25
functions/fkill.sh Executable file
View File

@@ -0,0 +1,25 @@
# fkill - kill process
fkill() {
local pid
local signal="${1:-9}"
local pattern="$2"
if ! [ "$signal" -eq "$signal" ] 2>/dev/null; then
echo "Invalid signal: $signal"
return 1
fi
if [ -n "$pattern" ]; then
pid=$(ps -ef | sed 1d | grep "$pattern" | fzf -m --header='[kill:process]' --preview 'echo {}' --preview-window down:3:wrap | awk '{print $2}')
else
pid=$(ps -ef | sed 1d | fzf -m --header='[kill:process]' --preview 'echo {}' --preview-window down:3:wrap | awk '{print $2}')
fi
if [ -n "$pid" ]; then
echo "Killing processes with PID: $pid"
echo "$pid" | xargs kill "-$signal"
else
echo "No process selected."
fi
}

26
functions/fo.sh Executable file
View File

@@ -0,0 +1,26 @@
# Modified version where you can press
# - CTRL-O to open with `open` command,
# - CTRL-E or Enter key to open with the $EDITOR
fo() {
# Use process substitution to capture fzf output
IFS=$'\n' read -r -d '' key file <<EOF
$(fzf-tmux --query="$1" --exit-0 --expect=ctrl-o,ctrl-e)
EOF
# Check if a file was selected
if [ -n "$file" ]; then
if [ "$key" = "ctrl-o" ]; then
# Check which "open" command to use
if command -v xdg-open > /dev/null 2>&1; then
xdg-open "$file" # For Linux
elif command -v open > /dev/null 2>&1; then
open "$file" # For macOS
else
echo "No suitable 'open' command found."
fi
else
${EDITOR:-vim} "$file"
fi
fi
}

18
functions/fs.sh Executable file
View File

@@ -0,0 +1,18 @@
# Determine size of a file or total size of a directory
fs() {
# Check if 'du' supports the -b option
if du -b /dev/null > /dev/null 2>&1; then
arg="-sbh"
else
arg="-sh"
fi
# If arguments are provided, use them; otherwise, use current directory
if [ $# -gt 0 ]; then
du $arg -- "$@"
else
# Use find to handle hidden files and directories
find . -maxdepth 1 -print0 | xargs -0 du $arg | sort -h
fi
}

25
functions/fshow.sh Normal file
View File

@@ -0,0 +1,25 @@
# fshow - git commit browser
fshow() {
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Not in a git repository"
return 1
fi
# Define the git log format
local log_format="%C(auto)%h%d %s %C(black)%C(bold)%cr"
# Use git log to get the commit history and pipe it to fzf
git log --graph --color=always --format="$log_format" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index \
--bind=ctrl-s:toggle-sort \
--bind=ctrl-d:preview-page-down \
--bind=ctrl-u:preview-page-up \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always' \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7,\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}

28
functions/fz.sh Normal file
View File

@@ -0,0 +1,28 @@
# Search z history with fzf
fz() {
# Check if z is installed
if ! command -v _z >/dev/null 2>&1; then
echo "Error: z is not installed or not in PATH"
return 1
fi
# If arguments are provided, use z directly
if [ $# -gt 0 ]; then
_z "$*" && return
fi
# Use fzf to select from z history
local dir
dir=$(_z -l 2>&1 | sed 's/^[0-9,.]* *//' |
fzf --height 40% --nth 1.. --reverse --inline-info +s --tac --query "${*##-* }" \
--preview 'ls -l {}' \
--preview-window right:50% \
--bind 'ctrl-/:change-preview-window(down|hidden|)' \
--header 'Press CTRL-/ to toggle preview window')
# Change to the selected directory
if [ -n "$dir" ]; then
cd "$dir" || return 1
fi
}

18
functions/ghpr.sh Executable file
View File

@@ -0,0 +1,18 @@
# Search and preview GitHub pull requests
ghpr() {
# Force GitHub CLI to use colors
export GH_FORCE_TTY=100%
# List pull requests and pipe to fzf for selection
selected_pr=$(gh pr list | fzf --ansi --preview 'GH_FORCE_TTY=100% gh pr view {1}' --preview-window down --header-lines 3)
# Check if a PR was selected
if [ -n "$selected_pr" ]; then
# Extract the PR number and checkout
pr_number=$(echo "$selected_pr" | awk '{print $1}')
gh pr checkout "$pr_number"
else
echo "No pull request selected."
fi
}

11
functions/server.sh Executable file
View File

@@ -0,0 +1,11 @@
# Server
server() {
if command -v browser-sync >/dev/null 2>&1; then
echo "Starting Browser-Sync server..."
browser-sync start --server --files "${1:-**}" "${@:2}"
else
echo "Error: browser-sync is not installed or not in the PATH."
echo "Please install it using npm: npm install -g browser-sync"
fi
}