dotfiles/.bash_aliases

307 lines
7.7 KiB
Bash

# I'm lazy
alias e="exit"
alias n="nano"
# Change PHP CLI binary
alias phpa="sudo update-alternatives --config php"
# Restart all code-server processes. Sometimes it hangs on first startup
alias restart_vscode="sudo systemctl restart code-server@vlw.service"
# Force undo and unstage the last commit from git and git remote
alias gitundo="git reset HEAD^ && git push origin +HEAD"
# +-------------------+
# | Directory & Files |
# +-------------------+
# Preview contents of a file or list the contents of a directory
l () {
# Set target to argument or default to current working directory
local target="${1:-$(pwd)}"
if [ -f $target ] ; then
# Target is not a text file
if [ -L $target ] || grep -q -P "[^\x00-\x7F]" $target; then
ls -lah $target
# Target is not a symlink, so it must be binary
if [ ! -L $target ]; then
echo ""
echo -e "\033[96mBinary file. Run 'll $target' to preview anyways.\e[0m"
fi
return
fi
less $target
return
fi
# List current directory contents and bail out if target does not exist
if [ ! -d $target ]; then
l $(dirname $target)
echo ""
echo -e "\e[0;91mNo such file or directory '$target'\e[0m"
return 1
fi
# Current directory is the user home directory
if [ $target == "$HOME" ]; then
# Ignore these files when running the "l" command from the home directory
local BLACKLIST=("Makefile")
local pattern
local pattern_ignore
for pattern in "${BLACKLIST[@]}"; do
pattern_ignore+="-I \"$pattern\" "
done
eval ls -lh $pattern_ignore
return 0
fi
ls -lh $target
}
# Preview the contents of a file or list the contents of a directory with hidden files shown
ll () {
# Set target to argument or default to current working directory
local target="${1:-$(pwd)}"
if [ -f $target ] ; then
less $target
return
fi
# List current directory contents and bail out if target does not exist
if [ ! -d $target ]; then
ll $(dirname $target)
echo ""
echo -e "\e[0;91mNo such file or directory '$target'\e[0m"
return 1
fi
ls -lAh $target
}
# Edit the contents of a file or list the contents of a directory (piped to less, with hidden files shown) if that file is not found
lll () {
# Set target to argument or default to current working directory
local target="${1:-$(pwd)}"
if [ -f $target ] ; then
nano $target
return
fi
# List current directory contents and bail out if target does not exist
if [ ! -d $target ]; then
lll $(dirname $target)
echo ""
echo -e "\e[0;91mNo such file or directory '$target'\e[0m"
return 1
fi
ls -lah --color=always $target | less -R
}
.. () {
# Count the number of dots in the first argument
local count=$(echo "$1" | grep -o "\." | wc -l)
# Check if count is greater than zero
if [ "$count" -gt 0 ]; then
# Construct the path to go up n directories
local path="$(printf '../%.0s' $(seq 1 $count))"
# Change the directory
cd $path || echo "Failed to change directory."
l
return
fi
cd .. && l
}
cdl () {
# Cdl to home if no args provided
if [ $# -eq 0 ]; then
cdl $HOME
return
fi
cd -P -- "$1" && l
}
cdll () {
# Cdll to home if no args provided
if [ $# -eq 0 ]; then
cdll $HOME
return
fi
cd -P -- "$1" && ll
}
# Make a new directory and cd into it, also list contents in case it already exists
mkd () {
mkdir -p -- "$1" && cd -P -- "$1" && l
}
# Swap the location of a file or folder with another file or folder
swp () {
if [ "$#" -ne 2 ]; then
echo -e "\e[0;91mExpected two arguments: swp <from> <to>\e[0m"
return 1
fi
if [ ! -e $1 ]; then
echo -e "\e[0;91m'$1' is not a file or folder\e[0m"
return 1
fi
tmp_suffix=$RANDOM
mv $1 "$1.$tmp_suffix"
mv $2 $1
mv "$1.$tmp_suffix" $2
}
# +-----+
# | Git |
# +-----+
# Short-hands for various git functions
gitf () {
case "$1" in
# Stage and list changes to be committed
"a")
git add $2
gitf
;;
# Checkout a git branch (defaults to "master")
"b")
local branch="${2:-master}"
if ! git show-ref --verify --quiet refs/heads/"$branch"; then
gitf
echo ""
echo -e "\e[0;91mNo such branch '$branch'\e[0m"
return 0
fi
git checkout $branch
gitf
;;
# Checkout [and create] a git branch (defaults to "master")
"cb")
local branch="${2:-master}"
# Checkout existing branch
if git show-ref --verify --quiet refs/heads/"$branch"; then
gitf b $branch
return 0
fi
git checkout -b $branch
gitf b $branch
;;
# Pull current branch from a remote (defaults to "origin"). "d" for download.
"d")
local remote="${2:-origin}"
git pull "$remote" $(git rev-parse --abbrev-ref HEAD)
git submodule update --recursive
# Delete local branches that used to be tracked on the remote but are now gone (deleted from remote)
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
;;
# Update all git submodules in this repo. "sd" for submodule download.
"sd")
# Only supports "master" as branch for now. This could cause unexpected results if a tracked submodule has a different branch or location name
git submodule foreach "(git checkout master; git pull)"
;;
# Push commited changes (current branch) to a remote (defaults to "origin"). "u" for upload.
"u")
local remote="${2:-origin}"
git push --set-upstream "$remote" $(git rev-parse --abbrev-ref HEAD)
;;
# Create a git "Work in Progress" commit with timestamps
"w")
git commit -m "wip: $(date +%Y-%m-%dT%H:%M:%S%z) ($(date +%s))"
;;
*)
git remote -v
echo ""
git branch -v
echo ""
git status
;;
esac
}
# +-----+
# | SSH |
# +-----+
# SSH to machine in debug mode at local.vlw.se
sshl () {
# Set login name from first argument or default to current user
local user="${1:-$(whoami)}"
ssh -p 2222 "$user"@local.vlw.se
}
# +------+
# | Tmux |
# +------+
# Tmux split commands, "q" for fast access, "w" for horizontal (right of q), "a" for vertical (below q)
alias qw="tmux split-window -h"
alias qa="tmux split-window -v"
# Create or attach tmux sessions
tmx () {
# A session name is provided
if [[ -n "$1" ]]; then
# Attatch to existing named session
if tmux has-session -t "$1" 2>/dev/null; then
tmux attach -t "$1"
return
fi
# Create a new named session
tmux new-session -s "$1" -d
return
fi
local sessions=$(tmux list-sessions -F "#S" 2>/dev/null)
local session_count=$(echo "$sessions" | wc -l)
# Create a new session if none exists
if [ -z "$sessions" ]; then
tmux new-session -s default
return
fi
# Attach to the first existing session
if [ "$session_count" -eq 1 ]; then
tmux attach-session -d -t $(tmux ls | head -n 1 | awk '{print $1}' | tr -d :)
return
fi
echo "$sessions"
echo ""
echo "Multiple sessions exist"
}