dotfiles/bash/.bash_functions

87 lines
2.1 KiB
Bash

# Kill process by port
killport () { lsof -ti ":$1" | xargs kill -9; }
# Rename tmux window after ssh session
ssh() {
tmux rename-window "$*"
command ssh "$@"
exit
}
# Creates a new directory and immediately changes into it
mkcd() {
mkdir -p "$1" && cd "$1"
}
# Extract compressed files easily
extract() {
if [ -f "$1" ] ; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.tar.xz) tar Jxvf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.tbz2) tar xvjf "$1" ;;
*.tgz) tar xvzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Show your current public IP address to the clipboard
showip() {
curl -s http://ifconfig.me ; echo "" #| tr -d '\n' | pbcopy
# echo "Public IP Address copied to clipboard!"
}
# Get the weather forecast for a given location.
weather() {
if [ -n "$1" ]; then
curl v2.wttr.in/"$1"
else
curl v2.wttr.in
fi
}
# Fetch common .gitignore templates and append them to the current .gitignore file
gitignore() {
curl -sL "https://www.gitignore.io/api/$1" >> .gitignore
}
# A quick way to search for a file in the current directory.
qfind() {
find . -name "$1"
}
# Go up a specified number of directories (or one level if no argument provided)
up() {
local p=""
for ((i=1; i<=$1; i++)); do
p="../$p"
done
cd "$p" || return
}
# Get the current timestamp (useful for creating timestamped files)
timestamp() {
date +%Y-%m-%d_%H-%M-%S
}
# Create a backup of a file or directory with a timestamp appended to its name
backup() {
cp -r "$1" "$1_$(timestamp)"
}
# URL-encode a given string (useful for creating encoded URLs in scripts)
urlencode() {
echo -n "$1" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g'
}