zsh:alias: Improve handling of unknown commands

On (new) systems where commands are still missing, it is often a little
surprise when commands do not work after zsh-syntax-highlighting colored
them green. That's why I do not want alias to be created when they just
wrap a command with the same name without the command existing.

There also cases like `rm` where I do not want to type out `command rm`
every time just because `trash` is not installed.
This commit is contained in:
2021-07-22 15:25:46 +02:00
parent fddbb1117d
commit fc2d811856

View File

@@ -1,22 +1,37 @@
## Author: druckdev ## Author: druckdev
## Created: 2019-01-16 ## Created: 2019-01-16
# Helper functions for this file. Are `unfunction`ed at the end.
is_exec() {
(( $# > 0 )) || return 1
(( $+commands[$1] || $+aliases[$1] || $+functions[$1] ))
}
add_flags() {
(( $# >= 2 )) || (( $+commands[$1])) || return 0
alias "$1"="$*"
}
# Default flags # Default flags
alias ls='ls-show-hidden --color=auto --group-directories-first -p -v' (( ! $+functions[ls-show-hidden] )) ||
alias grep='grep --color' alias ls='ls-show-hidden --color=auto --group-directories-first -p -v'
alias cp='cp -i' add_flags grep --color
alias mv='mv -i' add_flags cp -i
alias rm='rm -I' add_flags mv -i
alias less='less -N' add_flags rm -I
alias lsblk='lsblk -o NAME,LABEL,FSTYPE,SIZE,FSAVAIL,MOUNTPOINT' add_flags less -N
alias feh='feh -.' add_flags lsblk -o NAME,LABEL,FSTYPE,SIZE,FSAVAIL,MOUNTPOINT
add_flags feh -.
# Use multiple jobs when making
add_flags make -j
# XDG Base Directory Specification # XDG Base Directory Specification
alias tmux='tmux -f "${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf"' add_flags tmux -f "${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf"
alias tmsu='tmsu -D "${XDG_DATA_HOME:-$HOME/.local/share}/tmsu/db"' add_flags tmsu -D "${XDG_DATA_HOME:-$HOME/.local/share}/tmsu/db"
alias yarn='yarn --use-yarnrc "${XDG_CONFIG_HOME:-$HOME/.config}"/yarn/config' add_flags yarn --use-yarnrc "${XDG_CONFIG_HOME:-$HOME/.config}"/yarn/config
alias bash='bash --rcfile "${XDG_CONFIG_HOME:-$HOME/.config}"/bash/bashrc' add_flags bash --rcfile "${XDG_CONFIG_HOME:-$HOME/.config}"/bash/bashrc
alias mbsync='mbsync -c "$XDG_CONFIG_HOME"/isync/mbsyncrc' add_flags mbsync -c "$XDG_CONFIG_HOME"/isync/mbsyncrc
# Global # Global
alias -g G='| grep' alias -g G='| grep'
@@ -62,25 +77,31 @@
alias ll='l -A' alias ll='l -A'
alias cd..='cd ..' alias cd..='cd ..'
alias cl='() { cd "$@" && ls }' alias cl='() { cd "$@" && ls }'
alias pdf='zathura --fork &>/dev/null' (( ! $+commands[zathura] )) || alias pdf='zathura --fork &>/dev/null'
alias geeqie='launch qeeqie' (( ! $+commands[geeqie] )) || alias geeqie='launch qeeqie'
alias rd='rmdir' alias rd='rmdir'
alias md='mkdir -p' alias md='mkdir -p'
alias o='xdg-open' alias o='xdg-open'
alias :{q,Q}='exit' alias :{q,Q}='exit'
alias pdf2t{e,}xt='pdftotext' alias pdf2t{e,}xt='pdftotext'
alias rm='printf "\033[1;031mUse trash!\n\033[0m"; false' (( ! $+commands[trash] )) ||
alias rm='printf "\033[1;031mUse trash!\n\033[0m"; false'
alias battery='cat /sys/class/power_supply/BAT0/capacity' alias battery='cat /sys/class/power_supply/BAT0/capacity'
alias loadhist='fc -RI' alias loadhist='fc -RI'
alias hex='xxd' if (( $+commands[xxd] )); then
alias bin='xxd -b -c4 | cut -d" " -f2-5' alias hex='xxd'
alias bin='xxd -b -c4 | cut -d" " -f2-5'
fi
if (( $+commands[nvim] )); then if (( $+commands[nvim] )); then
alias vim='jobs | grep -q nvim && {fg;:;} || nvim' alias vim='jobs | grep -q nvim && {fg;:;} || nvim'
alias vimdiff='nvim --cmd "set list" -c "set listchars=tab:>·,space:·" -d' alias vimdiff='nvim --cmd "set list" -c "set listchars=tab:>·,space:·" -d'
fi fi
alias v='vim' ! is_exec vim ||
alias vi='vim' alias vi='vim'
alias man='nvim-man' ! is_exec vi ||
alias v='vi'
(( ! $+commands[man] )) ||
alias man='nvim-man'
alias resetCursor='echo -ne "\e[5 q"' alias resetCursor='echo -ne "\e[5 q"'
alias makeThisScratchpad='echo -ne "\033]0;scratchpad-terminal\007"' alias makeThisScratchpad='echo -ne "\033]0;scratchpad-terminal\007"'
# grep filenames and date entries in exiftool # grep filenames and date entries in exiftool
@@ -119,8 +140,6 @@
)" gpg' )" gpg'
# Use a reasonable time format # Use a reasonable time format
alias date='env LC_TIME=tk_TM date' alias date='env LC_TIME=tk_TM date'
# Use multiple jobs when making
alias make='make -j'
# Named directories # Named directories
for dir in "$HOME"/[^.]*(/); do for dir in "$HOME"/[^.]*(/); do
@@ -147,3 +166,5 @@
hash -d sose="$(echo ~uni/[0-9][0-9]-SoSe(NnOn[1]))" hash -d sose="$(echo ~uni/[0-9][0-9]-SoSe(NnOn[1]))"
hash -d wise="$(echo ~uni/[0-9][0-9]-WiSe(NnOn[1]))" hash -d wise="$(echo ~uni/[0-9][0-9]-WiSe(NnOn[1]))"
fi fi
unfunction add_flags is_exec