Zsh Alias Cheat Sheet

Categorizing the different ways aliases can be used.
(Work in Progress)

v0.2 21 Jun 2022 Probably contains errors please email any comments to zzapper@gmail.com

zzapper's ZSH home

alias 101 type 01: command shorteners

The most common type
alias gp='egrep -i'
alias ll='ls -lt'
alias gits='git status'

alias 101 type 02: common jobs

alias lmpg='ls *.(wmv|mpg|mov|avi)' # list any videos
alias phpn='php -l *.php(.om[1])' # syntax check newest

alias 101 type 03: Directly Access a file

alias vini='vi /etc/php/7.4/apache2/php.ini'
alias vip='vi index.php'

alias 101 type 04: Goto Directory

alias dtrash='cd ~/.local/share/Trash/files'
alias dbin='cd /usr/local/bin/'

alias 101 type 05: Alias with a 'parameter' at the end trick

technique by placing the 'parameter' at the end

alias gia='< ~/.zshrc | grep -ia'
gia photo

alias 101 type 06: Alias with parameters (using function)

alias gia='f(){grep -i "$1" ~/.zshrc};f'
gia photo

alias 101 type 06a: Template alias: uses a parameter,manipulates a string then implements it

temporarily turn an alias into a global alias

alias alg='f(){F=$1;eval $(alias -L "$F" | sed "s/alias /alias -g /")};f'

> alg 'll'
alias -g ll='ls -l'

alias 101 type 07: Quoting

crucial difference between single (dynamic) & double quotes (static) for aliases

alias dxd="echo $(date)" # Double quotes
dxd # always the date when alias created
alias dxs='echo $(date)' # Single Quotes
dxs # dynamically re-evaluated

alias 101 aliases type 08 : 'Ansi-C Quoting'

When complex quoting required

When complex quoting required
alias lsimg=$'for f (*.(jpeg|jpg|gif|png)) {printf $f;php -r "print_r(getimagesize(\'$f\'));" |grep -w width |sed \'s/ .3] =>//\'}'

alias 101 aliases type 09 : # alias -s(uffix) associate a file extension with an application

alias -s {odt,doc,docx}='soffice --writer'

alias -s {gif,png,jpg}='xnview'
alias -s pdf='xreader'
Example
then just type file name to open in specified app
cat.jpg  # opens in XnViewMP

alias 101 type 10 : # alias -L(ist) definitions

Just displays the alias definiton

alias -L 'G' 'GV' # note the quotes

 alias -g G=' | grep -iaE' # grep pipe
 alias -g GV=' | grep -ivaE' # negative grep pipe

alias 101 type 11 : Global Aliases used as Filters

Can Appear anywhere on the Command Line

alias -g G=' | grep -i'
 alias -g GV=' | grep -ivaE' # negative grep pipe
alias -g H=' | head '
alias -g M=' | less '
alias -g CO=' | xclip -sel clip'
Example
grep projectxy *.log M
e.g. search for an image
locate bridge G railway GV backup CO

alias 101 type 12a : Global Aliases used as File Selectors

xxxdetailxxx

alias -g TOH=' *(.mh0)' # last hours files
alias -g TOD=' *(.m0)'  # today's files
alias -g TOW=' *(.mw0)' # last week files

alias 101 type 12b : # Global Aliases used as File Selectors

xxxdetailxxx

alias -g NFI='**.(jpg|gif|png|jpeg)(.om[1])' # newest image in hierarchy
alias -g TW='*.(php|css|js)(.m0)' # Todays web files
vi TW  # re-edit todays web files

alias 101 type 12c : # Global Aliases used as Directory Selectors

/ = dir . = file

alias -g ND1='*(/om[1])' # newest dir
alias -g ND2='*(/om[2])' # 2nd newest dir
alias -g NF1='*(.om[1])' # newest file
cp NF ND

alias 101 aliases type 13a: # Generic Aliases : Basic GLOB

xxxdetailxxx

instead of having many different aliases use generic
aliases which you expand and modify as required
 ~ means directories/files to exclude 
alias -g GLOB=' **.(php|inc|js)~(libs|test)/*'
ll GLOB to Expand
Then tweal the glob as required

alias 101 type 14: Syntax Aide Aliases

Basic Templates for complex syntax for commands such as awk & sed

alias -g AWK=$' | awk \'{print  cnt++,$3,$4}\'' # Ansi C quoting
alias -g SED=$' | sed \'/color/ s/red/green/g\''
TAB Expand and then tweak as required

alias 101 type 15: Aliases using History

only work after being TAB expanded

Use an Alias to Access a locate result

alias  LF1='eval ${$(!!)[1]} E'
alias  LF2='eval ${$(!!)[2]} E'
alias  LD1='cd ${$(!!)[1]:h} ' # cd to 1st
alias  LD2='cd ${$(!!)[2]:h} '

alias 101 type 16: Generated Aliases

in your .zshrc

for n ({1..5}) alias -g NF$n="*(.om[$n])"
e.g. this gives you
vi NF4   # edit 4th newest file

alias 101 aliases type 17a: Aide Memoire Aliases

An alias to remind of of a greoup of aliaises e.g. for tmux

alias tmxh="alias -L 'tmx' 'tmxk' 'tmxn' 'tmxc'"
tmxh
alias tmx='tmux new-session \; split-window -v \; split-window -h \;'
alias tmxk='tmux kill-server'
alias tmxn='gvim tmux_notes.txt'
alias tmxc='gvim ~/.tmux.conf '

alias 101 aliases type 17b: # Helper aliases

display complex options

configured to auto-expand

alias -g OLDH='*(.mw+3) # m=minutes,d=days(default), w=week,M=Month +n older than n,-n younger'

alias 101 aliases type 18: Grouping aliases

aliases may include aliaises

alias a1='echo sue'
alias b1='echo tom'
alias c1='a1;b1'
c1
sue
tom

alias 101 type 19: Data Snapshots

xxxdetailxxx

alias sysload='printf "System load (1m/5m/15m): "; for l in 1 2 3 ; do printf "%.1f%s" "$(( $(cat /proc/loadavg | cut -f $l -d " ") * 100 / $(nproc) ))" "% "; done; printf "\n"'
sysload
System load (1m/5m/15m): 16.2% 25.0% 25.2%