Skip to content

Instantly share code, notes, and snippets.

@neitsab
Last active August 9, 2024 14:23
Show Gist options
  • Select an option

  • Save neitsab/8c4b67444618276ab99c7c748350d8d9 to your computer and use it in GitHub Desktop.

Select an option

Save neitsab/8c4b67444618276ab99c7c748350d8d9 to your computer and use it in GitHub Desktop.
Personal aliases used across distributions
## systemd
# Test whether sudo exists and who's the calling user
if command -v sudo >/dev/null 2>&1; then
[[ "$EUID" -ne 0 ]] && alias sctl='sudo systemctl' || alias sctl='systemctl'
else
alias sctl='systemctl'
fi
alias enable='sctl enable'
alias disable='sctl disable'
alias start='sctl start'
alias stop='sctl stop'
alias restart='sctl restart'
alias reload='sctl reload'
alias status='systemctl status'
alias sreboot='sctl reboot'
alias poweroff='sctl poweroff'
alias jctl='journalctl -o short-iso'
alias uctl='systemctl --user'
## general CLI
alias ls='ls --color=auto'
alias ll='ls -lah --time-style=long-iso'
alias grep='grep -i --color=auto'
alias diff='diff -y --suppress-common-lines'
alias df='df -hT -x devtmpfs -x tmpfs'
alias lsblk='lsblk -o NAME,TYPE,SIZE,LABEL,FSTYPE,MOUNTPOINT'
alias cp='cp --reflink=auto'
alias ipa='ip -c -br a'
alias ..='cd ..'
alias ...='cd ../../'
if command -v micro >/dev/null; then
alias nano='micro'
fi
### git
alias gss='git status'
alias gc='git commit'
alias gp='git push'
alias gpu='git pull'
alias glog='git log --decorate --pretty=oneline --abbrev-commit'
alias grog='glog --graph'
alias gdif='git diff'
## ssh
alias ssh-forget='ssh -o UserKnownHostsFile=/dev/null'
alias ssh-forget-pass='ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no -o UserKnownHostsFile=/dev/null'
alias ssh-old='ssh -o PubkeyAcceptedAlgorithms=+ssh-rsa -o HostkeyAlgorithms=+ssh-rsa'
## rsync-perfect
if command -v sudo >/dev/null 2>&1; then
[[ "$EUID" -ne 0 ]] && alias rsync-perfect='sudo rsync -aHAXShUU --info=progress2 --partial' || alias rsync-perfect='rsync -aHAXShUU --info=progress2 --partial'
else
[[ "$EUID" -ne 0 ]] && alias rsync-perfect="$(echo This alias won\'t work as a regular user without sudo)" || alias rsync-perfect='rsync -aHAXShUU --info=progress2 --partial'
fi
## general CLI
alias ls='ls --color=auto'
alias ll='ls -lah --time-style=long-iso'
alias grep='grep -i --color=auto'
alias diff='diff -y --suppress-common-lines'
alias df='df -hT -x devtmpfs -x tmpfs'
alias lsblk='lsblk -o NAME,TYPE,SIZE,LABEL,FSTYPE,MOUNTPOINT'
alias cp='cp --reflink=auto'
alias ipa='ip -c -br a'
alias ..='cd ..'
alias ...='cd ../../'
## systemd
alias sctl='systemctl'
alias uctl='systemctl --user'
alias enable='sctl enable'
alias disable='sctl disable'
alias status='systemctl status'
alias start='sctl start'
alias stop='sctl stop'
alias restart='sctl restart'
alias reload='sctl reload'
alias sreboot='sctl reboot'
alias poweroff='sctl poweroff'
alias jctl='journalctl -o short-iso'
### git
alias gss='git status'
alias gc='git commit'
alias gp='git push'
alias gpu='git pull'
alias glog='git log --decorate --pretty=oneline --abbrev-commit'
alias grog='glog --graph'
alias gdif='git diff'
## ssh / rsync
alias ssh-forget='ssh -o UserKnownHostsFile=/dev/null'
alias ssh-forget-pass='ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no -o UserKnownHostsFile=/dev/null'
alias ssh-old='ssh -o PubkeyAcceptedAlgorithms=+ssh-rsa -o HostkeyAlgorithms=+ssh-rsa'
alias rsync-perfect='rsync -aHAXShUU --info=progress2 --partial'
## package management
if command -v nala &>/dev/null; then
pm="nala"
else pm="apt"
fi
alias inst="sudo $pm install"
alias upd="sudo $pm update"
alias upg="sudo $pm upgrade" # "upgrade the system by installing/upgrading packages"
alias maj="upd && upg"
alias fup="sudo $pm full-upgrade" # "upgrade the system by removing/installing/upgrading packages"
alias fmaj="upd && fup"
alias remove="sudo $pm remove"
alias purge="remove --autoclean --autopurge"
alias query="$pm search"
alias search="$pm show"
alias pkginfo='dpkg -s'
alias whose='dpkg -S'
## package management
if command -v nala &>/dev/null; then
pm="nala"
else pm="apt"
fi
alias inst="$pm install"
alias upd="$pm update"
alias upg="$pm upgrade" # "upgrade the system by installing/upgrading packages"
alias maj="upd && upg"
alias fup="$pm full-upgrade" # "upgrade the system by removing/installing/upgrading packages"
alias fmaj="upd && fup"
alias remove="$pm remove"
alias purge="remove --autoclean --autopurge"
alias query="$pm search"
alias search="$pm show"
alias pkginfo='dpkg -s'
alias whose='dpkg -S'
## package management
if command -v yay &>/dev/null; then
pm="yay"
else pm="pacman"
fi
alias inst="$pm -S --needed"
alias asdeps='inst --asdeps'
alias maj='inst -yu'
alias majaur='yay -Sua'
alias maj-dev='majaur --devel'
alias remove='sudo pacman -Rs'
alias purge='sudo pacman -Rsn'
alias query="$pm -Ss"
alias search='pacman -Qs'
alias repinfo="$pm -Si"
alias pkginfo='pacman -Qi'
alias whose='pacman -Qo'
alias orphan='pacman -Qdtq'
alias rem_orphan="pacman -Qtdq | sudo pacman -Rns -"
# alias rem_orphan='yay -Yc'
alias cleancache="$pm -Sc"
alias mksrcinfo="makepkg --printsrcinfo > .SRCINFO"
## package management
if command -v yay &>/dev/null; then
pm="yay"
else pm="pacman"
fi
alias inst="$pm -S --needed"
alias asdeps='inst --asdeps'
alias maj='inst -yu'
alias majaur='yay -Sua'
alias maj-dev='majaur --devel'
alias remove='pacman -Rs'
alias purge='pacman -Rsn'
alias query="$pm -Ss"
alias search='pacman -Qs'
alias repinfo="$pm -Si"
alias pkginfo='pacman -Qi'
alias whose='pacman -Qo'
alias orphan='pacman -Qdtq'
alias rem_orphan="pacman -Qtdq | pacman -Rns -"
# alias rem_orphan='yay -Yc'
alias cleancache="$pm -Sc"
alias mksrcinfo="makepkg --printsrcinfo > .SRCINFO"
# https://wiki.archlinux.org/index.php/Zsh#The_.22command_not_found.22_hook
#source /usr/share/doc/pkgfile/command-not-found.zsh
# https://wiki.archlinux.org/index.php/Zsh#Fish-like_syntax_highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
WORDCHARS='~!#$%^&*(){}[]<>?.+;-'
[[ -e ~/.aliases ]] && source ~/.aliases
# Movement settings
# https://unix.stackexchange.com/a/537182
# https://unix.stackexchange.com/a/48589
# https://stackoverflow.com/a/71848062
# https://web.cs.elte.hu/local/texinfo/zsh/zsh_27.html
# Set global Bash-style movement: Ctrl+arrow & Alt+backspace stops at every non-alphanum character
autoload -U select-word-style
select-word-style bash
# But override Alt+arrow bindings to make large blank-defined jumps
bindkey $'\E[1;3D' vi-backward-blank-word
bindkey $'\E[1;3C' vi-forward-blank-word
# And Ctrl+backspace delete up to previous slash
''backward-delete-word() WORDCHARS=$WORDCHARS:s:/: zle .$WIDGET
zle -N backward-delete-word
bindkey '^H' backward-delete-word
# not needed in the current scenario
#zstyle ':zle:forward-word' word-style space
# WORDCHARS='~!#$%^&*(){}[]<>?.+;-'
## Change a few grml config options:
# capitalization and underscore don't matter in options, see
# https://scriptingosx.com/2019/06/moving-to-zsh-part-3-shell-options/
# - make * also match dotfiles
# - do not replace wildcard with all matches but for menu like for usual tab completion
# - try to correct the spelling of all arguments in a line
setopt GLOB_DOTS GLOB_COMPLETE CORRECT_ALL
# not only forward slash should be considered a word separator!!
#unfunction slash-backward-kill-word
function slash-backward-kill-word () {
# local WORDCHARS="${WORDCHARS:s@/@}"
# zle backward-word
zle backward-kill-word
}
zle -N slash-backward-kill-word
# history settings
# see https://www.soberkoder.com/better-zsh-history/
# https://zsh.sourceforge.io/Doc/Release/Options.html#History
# setopt | grep hist
# increase saved history *in the hist file* (session hist remains at 5000 for memory efficiency)
export SAVEHIST=1000000
# try to use something different than Unix epoch in hist file
# (not sure this applies to zsh: https://unix.stackexchange.com/a/103407)
export HISTTIMEFORMAT="[%F %T] "
# grml-zsh-config already sets share_history and extended_history;
# DO NOT SET INC_APPEND_HISTORY WHEN USING SHARE_HISTORY!! https://unix.stackexchange.com/a/651538
# do not ignore commands starting with a space: more often than not it is just a typo
# and then I search for it in vain...
unsetopt HIST_IGNORE_SPACE
# skip duplicates when searching history (but still write them to file)
# and remove superfluous blanks from each command line being added to the history list
setopt HIST_FIND_NO_DUPS HIST_REDUCE_BLANKS
## Autocomplete: command prediction from history
is4 && zrcautoload predict-on && \
zle -N predict-on && \
zle -N predict-off && \
bindkey "^X^Z" predict-on && \
bindkey "^Z" predict-off
## syntax highlighting
# https://wiki.archlinux.org/index.php/Zsh#Fish-like_syntax_highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
## Source shell-agnostic aliases from common file (https://askubuntu.com/a/195357)
source $HOME/.aliases
## For env var --> see .zshenv
## Zsh-only functions
function compress { tar -I zstd -cf "$1.tar.zst" "$@" }
function decompress { tar -I zstd -xf "$@" }
function mpva { mpv --no-video --ytdl-format=bestaudio ytdl://ytsearch:"$@" }
function shrinkpdf-low { /usr/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=shrunk-"$1" "$1" }
function shrinkpdf-mid { /usr/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=shrunk-"$1" "$1" }
function shrinkpdf-high { /usr/bin/gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=shrunk-"$1" "$1" }
# urlencode() {
# setopt localoptions extendedglob
# input=( ${(s::)1} )
# print ${(j::)input/(#b)([^A-Za-z0-9_.\!~*\'\(\)-])/%${(l:2::0:)$(([##16]#match))}}
# }
# command not found handler
# https://wiki.archlinux.org/index.php/Zsh#The_%22command_not_found%22_handler
command_not_found_handler() {
local pkgs cmd="$1" files=()
printf 'zsh: command not found: %s' "$cmd" # print command not found asap, then search for packages
files=(${(f)"$(pacman -F --machinereadable -- "/usr/bin/${cmd}")"})
if (( ${#files[@]} )); then
printf '\r%s may be found in the following packages:\n' "$cmd"
local res=() repo package version file
for file in "$files[@]"; do
res=("${(0)file}")
repo="$res[1]"
package="$res[2]"
version="$res[3]"
file="$res[4]"
printf ' %s/%s %s: /%s\n' "$repo" "$package" "$version" "$file"
done
else
printf '\n'
fi
return 127
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment