Skip to content

Instantly share code, notes, and snippets.

@yuhonas
Forked from uarun/zsh.md
Last active August 15, 2019 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuhonas/f0ef20e7405e67d815fc91cda6149774 to your computer and use it in GitHub Desktop.
Save yuhonas/f0ef20e7405e67d815fc91cda6149774 to your computer and use it in GitHub Desktop.
Zsh Tips & Tricks

Zsh Tips and Tricks

CD'ing to frequently used directories

setopt auto_cd
cdpath=($HOME/<dir1> $HOME/<dir2>)

Now simply from any dir

$ cd dir1

Suffix Aliases

Suffix aliases are supported in zsh since version 4.2.0. Some examples:

alias -s tex=vim
alias -s html=w3m
alias -s org=w3m

Now pressing return-key after entering foobar.tex starts vim with foobar.tex. Calling a html-file runs browser w3m. www.zsh.org and pressing enter starts w3m with argument www.zsh.org. Global aliases can be used anywhere in the command line. Example:

alias -g ...='../..'
alias -g ....='../../..'
alias -g .....='../../../..'
alias -g CA="2>&1 | cat -A"
alias -g C='| wc -l'
alias -g D="DISPLAY=:0.0"
alias -g DN=/dev/null
alias -g ED="export DISPLAY=:0.0"
alias -g EG='|& egrep'
alias -g EH='|& head'
alias -g EL='|& less'
alias -g ELS='|& less -S'
alias -g ETL='|& tail -20'
alias -g ET='|& tail'
alias -g F=' | fmt -'
alias -g G='| egrep'
alias -g H='| head'
alias -g HL='|& head -20'
alias -g Sk="*~(*.bz2|*.gz|*.tgz|*.zip|*.z)"
alias -g LL="2>&1 | less"
alias -g L="| less"
alias -g LS='| less -S'
alias -g MM='| most'
alias -g M='| more'
alias -g NE="2> /dev/null"
alias -g NS='| sort -n'
alias -g NUL="> /dev/null 2>&1"
alias -g PIPE='|'
alias -g R=' > /c/aaa/tee.txt '
alias -g RNS='| sort -nr'
alias -g S='| sort'
alias -g TL='| tail -20'
alias -g T='| tail'
alias -g US='| sort -u'
alias -g VM=/var/log/messages
alias -g X0G='| xargs -0 egrep'
alias -g X0='| xargs -0'
alias -g XG='| xargs egrep'
alias -g X='| xargs'

Argument History

!*        # ... All parameters of the last command
!$        # ... Last parameter of the last command
!^        # ... First parameter of the last command
!:1       # ... First parameter of the last command

!-2:2     # ... Second parameter from 2 commands ago
!:2-3     # ... Previous command's parameters 2 to 3
!:2*      # ... Previous command's parameters 2 onwards
!:2-      # ... Previous command's parameters 2 onwards omitting last

!$:h      # ... Last parameter, strip one level
!$:h:h    # ... Last parameter, strip two levels

!?ls      # ... Last 'ls' command

Directory Substitution

cd ~/projects/sample/src/main/java/com/arun/examples/
cd java scala

The last command will put you in ~/projects/sample/src/main/scala/com/arun/examples/ (Now that's Hot !)

Previous Command Substitution

r apple=orange
!!:s/apple/orange/     # ... Subst previous command replacing first 'apple' with 'orange'
!!:gs/apple/orange/    # ... Subst previous command replacing all occurrences of 'apple' with 'orange'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment