Useful Alias for ZSH
Tags: productivity, zsh, tutorial, terminal
Published: 2021-02-23 14:51:07

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](https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins), I've defined a lot more that I miss. Hope can also be useful for you.

---

# Terminal

```bash
alias c="clear"
alias x="exit"
alias e="code -n ~/ ~/.zshrc ~/.aliases ~/.colors ~/.hooks"
alias r="source ~/.zshrc"
```

# History

```bash
alias h="history -10" # last 10 history commands
alias hc="history -c" # clear history
alias hg="history | grep " # +command
```

![history-grep](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj4z1v0r02qqlzudnowi.png)

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

```bash
alias ag="alias | grep " # +command
```

![alias-grep](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7gg0dpbc50fsl4fdpzd.png)

# Utils

```bash
# 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"
```

# Git

```bash
alias gcg="git config --edit --global"
alias gcl="git config --edit --local"
```

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

```bash
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
```

Or use a command validation to confirm dangerous actions.

{% post equiman/command-validations-with-zsh-2boa %} 

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.

```bash
alias gcu="git config user.name \"equiman\" && git config user.email \"equiman@users.noreply.github.com\""
```
{% post equiman/zsh-cheatsheet-for-git-plugin-1f6a %}

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

```bash
alias gcae="git commit --allow-empty -m " # <message>
```

# npm

```bash
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
```

# VSCode

```bash
alias vc="code"
alias vco="code ."
alias vcp="vsce package"
```

# Volta (NVM replacement)

```bash
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"
```

# Image Optimizer

```bash
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
```

---

You can download or clone this code and other ZSH utilities from GitHub: [dot Files](https://github.com/deinsoftware/dot-files) repository.

---

**That’s All Folks!**
**Happy Coding** 🖖

[![ko-fi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrnhg4dyr5nymk11ynvk.png)](https://ko-fi.com/equiman)