ENH make aliases more robust

This commit is contained in:
Nathan Dwarshuis 2022-05-20 13:19:50 -04:00
parent 9f95636ba6
commit 8d4bd80276
1 changed files with 106 additions and 73 deletions

View File

@ -161,35 +161,49 @@ export KEYTIMEOUT=1
precmd () { echo -ne "\033]0;urxvt: ${PWD}\007" } precmd () { echo -ne "\033]0;urxvt: ${PWD}\007" }
## --------------------------------------------------
## Alias helper functions
## --------------------------------------------------
exists () {
command -v "$1" &> /dev/null
}
alias_if_N () {
while [ "$1" != "--" ]; do
exists "$1" || return 1
shift
done
shift
for a in "$@"; do
alias "$a"
done
}
alias_if () {
alias_if_N "$1" -- "${@:2}"
}
alias_if_else () {
if ! alias_if_N "$1" -- "$2"; then
alias "$3"
fi
}
alias_if_sudo () {
alias_if_N "$1" -- "${@:2}"
}
## -------------------------------------------------- ## --------------------------------------------------
## Aliases ## Aliases
## -------------------------------------------------- ## --------------------------------------------------
alias als='alias | grep' setopt autopushd
alias hs='history 1 | grep'
alias grep='grep --color=auto' ## ASSUME these will always exist
alias sudo='sudo '
if [ -e /bin/colordiff ]; then
alias diff=colordiff
else
alias diff='diff --color'
fi
alias ls='ls --color' alias ls='ls --color'
if [ -e /bin/exa ]; then
alias ll='exa -alhg --group-directories-first'
alias llt='exa -T'
alias llg='ll --git'
else
alias ll='ls -alh'
[ -e /bin/tree ] && alias llt='tree'
fi
setopt autopushd
alias d='dirs -v | head -10' alias d='dirs -v | head -10'
alias d1='cd -' alias d1='cd -'
alias d2='cd -2' alias d2='cd -2'
@ -207,63 +221,82 @@ alias uuu='cd ../../..'
alias uuuu='cd ../../../..' alias uuuu='cd ../../../..'
alias uuuuu='cd ../../../../..' alias uuuuu='cd ../../../../..'
alias reboot='sudo /sbin/reboot' ## don't assume the rest of these will exist
alias poweroff='sudo /sbin/poweroff'
alias halt='sudo /sbin/halt'
alias shutdown='sudo /sbin/shutdown'
alias sc="sudo systemctl" alias_if grep \
alias sce="sudo systemctl enable" grep='grep --color=auto' \
alias scd="sudo systemctl disable" als='alias | grep' \
alias scs="sudo systemctl start" hs='history 1 | grep'
alias sct="sudo systemctl stop"
alias scr="sudo systemctl restart"
alias sca="systemctl status"
alias scu="systemctl --user" alias_if sudo sudo='sudo '
alias scue="systemctl --user enable"
alias scud="systemctl --user disable"
alias scus="systemctl --user start"
alias scut="systemctl --user stop"
alias scur="systemctl --user restart"
if [ -e /bin/git ]; then alias_if_else colordiff diff=colordiff 'diff --color'
alias g='git'
alias gcl='git clone' if ! alias_if exa \
alias ga='git add' ll='exa -alhg --group-directories-first' \
alias grm='git rm' llt='exa -T' \
alias ga='git add' llg='ll --git'; then
alias gus='git reset HEAD' alias ll='ls -alh'
alias gm="git merge" alias_if tree llt='tree'
alias gmv='git mv'
alias gs='git status -s -b'
alias gp='git push'
alias gpo='git push origin'
alias gpom='git push origin master'
alias gr='git remote'
alias gd='git diff'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gcm='git commit -v -m'
alias gcam="git commit -v -am"
alias gci='git commit --interactive'
alias gb='git branch'
alias gco='git checkout'
alias gg="git log --graph --pretty=format:'%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%C(cyan)\\ [%cn]' --abbrev-commit --date=relative"
alias ggs="gg --stat"
alias gw="git whatchanged"
alias gt="git tag"
alias gnew="git log HEAD@{1}..HEAD@{0}"
alias ggui="git gui"
fi fi
if [ -e /bin/chezmoi ]; then alias_if_sudo reboot reboot='sudo reboot'
alias cze="chezmoi edit" alias_if_sudo poweroff poweroff='sudo poweroff'
alias czea="chezmoi edit --apply" alias_if_sudo halt halt='sudo halt'
alias czd="chezmoi diff" alias_if_sudo shutdown shutdown='sudo shutdown'
alias cza="chezmoi add"
alias czc="chezmoi cd" alias_if_sudo systemctl \
fi sc="sudo systemctl" \
sce="sudo systemctl enable" \
scd="sudo systemctl disable" \
scs="sudo systemctl start" \
sct="sudo systemctl stop" \
scr="sudo systemctl restart"
alias_if systemctl \
sca="systemctl status" \
scu="systemctl --user" \
scue="systemctl --user enable" \
scud="systemctl --user disable" \
scus="systemctl --user start" \
scut="systemctl --user stop" \
scur="systemctl --user restart"
alias_if git \
g='git' \
gcl='git clone' \
ga='git add' \
grm='git rm' \
ga='git add' \
gus='git reset HEAD' \
gm="git merge" \
gmv='git mv' \
gs='git status -s -b' \
gp='git push' \
gpo='git push origin' \
gpom='git push origin master' \
gr='git remote' \
gd='git diff' \
gc='git commit -v' \
gca='git commit -v -a' \
gcm='git commit -v -m' \
gcam="git commit -v -am" \
gci='git commit --interactive' \
gb='git branch' \
gco='git checkout' \
gg="git log --graph --pretty=format:'%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%C(cyan)\\ [%cn]' --abbrev-commit --date=relative" \
ggs="gg --stat" \
gw="git whatchanged" \
gt="git tag" \
gnew="git log HEAD@{1}..HEAD@{0}" \
ggui="git gui"
alias_if chezmoi \
cze="chezmoi edit" \
czea="chezmoi edit --apply" \
czd="chezmoi diff" \
cza="chezmoi add" \
czc="chezmoi cd"
## -------------------------------------------------- ## --------------------------------------------------
## Manly Colors ## Manly Colors