DEV Community

Cover image for Useful Alias for ZSH
Camilo Martinez
Camilo Martinez

Posted on • Updated on

Useful Alias for ZSH

When I start working all the machines in my office use Windows. That's the moment when I start missing the alias ability that I learned at the university where I used to have Linux on my laptop.

Happily now, ZSH (or bash) can be used on Linux, macOS, and Windows (with WSL).

In addition to all the aliases that can be activated with ZSH plugins, I've defined a lot more that I miss. Hope also can be useful for you.


Terminal

alias c="clear"
alias x="exit"
alias e="code -n ~/ ~/.zshrc ~/.aliases ~/.colors ~/.hooks"
alias r="source ~/.zshrc"
Enter fullscreen mode Exit fullscreen mode

History

alias h="history -10" # last 10 history commands
alias hc="history -c" # clear history
alias hg="history | grep " # +command
Enter fullscreen mode Exit fullscreen mode

history-grep

You can reuse the command with !# (! + history number) to reuse the same command. According to previous image, if you use !46 it will recover the npm -v command.

Alias

alias ag="alias | grep " # +command
Enter fullscreen mode Exit fullscreen mode

alias-grep

Utils

# https://github.com/abishekvashok/cmatrix
# sudo apt install cmatrix / brew install cmatrix
alias m="cmatrix -abs"

# https://htop.dev/
# sudo apt install htop / brew install htop
alias t="htop"

# https://dev.yorhel.nl/ncdu
# sudo apt install ncdu / brew install ncdu
alias d="ncdu --exclude /mnt --color dark" # +path

# https://www.speedtest.net/apps/cli
alias st="speedtest"

# https://github.com/sindresorhus/clipboard-cli
# npm install -g clipboard-cli
alias cb="clipboard"
Enter fullscreen mode Exit fullscreen mode

Git

alias gcg="git config --edit --global"
alias gcl="git config --edit --local"
Enter fullscreen mode Exit fullscreen mode

⚠ Use next alias with extra caution, you may lose all your changes.

alias guc="git reset --hard HEAD" # undo changes and preserve untracked files
alias gcc="git clean -f -d -x" # clean ALL changes and remove untracked files
Enter fullscreen mode Exit fullscreen mode

Or use a command validation to confirm dangerous actions.

I'm not set up the global user and mail on git because I work on different projects. So I have an alias with the most used user.

alias gcu="git config user.name \"equiman\" && git config user.email \"equiman@users.noreply.github.com\""
Enter fullscreen mode Exit fullscreen mode

npm

alias rnm="rm -rf node_modules"
alias rbn="rm -rf build node_modules"
alias rap="rm -rf build coverage node_modules package-lock.json && npm i"
alias cap="clean && rap"

alias npk="npx npkill" #clean unused node_modules
alias nkp="npx kill-port " # +portnumber
alias nfk="npx fkill-cli" # +[<pid|name|:port> …] #kill processes

alias nlg="npm list -g --depth 0" #list global packages installed

alias ni="npm i"
alias nis="npm i -S " # +package@version
alias nise="npm i -S -E " # +package@version
alias nid="npm i -D " # +package@version
alias nide="npm i -D -E " # +package@version
alias nr="npm r " # +package@version

alias nrb="npm run build"
alias nrbd="npm run build:dev"
alias nrbq="npm run build:qa"
alias nrs="npm run start"
alias nrsd="npm run start:dev"
alias nrsq="npm run start:qa"
alias nrt="npm run test"
alias nrtc="npm run test:c" #test with coverage

alias np="npm run build && npm publish"
alias nu="npm unpublish " # +package@version
Enter fullscreen mode Exit fullscreen mode

VSCode

alias vc="code"
alias vcp="vsce package"
Enter fullscreen mode Exit fullscreen mode

Volta (NVM replacement)

alias vi14="volta install node@14"
alias vi16="volta install node@lts"
alias vpn="volta pin node@16.13.1 && volta pin npm@8.1.2"
alias vla="volta list all"
alias vln="volta list node"
alias vlp="volta list npm"
alias vcv="node -v && npm -v"
Enter fullscreen mode Exit fullscreen mode

Serverless

alias si="sls info"
alias sd="sls deploy"
alias sdf="sls deploy -f " # +function_name
alias sos="serverless offline start"
Enter fullscreen mode Exit fullscreen mode

Image Optimizer

alias iow="npx @squoosh/cli --webp auto " # +file-name
alias ioa="npx @squoosh/cli --avif auto " # +file-name
alias iop="npx @squoosh/cli --oxipng auto " # +file-name
Enter fullscreen mode Exit fullscreen mode

You can download or clone this code and other ZSH utilities from GitHub: ZSH Dev Toolkit repository.


That’s All Folks!
Happy Coding 🖖

ko-fi

Discussion (1)

Collapse
dasginger profile image
DasGinger

Cheers! Thanks for the quick aliases.