Why Oh My ZSH is so cool?
Tags: productivity, zsh, terminal, tooling
Published: 2023-03-04 00:37:11

There is a before and after listening to these letters together: [ZSH](https://zsh.sourceforge.io/). You may wonder: Why did not I hear about this before? D'oh!

ZSH (Z Shell) it's a real evolution, modernize terminal things with simple solutions. But the thing getting better when you discover [Oh My ZSH!](https://ohmyz.sh/) a framework for ZSH that boost your productivity and improve your workflow.

![cool bro](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hyh668g5cq84dln9myiw.png)

## So, why?

- No more `cd` (change directory) command. Just use:

| Command   | Result                                       |
| --------- | -------------------------------------------- |
| `folder/` | write the folder name with `/` at the end    |
| `..`      | go back one folder (parent dir)              |
| `...`     | go back two folders (parent from parent dir) |
| `......`  | go back five folders                         |
| `/`       | go to root                                   |
| `~`       | go to home                                   |
| `-`       | jump to previous path                        |

- Recursive path expansion. `/u/lo/b` expands to `/user/local/bin`

- Spelling correction and approximate completion, automatic correct when having minor mistake typing a directory name

- `take` the 3 in 1 command who create a directory and automatically change the path to it

| Command           | Result                                                   |
| ----------------- | -------------------------------------------------------- |
| `take <url_file>`| Download gzip file (`.gz`, `.bz2`, `.xz`) and uncompress |
| `take <git_repo>` | Clone git repo from url (`http`, `ssh`)                  |
| `take <folder>`   | Create a new folder                                      |

- `zsh_stats` will give you a list of the top 20 commands and how many times they've been run

- Tab completion is another great feature. For example, typing `ls -` and pressing `TAB` will list all of the command's options, along with a helpful description of what they do

- Alias commands. List all with `alias` or filter it with `grep` for example `alias | grep git`

- Globbing

| Command            | Result                                              |
| ------------------ | --------------------------------------------------- |
| `ls *.txt`         | list all `txt` files in the current directory       |
| `ls **/*.txt`      | list all `txt` files including subdirectories       |
| `ls **/(READ)*.*`  | find for files that start with the word `READ`      |
| `ls **/*(READ).*`  | find for files that end with the word `READ`        |
| `ls **/*(READ)*.*` | find for files that have the word `READ` anywhere   |
| `ls **/*(.)`       | search for files only                               |
| `ls **/*(/)`       | search for folders only                             |

- Expanding File Names and Directories

| Command                                                  | Result                                                                      |
| -------------------------------------------------------- | --------------------------------------------------------------------------- |
| `touch name-{1..4}.txt`                                  | create files `name-1.txt`, `name-2.txt`, `name-3.txt`, `name-4.txt`         |
| `touch name.{css,js,test.js}`                            | create files with different extension `name.css`, `name.js`, `name.test.js` |
| `cp folder/name.js{,.bak}`                               | create a copy of the file ending `.bak`                                     |
| `diff folder/{new,old}/name.js`                          | view differences between two files                                          |
| `mkdir -p {source,build,man,help{/pages,/yelp,/images}}` | create a complete folder structure                                          |
| `wget http://site.com/folder{1,2}/pic{001,002}.jpg`      | download multiple files                                                     |

- Expand environment variables `$ENV` followed by `TAB`

- Expand `kill` command followed by `TAB`

- History substring search writing a command and pressing the up arrow cycles through previous usages

- Autocomplete, jump between options with tab, and press return for selection. Works with directories, files, and commands

- Run history command with `!` followed by the number in history, like `!137`

- Entering `!!` will bring up the last command. This is handy if a command fails because it needs admin rights. In this case you can type `sudo !!`

## Plugins

There are [a lot of plugins](https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins) to use. It's recommended to explore the options and use what is good for your needs.

My recommendations are:
- [sudo](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/sudo) to easily prefix your current or previous commands with sudo by pressing esc twice
- [command-not-found](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/command-not-found) to provide suggested packages to be installed if a command cannot be found
- [extract](https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/extract) that extracts the archive file you pass it, and it supports a wide variety of archive filetypes
- [git](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git) provides many aliases and a few useful functions
- [history-substring-search](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/history-substring-search) a clean-room implementation of the Fish shell's history search feature, where you can type in any part of any command from history and then press chosen keys, such as the UP and DOWN arrows, to cycle through matches
- [web-search](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/web-search) adds aliases for searching with Google, Wiki, Bing, YouTube and other popular services
- [z](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/z) command that tracks your most visited directories and allows you to access them with very few keystrokes

I've another extra from plugins from external repositories to add more functionalities:

- [autosuggestions](https://github.com/zsh-users/zsh-autosuggestions.git) suggests commands as you type based on history and completions.
- [syntax-highlighting](https://github.com/zsh-users/zsh-syntax-highlighting.git) Fish shell-like syntax highlighting for ZSH
- [ohmyzsh-full-autoupdate](https://github.com/Pilaton/OhMyZsh-full-autoupdate) Automatic update of custom plugins and themes Oh My Zsh

## Themes

There are also [a lot of themes](https://github.com/ohmyzsh/ohmyzsh/wiki/Themes), but my favorite is [Powerlevel10k](https://github.com/romkatv/powerlevel10k) because is easy to set up and use

![p10k](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/km2jmrlk34k891qdfvbu.png)

## Git

Incredible and complete aliases for Git

{% post https://dev.to/equiman/zsh-cheatsheet-for-git-plugin-1f6a %}

Git Status Prompt can contain the following bits:

| segment     |  meaning                                              |
|-------------|-------------------------------------------------------|
| `master`    | current branch                                        |
| `#v1`       | HEAD is tagged with `v1`; not shown when on a branch  |
| `@5fc6fca4` | current commit; not shown when on a branch or tag     |
| `⇣1`        | local branch is behind the remote by 1 commit         |
| `⇡2`        | local branch is ahead of the remote by 2 commits      |
| `⇠3`        | local branch is behind the push remote by 3 commits   |
| `⇢4`        | local branch is ahead of the push remote by 4 commits |
| `*5`        | there are 5 stashes                                   |
| `merge`     | merge is in progress (could be some other action)     |
| `~6`        | there are 6 merge conflicts                           |
| `+7`        | there are 7 staged changes                            |
| `!8`        | there are 8 unstaged changes                          |
| `?9`        | there are 9 untracked files                           |

## Shortcuts

### Edit long command

`ctrl+x+e` open the command on an editor to easy edit, once save and close the editor it will be updated on the terminal

You can set your favorite editor, for example run `export EDITOR="code -w"` to use VSCode

### Park a command

`ctrl+q` "parks" the command you're currently typing and takes you back to the prompt, letting you start over and type another command. Once you run that other command, the original command is un-parked and refills the command line so you can continue.

This is good for if you, say, forgot to do a command before a command.

### Reveal alias

`ctrl+x` `a` will transform the alias to the real command

### Path history

ZSH keeps the history of directories you visited so you can quickly switch to any of them.

To see the list, type `dirs -v`. Switch to any directory in this list by typing `~#` where `#` is the number of the directory in the list.

## Hooks

You can personalize actions (before and after a command) with ZSH Hooks

{% post https://dev.to/equiman/reveal-the-command-behind-an-alias-with-zsh-4d96 %}

{% post https://dev.to/equiman/command-validations-with-zsh-2boa %}

---

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

[![beer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)

> **Sources**
> - [Learn the basics of the ZSH shell](https://linuxconfig.org/learn-the-basics-of-the-zsh-shell)
> - [A Guide to Zsh Expansion with Examples](https://thevaluable.dev/zsh-expansion-guide-example/)
> - [How to Use Brace Expansion in Linux's Bash Shell](https://www.howtogeek.com/725657/how-to-use-brace-expansion-in-linuxs-bash-shell/)
> - [75 Zsh Commands, Plugins, Aliases and Tools](https://www.sitepoint.com/zsh-commands-plugins-aliases-tools/)
> - [ZSH Globbing as an Alternative to Find Command](https://dmitry-antonyuk.medium.com/zsh-globbing-as-an-alternative-to-find-command-2ebf9da5cffe)
> - [https://opensource.com/article/18/9/tips-productivity-zsh](https://opensource.com/article/18/9/tips-productivity-zsh)