Migrating from Bash to Zsh

productivityJanuary 04, 2014Dotby Justin Gordon

I migrated from Bash to Zsh in October and I'm quite thrilled with zsh.

Motivation

What motivated me to finally move?

  1. I no longer used the shell within Emacs. If you have a bash setup that works well in Emacs, don't try to switch. Since moving from emacs bindings to evil-mode, I am quite comfortable in Vim, so I find myself using iTerm way more than the Emacs shell.
  2. While doing some pair programming with @jakeonrails, I saw the coolness of zsh, which he pointed out to me "was no big deal", just what's on the Railscasts Episode on Oh My ZSH.

Tips on Migrating

Here's a few tips to somebody migrating might find useful:

  1. Try out oh-my-zsh and take a look at the themes examples. The themes seem to be all customizations of the prompt. I'll share what I came up with at the bottom, which is a modification of the default robbyrussell theme.
  2. While most of your bash code will migrate as-is, this is a good time to clean up some cruft in your files. I like to organize my shell code into small files, each with a particular theme, and then have the .zshrc source those, rather than having a giant .zshrc file.
  3. The oh-my-zsh plugins are way for you to share shell configuration with other members of the community. It's simple to read what those plugins are doing. Many are just setting aliases. I started to migrate my own configuration code by converting to plugins, but then I realized that that's overkill. If I ever want to share the configuration, at that point, I can convert to a plugin, which is quite simple.
  4. If you have any shell functions that use [, you might have escape that character for zsh.
  5. If you install zsh plugins, be very careful with any newly installed aliases from the plugins. I previously had gl aliased as 'git log' and the git plugin uses gl for git pull, which caused me a huge headache when I ran that within my octopress branch.
  6. You need to escape the ^ character for commands such as git reset HEAD\^

Migration Notes

Escape []

In the third line of this function, I had to escape the [ and the ].

opost() {
  cd $OCTO_HOME
  output=$(rake new_post\["${1}"\])
  new_file=$(echo $output | awk '{print $4}')
  base=$(basename $new_file)
  new_location=$OCTO_HOME/source/org_posts/
  mv $OCTO_HOME/$new_file $new_location
  echo created $new_location$base
  cd -
}

My new zsh prompt

image1

To set this up, I created a custom theme called justin808 by doing the following:

  1. Create a theme file oh_my_zsh/custom/justin808.zsh-theme. See below.
  2. Export the theme name.

This is what it looks like in my .zshrc file. The first line is because I moved my ZSH configuration files.

export ZSH=$HOME/.oh-my-zsh
export ZSH_THEME="justin808"

Here is my theme file oh_my_zsh/custom/justin808.zsh-theme

hostname=`hostname`
if [ $hostname != "$HOME_HOST" ] || [ $USER != "justin" ]; then
  host_stuff='%n@%m:'
else
  host_stuff=''
fi
PROMPT='%{$fg_bold[red]%}➜ %{$fg_bold[green]%}%p ${host_stuff}%{$fg[cyan]%}${PWD/#$HOME/~} %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'

# display exitcode on the right when >0
return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"

RPROMPT='${return_code}$(git_prompt_status)%{$reset_color%} [%*]'

# RPROMPT='[%*]'
ZSH_THEME_GIT_PROMPT_PREFIX="(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
GIT_PS1_SHOWUPSTREAM=verbose

ZSH_THEME_GIT_PROMPT_ADDED="%{$fg[green]%} ✚"
ZSH_THEME_GIT_PROMPT_MODIFIED="%{$fg[blue]%} ✹"
ZSH_THEME_GIT_PROMPT_DELETED="%{$fg[red]%} ✖"
ZSH_THEME_GIT_PROMPT_RENAMED="%{$fg[magenta]%} ➜"
ZSH_THEME_GIT_PROMPT_UNMERGED="%{$fg[yellow]%} ═"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[cyan]%} ✭"

Closing Remark

Could your team use some help with topics like this and others covered by ShakaCode's blog and open source? We specialize in optimizing Rails applications, especially those with advanced JavaScript frontends, like React. We can also help you optimize your CI processes with lower costs and faster, more reliable tests. Scraping web data and lowering infrastructure costs are two other areas of specialization. Feel free to reach out to ShakaCode's CEO, Justin Gordon, at [email protected] or schedule an appointment to discuss how ShakaCode can help your project!
Are you looking for a software development partner who can
develop modern, high-performance web apps and sites?
See what we've doneArrow right