Did I Improve ThisBlogAbout

Terminal Base Setup

02 July, 2020 - 5 min

Why?

The terminal can be an extremely powerful ally for your productivity. One of the aspects that I appreciate the most on the terminal is the keyboard-driven workflow.

I'm currently using the z shell, which is based on Bourne Shell (sh). It shares some features with bash since they both come from sh. Overall I would say zsh has the better set of features from the two. Just to name a few:

  • navigation between folders is greatly improved - you can change folders by just naming them, without the need to write cd.
  • typos correction by default - when you are typing a file/folder name it can detect and infer which file/folder you actually meant to type.
  • the oh-my-zsh community - which is the main reason why I changed to zsh. It provides a very easy setup for a huge number of plugins and themes.

Right now I'm using iTerm2, a macOS terminal emulator. I started using it when I moved to macOS because it was recommended everywhere. Re-evaluating it is on my todo list, just haven't put the time into it.

Small tip for iTerm2 users - use natural text editing (activate it on Profiles - session - keys). It allows to use option backspace to delete words, option arrows to navigate between words, etc.

Zsh on Oh My Zsh with powerlevel10k

Like I stated before, I use the oh-my-zsh framework for managing my zsh configuration. If you don't have zsh, see this link on how to install zsh.

You can install oh-my-zsh using the following command:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Powerlevel10k theme

To give the shell a neat look I use the zsh Powerlevel10k theme. Its quite simple to configure.

Assuming you have configured oh-my-zsh, you can simply run the two commands bellow. The first will download the necessary files to your machine and the second one will open an install wizard that will help you personalize the theme according to your preferences.

# download the necessary files
$ git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

# find the ZSH_THEME variable on your ~/.zshrc and set it to:
ZSH_THEME="powerlevel10k/powerlevel10k"

# restart iterm2
# run the install wizard to personalize the theme
$ p10k configure

Plugins

Oh-my-zsh provides access to a bunch of plugins out of the box. Currently, I'm only using the git and zsh-z plugins. The git plugin provides a visual state of your current git repo on the shell along with providing a very nice set of aliases. The zsh-z plugin significantly improves the folder navigation process.

Git plugin

To activate it if you have oh-my-zsh configured, just add git to the plugins variable in your .zshrc. Should be something like plugins=(git)

In the following image, you can see the state indicating I'm on the development branch. The !1 indicates on modified file. The ?1 indicates one untracked file. The orange color indicates uncommitted changes. You can also see that I ran gst which is an alias for git status.

Git repo status Fig. 1 - Git repo status

These are some of the aliases I'll frequently use:

gcmsg='git commit -m'
gpsup='git push --set-upstream origin $(git_current_branch)'
gp='git push'
gup='git pull --rebase'
grbm='git rebase $(git_main_branch)'
gupom='git pull --rebase origin $(git_main_branch)'
gaa='git add --all'
gcd='git checkout develop'
gcm='git checkout master'
gco='git checkout <branch name>'
gd='git diff'
gco -  # which toggles between the last branch and the current. Very usefull for merges.

One common flow for me when working on a feature branch:

gcm  # checkout master
gup  # get it up to date
gcb issueid-my-new-branch  # create new branch

# ... after a while if I want to update it
gupom # rebase from master
# alternatively you can do
gcm  # checkout master
gup  # get it up to date
gco -  # come back to the branch I'm working on
grbm # rebase with master
# possible conflict solving ahead :)

Zsh-z

Z is a command-line tool that boosts your ability to quickly change between your most (recently) visited directories. It keeps track of which directories you use and for how long. It creates a list of directories in order of most likely to visit.

To install it if you have oh-my-zsh configured, just ran the following command (if not take a look at the official installation instructions):

# download the files to the plugins zsh folder
git clone https://github.com/agkozak/zsh-z $ZSH_CUSTOM/plugins/zsh-z

Edit your zsh config file and find the plugins variable. It should look something like this if you also activated the git plugin: plugins=(git zsh-z)

I find it very useful to navigate between directories with very few keypresses. Here is an example of its usage.

Git repo status Fig. 2 - Z example. credits github@agkozak/zsh-z

I hope this article helped you improve your terminal workflow. I'm following up this with posts on terminal apps I use and other small improvements I try to add to my terminal workflow.

Thanks for reading!

Vicente




A list of articles in the series Terminal Workflow:

1. Terminal Base Setup

  1. Tmux - terminal multiplexer
  2. NeoVim - text editor
  3. Jrnl - note app
  4. Pass - password manager
  5. Ranger - file manager
  6. Simple Terminal Apps
  7. Terminal Aliases