Zsh is pretty cool

Well, thanks to @alex, who mentioned that he uses zsh and not bash, so I spent or wasted two hours understanding it. It is definitely cool and the high points are:

  1. Bash scripts still work. Unlike other shells, the syntax is close to bash and as long as your scripts use #!/usr/bin/env bash then all your old stuff just works.
  2. 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-zsh will get you most of what you need.

The main difference are lots of user interface things that make working faster like:

  1. cd completions. If you do a change directory and hit tabe, it will show you all directories thare a possible
  2. auto_cd. Bash has this now too, but just typing a directory name will do cd
  3. spell checkers. Turns out bash now supports spell checking with shopt -s cdspell and zsh with setopt correct

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

  1. 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
  2. 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
  3. 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.
  4. web-search. type things in command like to do a web search like “google any-string”
  5. colorize – uses pygmenter to ccat files in color
  6. copyfile – copy the file to your clipboard (so you can paste the contents somewhere later)
  7. thef*ck – ESC twice to correct command (conflicts with sudo plugin), you do need to brew install this first. It si really helpful.
  8. 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.
  9. 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,

  1. dirhistory – ALT-Left goes to previous directory, ALT-right so like dirs (does not work with AnnePro2)
  2. copydir – copydir will copy the last path onto clipboard
  3. wd – warp directory do a wd add to add to a list of directories, so this is an explicit version of z
  4. 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:

  1. gh. add command completions for github command line interface
  2. git-lfs. add completions and aliases like glfsi for git lfs install and glfst – git lfs track
  3. gnu-utils. use gnu without the g prefix (in bash, I have a gigantic lists of PATH additions to do the same
  4. helm. command completions
  5. kubectl. completions and aiias like k for kubectl and kaf for kubectl apply -f
  6. gcloud. google cloud completions
  7. pip. command completion
  8. pipenv. completions and alias like psh pipenv shell and pi for pipenv install
  9. rg. completions
  10. 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:

  1. 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.
  2. 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
  3. 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.

I’m Rich & Co.

Welcome to Tongfamily, our cozy corner of the internet dedicated to all things technology and interesting. Here, we invite you to join us on a journey of tips, tricks, and traps. Let’s get geeky!

Let’s connect

Recent posts

Loading Mastodon feed…