308 lines
7.6 KiB
Bash
308 lines
7.6 KiB
Bash
### Andrew's .bashrc
|
|
|
|
|
|
|
|
# _ _
|
|
# ___ _ ____ _(_)_ __ ___ _ __ _ __ ___ ___ _ __ | |_
|
|
# / _ \ '_ \ \ / / | '__/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __|
|
|
# | __/ | | \ V /| | | | (_) | | | | | | | | | __/ | | | |_
|
|
# \___|_| |_|\_/ |_|_| \___/|_| |_|_| |_| |_|\___|_| |_|\__|
|
|
|
|
|
|
# Set some defaults.
|
|
export EDITOR="nvim"
|
|
export VISUAL="nvim"
|
|
export ALTERNATE_EDITOR=""
|
|
export PATH=".:~/bin:~/.local/bin:/usr/games:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin"
|
|
export LSCOLORS="ExGxBxDxCxEgEdxbxgxcxd"
|
|
|
|
# include doom in path if it exists
|
|
if [ -d ~/.config/emacs/bin ]; then
|
|
export PATH="$HOME/.emacs.d/bin:$PATH"
|
|
fi
|
|
|
|
# checks (stolen from zshuery)
|
|
if [[ $(uname) = 'Linux' ]]; then
|
|
IS_LINUX=1
|
|
fi
|
|
|
|
if [[ $(uname) = 'Darwin' ]]; then
|
|
IS_MAC=1
|
|
fi
|
|
|
|
if [[ -x `which brew >/dev/null 2>&1` ]]; then
|
|
HAS_BREW=1
|
|
fi
|
|
|
|
if [[ -x `which apt-get >/dev/null 2>&1` ]]; then
|
|
HAS_APT=1
|
|
fi
|
|
|
|
|
|
# _
|
|
# _ __ _ __ ___ _ __ ___ _ __ | |_
|
|
# | '_ \| '__/ _ \| '_ ` _ \| '_ \| __|
|
|
# | |_) | | | (_) | | | | | | |_) | |_
|
|
# | .__/|_| \___/|_| |_| |_| .__/ \__|
|
|
# |_| |_|
|
|
|
|
|
|
# define useful aliases for color codes
|
|
sh_norm="\[\033[0m\]"
|
|
sh_black="\[\033[0;30m\]"
|
|
sh_dark_gray="\[\033[1;30m\]"
|
|
sh_blue="\[\033[0;34m\]"
|
|
sh_light_blue="\[\033[1;34m\]"
|
|
sh_green="\[\033[0;32m\]"
|
|
sh_light_green="\[\033[1;32m\]"
|
|
sh_cyan="\[\033[0;36m\]"
|
|
sh_light_cyan="\[\033[1;36m\]"
|
|
sh_red="\[\033[0;31m\]"
|
|
sh_light_red="\[\033[1;31m\]"
|
|
sh_purple="\[\033[0;35m\]"
|
|
sh_light_purple="\[\033[1;35m\]"
|
|
sh_brown="\[\033[0;33m\]"
|
|
sh_yellow="\[\033[1;33m\]"
|
|
sh_light_gray="\[\033[0;37m\]"
|
|
sh_white="\[\033[1;37m\]"
|
|
|
|
function git_prompt {
|
|
PCHANGED=""
|
|
PAHEAD=""
|
|
PBEHIND=""
|
|
local FULL_BRANCH="$(git symbolic-ref -q HEAD 2>/dev/null || git name-rev --name-only --no-undefined --always HEAD 2>/dev/null)"
|
|
PBRANCH="$(echo $FULL_BRANCH | sed 's/refs\/heads\///')"
|
|
if [ -n $PBRANCH ]; then
|
|
echo -n "${PBRANCH} "
|
|
# Are there unstaged changes in the working directory
|
|
local CHANGED=$(git status --porcelain --ignore-submodule -unormal 2>/dev/null | wc -l)
|
|
if [ $CHANGED -gt 0 ]; then
|
|
PCHANGED="◊"
|
|
fi
|
|
# Are we behind the origin?
|
|
local NUM_BEHIND="$(git log --oneline ..@{u} 2>/dev/null | wc -l | tr -d ' ' )"
|
|
if [ "$NUM_BEHIND" -gt 0 ]; then
|
|
PBEHIND="↓"
|
|
fi
|
|
# Are we ahead of the origin?
|
|
local NUM_AHEAD="$(git log --oneline @{u}.. 2>/dev/null | wc -l | tr -d ' ')"
|
|
if [ "$NUM_AHEAD" -gt 0 ]; then
|
|
PAHEAD="↑"
|
|
fi
|
|
|
|
if [[ -n $PCHANGED || -n $PAHEAD || -n $PBEHIND ]]; then
|
|
echo -n "${PCHANGED}${PBEHIND}${PAHEAD}"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
|
|
|
|
# Make a pretty prompt.
|
|
export PROMPT_COMMAND='history -a; if [ $? -ne 0 ];then ERROR_FLAG=1;else ERROR_FLAG=;fi;'
|
|
export PS1=${HOSTCOLOUR}${sh_light_gray}'\h'${sh_green}':\w'${sh_light_gray}' $(git_prompt)'${sh_light_gray}'${ERROR_FLAG:+'${sh_light_red}'}\$ '${sh_norm}
|
|
|
|
# New files and folders should not be world readable
|
|
umask 0027
|
|
|
|
# Borrow features from sensible.bash
|
|
|
|
# Trim long paths in prompt
|
|
PROMPT_DIRTRIM=2
|
|
|
|
# Update window size after every command
|
|
shopt -s checkwinsize
|
|
|
|
|
|
|
|
|
|
|
|
# _ _ _
|
|
# | |__ (_)___| |_ ___ _ __ _ _
|
|
# | '_ \| / __| __/ _ \| '__| | | |
|
|
# | | | | \__ \ || (_) | | | |_| |
|
|
# |_| |_|_|___/\__\___/|_| \__, |
|
|
# |___/
|
|
|
|
# Append history don't overwrite
|
|
shopt -s histappend
|
|
|
|
# Save multi-line commands as one line
|
|
shopt -s cmdhist
|
|
|
|
# Avoid duplicate entries
|
|
export HISTCONTROL="erasedups:ignoreboth"
|
|
|
|
# Use ISO8601 time format for history file.
|
|
export HISTTIMEFORMAT='%F %T '
|
|
|
|
# Big history file
|
|
HISTSIZE=500000
|
|
HISTFILESIZE=100000
|
|
|
|
|
|
|
|
|
|
# _ _
|
|
# __ _| (_) __ _ ___ ___ ___
|
|
# / _` | | |/ _` / __|/ _ \/ __|
|
|
# | (_| | | | (_| \__ \ __/\__ \
|
|
# \__,_|_|_|\__,_|___/\___||___/
|
|
|
|
# jump down the tree.
|
|
alias ..="cd .."
|
|
|
|
# empty the screen (or use C-l)
|
|
alias c="clear"
|
|
|
|
# why wouldn't you want human readable sizes?
|
|
alias df="df -h"
|
|
|
|
# get outta here!
|
|
alias e="exit"
|
|
|
|
# grep shortie
|
|
alias g="grep --color=auto -i "
|
|
|
|
# shorter ls
|
|
alias l="ls -G --color"
|
|
|
|
# list files with details
|
|
alias ll="ls -lh --color"
|
|
|
|
# list all files.
|
|
alias la="ls -alh --color"
|
|
|
|
# List directories.
|
|
alias ld="ls --color -Gd */"
|
|
|
|
# add some color to grep.
|
|
alias grep="grep --color=auto"
|
|
|
|
# make the whole directory tree if required and let us know about it.
|
|
alias mkdir="mkdir -p -v "
|
|
|
|
# don't ping forever.
|
|
#alias ping="ping -c5"
|
|
|
|
# refresh the shell customizations without opening a new one.
|
|
alias refresh="source ~/.bashrc && hash -r"
|
|
|
|
# A python calculator
|
|
alias pc='python -ic "from __future__ import division; from math import *"'
|
|
|
|
# Start nano with line numbering no wrapping and autoindenting
|
|
alias nano='nano -ciw '
|
|
|
|
|
|
# emacsclient
|
|
alias ec="emacsclient -n -c -a emacs"
|
|
|
|
if [[ HAS_GNOME ]]; then
|
|
alias nosleep="gnome-session-inhibit --inhibit-only"
|
|
fi
|
|
|
|
# magic packets to wake up hosts
|
|
alias wake_richie="wakeonlan ec:b1:d7:44:91:40"
|
|
alias wake_ari="wakeonlan 3c:97:0e:97:32:84 6c:88:14:36:92:b0"
|
|
alias wake_uzi="wakeonlan f4:d4:88:7d:6a:e9 34:99:71:d8:a4:48"
|
|
|
|
|
|
|
|
# __ _ _
|
|
# / _|_ _ _ __ ___| |_(_) ___ _ __ ___
|
|
# | |_| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
|
|
# | _| |_| | | | | (__| |_| | (_) | | | \__ \
|
|
# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
|
|
|
|
# General purpose extract command.
|
|
extract () {
|
|
if [ -f $1 ] ; then
|
|
case $1 in
|
|
*.tar.bz2) tar xjf $1 ;;
|
|
*.tar.gz) tar xzf $1 ;;
|
|
*.tar.xz) tar xJf $1 ;;
|
|
*.bz2) bunzip2 $1 ;;
|
|
*.rar) rar x $1 ;;
|
|
*.gz) gunzip $1 ;;
|
|
*.tar) tar xf $1 ;;
|
|
*.tbz2) tar xjf $1 ;;
|
|
*.tgz) tar xzf $1 ;;
|
|
*.zip) unzip $1 ;;
|
|
*.Z) uncompress $1 ;;
|
|
*.7z) 7za e $1 ;;
|
|
*.xz) xz -dv $1 ;;
|
|
*) echo "'$1' cannot be extracted via extract()" ;;
|
|
esac
|
|
else
|
|
echo "'$1' is not a valid file"
|
|
fi
|
|
}
|
|
|
|
|
|
# General purpose backup command.
|
|
bu () { tar czf ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.tgz $1; }
|
|
|
|
# create an archive of a list of files, compress the archive, and encrypt the compressed archive
|
|
function archive-and-encrypt {
|
|
echo -n "Enter desired output filename: "
|
|
read filename
|
|
tar cJ $1 | \
|
|
gpg -q -r andrew@amdavidson.com -s -e -o "$filename.tar.xz.gpg"
|
|
}
|
|
|
|
|
|
# shortcut for tmux command
|
|
function tm {
|
|
tmux new-session -A -s main -x $(tput cols) -y $(tput lines)
|
|
}
|
|
|
|
|
|
# run mosh and tmux to a specific host
|
|
function mt {
|
|
mosh $1 -- tmux new-session -A -s main -x $(tput cols) -y $(tput lines)
|
|
}
|
|
|
|
|
|
|
|
|
|
# _ _
|
|
# (_)_ __ ___ _ __ ___ _ __| |_ ___
|
|
# | | '_ ` _ \| '_ \ / _ \| '__| __/ __|
|
|
# | | | | | | | |_) | (_) | | | |_\__ \
|
|
# |_|_| |_| |_| .__/ \___/|_| \__|___/
|
|
# |_|
|
|
|
|
# import fzf if possible
|
|
|
|
# Debian
|
|
if [ -d /usr/share/doc/fzf/examples ]; then
|
|
source /usr/share/doc/fzf/examples/key-bindings.bash
|
|
|
|
if [ -f /usr/share/doc/fzf/examples/completion.bash ]; then
|
|
source /usr/share/doc/fzf/examples/completion.bash
|
|
fi
|
|
fi
|
|
|
|
# Arch
|
|
if [ -d /usr/share/fzf ]; then
|
|
source /usr/share/fzf/key-bindings.bash
|
|
|
|
if [ -f /usr/share/fzf/completion.bash ]; then
|
|
source /usr/share/fzf/completion.bash
|
|
fi
|
|
fi
|
|
|
|
# MacOS
|
|
if [ -d /opt/homebrew/opt/fzf/shell ]; then
|
|
source /opt/homebrew/opt/fzf/shell/key-bindings.bash
|
|
source /opt/homebrew/opt/fzf/shell/completion.bash
|
|
fi
|
|
|
|
|
|
|
|
# Import local extensions if needed.
|
|
if [ -f $HOME/.bashrc_local ]; then
|
|
source $HOME/.bashrc_local
|
|
fi
|
|
|