Zsh is pretty cool
- Bash scripts still work. Unlike other shells, the syntax is close to bash and as long as your scripts use
#!/usr/bin/env bashthen all your old stuff just works.
- Plugins. The real advantage of this is that instead of random junk in your ~/.bashrc, you get a plugin system so the extra script code is hidden behind a well know framework. The most used of these is Oh my zsh which is a collection of 250 plus plugins. The nice thing is that installation of this is easy,
brew install zsh oh-my-zshwill get you most of what you need.
The main difference are lots of user interface things that make working faster like:
- cd completions. If you do a change directory and hit tabe, it will show you all directories thare a possible
- auto_cd. Bash has this now too, but just typing a directory name will do cd
- spell checkers. Turns out bash now supports spell checking with
shopt -s cdspelland zsh with
Oh My Zsh Plugin
The plugins are monsterous in Oh My Zsh and there are so many good ones. The most convenient are the completions which normally need a line each in bash, but now it's just a plugin. So these include as an example in my configuration
plugins=(fzf git macos web-search copydir copyfile dirhistory gh gcloud git-lfs asdf colorize wd gnu-utils helm kubectl pip pipenv ripgrep vi-mode z npx npm thefuck)
And here is what these do and as an aside looking at the code on github seems to be the best way to figure out these things are doing
- git. This add completions and a host of alias like ga for git add, gca for git commit -av, gd for git diff, gfa for git fetch --all --prune, gp for git push
- macos. A host o alias that integrate with the MacOS graphica interface like ofd for open in finder, pfd for print finder path, cdf for change directory in finder, tab for open current directory in a new term tab, music for start the Music applicationc, pushdf for push finder dir, showfiles for show hidden files in Finder and btrestart to restart bluetooth
- vi-mode - ESC to enter vi edit mode. Another ESC puts in you in normal mode. Also if you are in command/normal mode, typing vv with get you quicky into vi to edit the command. As an aside, bash has a mode like this with set -o vi which uses the vi keys.
- web-search. type things in command like to do a web search like "google any-string"
- colorize - uses pygmenter to ccat files in color
- copyfile - copy the file to your clipboard (so you can paste the contents somewhere later)
- thef*ck - ESC twice to correct command (conflicts with sudo plugin), you do need to brew install this first. It si really helpful.
- fzf - This is a smart command and file searcher, note there is a problem where the ^r command is overwritten by vi-mode so make sure that fzf comes after vi-mode. But with this if you type fzf or if you want to find a command quickly, it overrides the Ctrl-r keys.
- npx, npm - the nice thing is that rather than having to put all kinds of sourcing, running these plugins just takes care of command completions.
Finally, there are bunch of plugins that help with moving around directories. Although I use dirs and pushd and popd a lot,
- dirhistory - ALT-Left goes to previous directory, ALT-right so like dirs (does not work with AnnePro2)
- copydir - copydir will copy the last path onto clipboard
- wd - warp directory do a wd add to add to a list of directories, so this is an explicit version of z
- z - z guess which directory you want to go to, it caches all the directories you have used and if you type a partial it will jump to that directory
Then a host of things that just add command line completions and also give you some command abbreviations:
- gh. add command completions for github command line interface
- git-lfs. add completions and aliases like glfsi for git lfs install and glfst - git lfs track
- gnu-utils. use gnu without the g prefix (in bash, I have a gigantic lists of PATH additions to do the same
- helm. command completions
- kubectl. completions and aiias like k for kubectl and kaf for kubectl apply -f
- gcloud. google cloud completions
- pip. command completion
- pipenv. completions and alias like psh pipenv shell and pi for pipenv install
- rg. completions
- asdf. completions
Zinit Plugin manager for not oh-my-zsh plugins
There are many plugins that aren't part of Oh My Zsh, so you need to either manually git clone them or use a manager to do this. Zinit (out of an incredible host of these is very easy to use). Alternatively, you can do brew installs of them and then add a source line to the .zshrc, but using zinit with
brew install zinit is much easier.
So to putting this in .zshrc is pretty easy:
# brew install zinit and then add the source line source "$(brew --prefix)/opt/zinit/zinit.zsh # https://github.com/zdharma-continuum/zinit zinit ice depth"1" # git clone depth zinit ice wait # load asynchronously zinit light zsh-users/zsh-autosuggestions zinit light romkatv/powerlevel10k zinit light oldratlee/hacker-quotes
What this adds are three pretty important things:
- zsh-autosuggestions. If you hit right arrow key and it will guess what command you want based on your history. It will call tab completion as well if that doesn't work.
- powerlevel10k. I invested so many hours in getting powerline-go to work on bash and this makes it so easy to have a good command line prompt
- hacker-quotes. I actually didn't get this to work but it is supposed to give you a suitable quote everytime you start a shell.