Shell

[SHELL] Let's use Zsh!

eomiso(Aesop) 2020. 10. 2. 08:57

You probably are just doing well with bash but if you are a developer, you would have probably heard about Zsh. A new shell that apple has chosen to impose on their MacOS from now on.
As a person who likes to try out new stuff all the time, I’ve been using it for quite a while long before Apple implemented it on there mac.

[Tables of Content]

[Prerequisite]
* Linux/Mac
* Git (should be installed before hand)

Get zsh and change your default terminal to zsh

If you are using mac, skip this section.
If you are using ubuntu(linux) try using this script here

# install zsh (ubuntu)

sudo apt-get update
sudo apt-get install zsh

# making zsh as your default shell

sudo usermod -s $(command -v zsh) $(whoami)
sudo reboot

Use Oh-my-zsh

The zsh itself already has its ups such as auto completion. But its best part comes with the opensource framework for zsh: Oh-my-zsh.
You should absolutely try this out. Here is the script for downloading Oh-my-zsh. here

if command -v curl &> /dev/null; then
  sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
elif command -v wget &> /dev/null; then
  sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
else
  echo -en "You need curl or wget. "
  echo "Unexpected termination of $0"
  exit 0
fi

# if in linux
eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)

After installing Zsh, your commands such as vim, brew might not work. In this case try this out (or uncomment the second line in .zshrc). If this doesn’t work and if you have linuxbrew installed, you need to add eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv) to .zshrc file. Or add source .bash_profile since you would probably have this line of code there if you installed linuxbrew with bash.

Theme: agnoster

If you want to use the agnoster theme (I just go with robbyrussell because I didn’t like having half of my terminal space with directory hierarchy), You have to install PowerLine9k fonts.
To do that simply run this script here

cd ~/Documents
git clone https://github.com/powerline/fonts.git --depth=1
./fonts/install.sh

Then, if you are using ubuntu open terminal → edit → preference → find 'custom font' and change to one of powerline fonts
If you are a mac user open terminal → preference → font 'change' → change to one of powerline fonts

Then open .zshrc file and change theme= to theme=agnoster

vim ~/.zshrc

ZSH_THEME="agnoster"

source ~/.zshrc

Tips on Using Oh-my-zsh

One thing I really use often is cd -.
Press tab after cd - then it will show up your directory histories. It is super useful.

example

Additional Zsh Configuration

(1) multiline
There might be a bit of twitch at the cursor if the window of the terminal is too small.
If you would like to use a multiline, add below script to your .zshrc file

# add to .zshrc
build_prompt() {
  RETVAL=$?
  prompt_status
  prompt_virtualenv
  prompt_context
  prompt_dir
  prompt_git
  prompt_hg
  prompt_newline
  prompt_end
}

prompt_newline() {
  if [[ -n $CURRENT_BG ]]; then
    echo -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR
%(?.%F{$CURRENT_BG}.%F{red})❯%f"

  else
    echo -n "%{%k%}"
  fi

  echo -n "%{%f%}"
  CURRENT_BG=''
}

(2) Deleting the username on the bar
If you wish to dele user_name@local in the agnoster theme, add bellow script to your .zshrc file

# add to .zshrc
prompt_context() {
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
  fi
}

(3) Syntax Highlighting

For mac (if you have homebrew installed), and for linux(if you have linuxbrew installed)

# run this script in shell
brew install zsh-syntax-highlighting
find -name zsh-syntax-highlighting.zsh -exec source {}

[Reference]
What is zsh and why use it instead of Bash?
zsh부터 vim까지 총정리!

Trouble Shoot

Zsh not finding commands that used to work in bash

there are various ways to handle this. But I usually go with a very easy way. Just simply add these lines on your .zshrc file.

# add these lines to the top of the .zshrc file
source .bash_profile
source .bashrc

Written with StackEdit.