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 can also 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 setting 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

Empty commit, very useful to re-run CI/CD

alias gcae="git commit --allow-empty -m " # <message>
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 vco="code ."
alias vcp="vsce package"
Enter fullscreen mode Exit fullscreen mode

Volta (NVM replacement)

alias vil="volta install node@lts"
alias vpl="volta pin node@16.17.1 && volta pin npm@8.15.0"
alias vi18="volta install node@18"
alias vp18="volta pin node@18.10.0 && volta pin npm@8.19.2"
alias vla="volta list all"
alias vln="volta list node"
alias vlc="volta list node --current --format plain"
alias vld="volta list node --default --format plain"
alias vlp="volta list npm"
alias vcv="node -v && npm -v"
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: dot Files repository.


That’s All Folks!
Happy Coding 🖖

ko-fi

Top comments (1)

Collapse
 
dasginger profile image
DasGinger

Cheers! Thanks for the quick aliases.