The Ultimate Guide to Your Terminal Makeover 2021 | by Shinichi Okada | Towards Data Science

[Latest update 2021–12–17, Starship and Nerd Fonts.]
Table of Contents· Introduction  ∘ Homebrew· iTerm2· Zsh· Oh-My-Zsh  ∘ Errors  ∘ Shortcut for toggling hidden files· Themes  ∘ Starship (Updated)  ∘ iTerm2 Theme· Oh-My-Zsh and plugins  ∘ 1. git plugin  ∘ 2. alias  ∘ 3. alias dirs and cd -  ∘ 4. autojump plugin  ∘ 5. brew plugin  ∘ 6. zsh-syntax-highlighting plugin  ∘ 7. zsh-autosuggestions plugin· Duplicate a tab· Shaping up terminal command history· Creating aliases· Useful commands· Terminal Shortcuts· Task management· Fun with Terminal  ∘ Screensaver  ∘ FIGlet  ∘ Colors  ∘ Fonts· Weather reports on your terminal· macOS/Linux commands  ∘ Terminal calendars  ∘ date  ∘ ditto  ∘ Common terminal commands· Conclusion· Update Log· Newsletter
Do you use your terminal all day? Is the terminal the first app you start after restarting your computer? In this article, you will find how to improve your terminal look and practical commands for your day-to-day work.
[Update] You can install all packages in this article with a line of code, see this article.
towardsdatascience.com
You need to have Homebrew installed. If you don’t have it, you can install it by running the following in a terminal.
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Homebrew requires Command Line Tools for Xcode. But if you don’t have it, the installation will install it for you.
Using a text editor (Vim/VSCode/TextEdit) add the following:
# ~/.zshrc export PATH=$(brew --prefix)/bin:$PATH 
Then source ~/.zshrc:
$ . ~/.zshrc
This will load ~/.zshrc.
Run brew help to see if it is installed.
towardsdatascience.com
towardsdatascience.com
iTerm2 is a replacement for the terminal and it works on Macs. iTerm2 brings the terminal modern looks with great features.
You can download iterm2 or you can install iTerm2 with Homebrew.
$ brew install iterm2
Zsh is a shell designed for interactive use and it is also a powerful scripting language.
You can find your shell.
$ echo $SHELL/bin/zsh
And find your zsh version.
$ zsh --versionzsh 5.8 (x86_64-apple-darwin20.0)
If you don’t have it then install it with Homebrew.
$ brew install zsh
If your shell is not zsh, run the following.
$ chsh -s /bin/zsh
Then restart your iTerm2 or open a new tab in iTerm2.
$ echo $SHELL
Oh-My-Zsh is an open-source, community-driven framework for managing your ZSH configuration. It comes bundled with a ton of helpful functions, helpers, plugins, themes.
You can install Oh-My-Zsh with running one of the following commands in iTerm. You can install this via the command-line with either curl or wget.
via curl
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
via wget
$ sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
You can see the terminal prompt change to ~.
Update Oh-My-Zsh:
$ omz update
Oh-My-Zsh may overwrites ~/.zshrc, you need to add the path to brew. This time you need to add it before plugins.
Using a text editor (Vim/VSCode/TextEdit):
# ~/.zshrc export PATH=$(brew --prefix)/bin:$PATH plugins=(git)
Do you have the following errors?
Or
You need to change the access permission to these files. These commands will fix them.
Run the followings on your terminal:
$ chmod 755 $(brew --prefix)/share/zsh$ chmod 755 $(brew --prefix)/share/zsh/site-functions
And open another tab.
You can toggle showing hidden files in Finder by Command + Shift + Period.
betterprogramming.pub
betterprogramming.pub
In this section, you will find Starship and iTem2 themes. You just need to use one of them. I used to use iTerm2 theme, but now I use Starship.
I started using the Starship and I quite like it. The Starship is a minimal, blazing-fast, and infinitely customizable prompt.
After installing iTerm2, Zsh, Oh-My-Zsh as I explained before you install the Starship.
For example, if you are a Homebrew user:
$ brew install starship
Add the following to the end of ~/.zshrc:
From your terminal:
$ echo 'eval "$(starship init zsh)"' >> ~/.zshrc
Or if you know Vim (or TextEdit for beginners):
eval "$(starship init zsh)"
We change the iTerm2 theme to Minimal.
We change the color presets to Snazzy:
Starship recommends using Nerd-font. You can install it with brew:
$ brew tap homebrew/cask-fonts$ brew install --cask font-hack-nerd-font
Or you can install Fira Code using brew:
$ brew tap homebrew/cask-fonts && brew install --cask font-fira-code-nerd-font
Select Hack Nerd Font Mono under iTerm2 Preference > Profile > Text:
Or select FiraCode Nerd Font Mono:
In one of my npm package directories, it shows Git status, repo version, and Node version with icons.
The [!] is one of Starship Git status options and it is telling you that the directory is modified.
If you want to know what’s going in the status bar, use the starship explain command:
$ starship explain
You can change the prompt using the Starship configuration.
Powerline
iTerm has more than 200 themes. My favorite themes are Cobalt2 and Oceanic next.
Let’s install Cobalt2.
Download this repo and drop the cobalt2.zsh-theme file in to the ~/.oh-my-zsh/themes/ directory. Command + C to copy the file and Command + Option + V to cut-paste.Open up your ZSH preferences at ~/.zshrc and change the theme variable to ZSH_THEME=cobalt2.
Download this repo and drop the cobalt2.zsh-theme file in to the ~/.oh-my-zsh/themes/ directory. Command + C to copy the file and Command + Option + V to cut-paste.
Open up your ZSH preferences at ~/.zshrc and change the theme variable to ZSH_THEME=cobalt2.
3. Install Powerline and necessary fonts - one way is using PIP
$ pip3 install --user powerline-status
You may need to upgrade PIP.
$ pip3 install --upgrade pip
4. Install all necessary fonts by downloading or cloning the git repository.
$ git clone https://github.com/powerline/fonts$ cd fonts$ ./install.sh
If you wish to remove the fonts directory.
$ ..$ rm -rf fonts
5. In iTerm2 access the Preferences pane on the Profiles tab.
6. Under the Colors tab import the cobalt2.itermcolors file via the Color Presets drop-down.
7. Under the Text tab change the font for each type (Regular and Non-ASCII) to ‘Inconsolata for Powerline’.
8. Refresh ZSH by typing source ~/.zshrc on the command line.
VS Code terminal
For your VS Code terminal, you need to add the following to Setting(JSON).
{    "terminal.integrated.fontFamily":"Inconsolata for Powerline",}
If you feel a bit adventurous, try powerlevel10k theme.
Oh-My-Zsh has built-in commands.
Oh-My-Zsh’s power comes with its plugins. There are more than 260 plugins are available.
The zsh git plugin provides many aliases and a few useful functions. To install it, add git to the plugins array in your .zshrc file. Open .zshrc with your text editor:
plugins=(git)
The following is an example workflow using the git plugin.
# make a directory and cd into it$ mkdir -p Datascience/terminal-article && cd $_# create a new git repo$ git initInitialized empty Git repository in /Users/shinokada/DataScience/terminal-article/.git/# Add a README.md$ echo "# Terminal-article" >> README.md# git add .$ ga .# git commit -m $ gcmsg "First commit"[master (root-commit) 128f2b9] First commit1 file changed, 1 insertion(+)create mode 100644 README.md# git remote add$ gra origin git@github.com:shinokada/terminal-article.git
# modify a file$ echo "more fix" >> README.md# git status$ gst# git add .$ ga .# git commit and message$ gcmsg "Update"# git push origin$ ggp
You can use alias to see all your alias commands. Please see this page for more aliases.
You use aliases cd -, cd -2, etc. with the dirs command as you can see in the above image. The dirs displays the contents of the directory stack. The current directory is always the "top" of the directory stack. cd -2 changes directory to the second in the directory stack.
autojump plugin loads the autojump navigation tool. autojump is a faster way to navigate your filesystem. It works by maintaining a database of the directories you use the most from the command line.
First, install it on your Mac OS.
$ brew install autojump# or for port user$ port install autojump
To use it, add autojump to the plugins array in your .zshrc file:
# no comma between plugins required plugins=(git autojump)
Add the following to the end of ~/.zshrc (Intel x86_64 or arm64):
[ -f $(brew --prefix)/etc/profile.d/autojump.sh ] && . $(brew --prefix)/etc/profile.d/autojump.sh
Reload .zshrc by so .zshrc on your terminal or open a new tab.
By changing your directory, autojump records directories. You type j + first few letters of a directory. e.g j Data.
The brew plugin adds several aliases for common brew commands.
To use it, add brew to the plugins array of your .zshrc file:
plugins=(git autojump brew)
After adding brew to plugins, the following message will come.
You can start using it.
Create your own alias
In .zshrc add the following. buou will update, show outdated and upgrade.
# brew update && brew outdated && brew upgrade ➡️  buoualias buou="brew update && brew outdated && brew upgrade && brew cleanup"
Install zsh-syntax-highlighting by cloning the repository in oh-my-zsh’s plugins directory:
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
Note that zsh-syntax-highlighting must be the last plugin sourced. Activate the plugin in ~/.zshrc:
plugins=(some other plugins zsh-syntax-highlighting)
Restart iTerm2, open a new tab, or run source .zshrc.
Before:
After:
The zsh-autosuggestions is fish-like autosuggestion for zsh.
Clone the repository into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins):
Clone the repository into $ZSH_CUSTOM/plugins (by default ~/.oh-my-zsh/custom/plugins):
$ git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
2. Add the plugin to the list of plugins in ~/.zshrc:
plugins=(git brew zsh-syntax-highlighting zsh-autosuggestions)
3. Start a new terminal session or open a new terminal tab.
$ source ~/.zshrc
When you press CMD+T, iTerm will open a new tab and the location is the ~ or home directory. I’d like to duplicate a tab with the same directory.
On your iTerm, press CMD+, to open settings. Press Keys and select Key Bindings. Click + sign at the bottom left and choose your keyboard shortcut, like Shift+CMD+T. Select Duplicate Tab in the action.
History is stored in ~/.zsh_history or ~/.bash_history or ~/.history depending on your shell. History is stored for 1000 or 2000 lines depending on your system. You can find the number of history:
$ echo $HISTSIZE 50000$ echo $HISTFILE/Users/shinokada/.zsh_history$ echo $SAVEHIST10000
HISTSIZE is the maximum number of lines that are kept in a session and SAVEHIST is the maximum number of lines that are kept in the history file. HISTFILE is the file name where your system is saving history.
You can change these numbers by adding the following to .zshrc.
HISTSIZE=5000SAVEHIST=5000
reverse-i-search allows you to search for a command from the history file. Press CTRL+r to start it and type whatever you remember to search. If you keep pressing CTRL+r, the terminal searches the next command in the history file.
You can see all your command history using a bash command history.
# shows all command history$ history# shows first 10 commands$ history | head# shows last 10 commands$ hisotry | tail# shows less history$ history | less# Use grep to search your command history$ history | grep Search-word# Use grep to do a case-insensitive search of your command history$ hisotry | grep -i search-word
In order to remove commands from your history list, you add the following to your .zshrc :
setopt HIST_IGNORE_SPACE
This removes command lines from the history list when the first character on the line is a space.
# This won't work$ cd ~# This will work because there is a space before the command$  cd ~
You may not want common commands in the command history. Adding the following to zshrc prevent ll ls la cd man scp vim nvim less ping open file which whois drill uname md5sum traceroute commands in the command history.
zshaddhistory() {    local line=${1%%$'\n'}    local cmd=${line%% *}    # Only those that satisfy all of the following conditions are added to the history    [[ ${#line} -ge 5       && ${cmd} != ll       && ${cmd} != ls       && ${cmd} != la       && ${cmd} != cd       && ${cmd} != man       && ${cmd} != scp       && ${cmd} != vim       && ${cmd} != nvim       && ${cmd} != less       && ${cmd} != ping       && ${cmd} != open       && ${cmd} != file       && ${cmd} != which       && ${cmd} != whois       && ${cmd} != drill       && ${cmd} != uname       && ${cmd} != md5sum       && ${cmd} != pacman       && ${cmd} != xdg-open       && ${cmd} != traceroute       && ${cmd} != speedtest-cli    ]]}zshaddhistory
As I showed before withbuou, you can add your own alias to .zshrc file.
# brew update && brew outdated && brew upgradealias buou="brew update && brew outdated && brew upgrade && brew cleanup"# npm outdated -g --depth=0 && npm update -galias npmou="npm outdated -g --depth=0 && npm update -g"
I havevirtualenv and virtualenvwrapper installed. And the following aliases allow me to use note to start a Jupyter environment and Jupyter notebook. lab will start the environment and Jupyterlab.
# start jupyter environmentalias wj='workon jupyter'# start jupyter notebookalias note='workon jupyter && jupyter notebook'# start jupyterlabalias lab='workon jupyter && jupyter lab'
# Repeat the last command$ !!# Clear the screen$ clear # Or you can clear the screen with CTRL+l, as well
Instead of using the rm command, I suggest using rmtrash. It will move the file to OS X’s Trash rather than removing permanently. You can install it using brew:
$ brew install rmtrash
And create an alias in your ~/.zshrc file:
alias   del="rmtrash"
Removing files in Download directory:
# Using autojump$ j Down$ del ./*
You can find more keyboard shortcuts for Mac here.
You can use taskworrior to manage your todo-list from the command line.
If you are Homebrew user:
$ brew install task
You can find other OS installations from this page.
You can find a quick demonstration, best practices and example commands in the doc.
$ task add Buy milkCreated task 1.$ task listID Description-- -----------1  Buy milk
Use +/ - to add/delete tags.
$ task add Buy cake +shopping -lunch
Use project: to add Project.
$ task add Update a file project: 'Medium article A'
Use task ID modify to modify a list.
$ task modify 1 Buy milk and bread
Use task ID done when you complete a task.
$ task 1 done
pipes.sh is an animated terminal screensaver. You can install it using Homebrew:
$ brew install pipes-sh
Find out the options:
$ pipes.sh -hUsage: pipes.sh [OPTION]...Animated pipes terminal screensaver. -p [1-] number of pipes (D=1). -t [0-9] type of pipes, can be used more than once (D=0). -c [0-7] color of pipes, can be used more than once (D=1 2 3 4 5 6 7 0). -t c[16 chars] custom type of pipes. -f [20-100] framerate (D=75). -s [5-15] probability of a straight fitting (D=13). -r LIMIT reset after x characters, 0 if no limit (D=2000). -R   randomize starting position and direction. -B   no bold effect. -C   no color. -K   pipes keep their color and type when hitting the screen edge. -h  help (this screen). -v  print version number.
You can change the number of pipes using the -p option and the type of pipes using the -t option, etc.
$ pipes.sh -p4 -t2
When you press any key, it stops.
You can install cmatrix if you are fun of Matrix movies.
$ brew install cmatrix# run cmatrix$ cmatrix
You need to press Ctrl-c to stop the screen saver.
I created the title image with FIGlet. FIGlet is a program for making large letters out of ordinary texts.
$ brew install figlet$ printf "\e[92m" && figlet -f standard "Terminal Tips"
printf "\e[92m" sets the output color. You can print your terminal color code.
$ for code in {30..37}; do \echo -en "\e[${code}m"'\\e['"$code"'m'"\e[0m"; \echo -en "  \e[$code;1m"'\\e['"$code"';1m'"\e[0m"; \echo -en "  \e[$code;3m"'\\e['"$code"';3m'"\e[0m"; \echo -en "  \e[$code;4m"'\\e['"$code"';4m'"\e[0m"; \echo -e "  \e[$((code+60))m"'\\e['"$((code+60))"'m'"\e[0m"; \done
Now you can change the color using one of the colors.
-f standard sets the font. You can select a wide variety of fonts.
You can print the weather report on your terminal using wttr.in.
$ curl wttr.in/CityName
If you want to find the weather for your current location, run curl wttr.in.
You can find more options:
$ curl wttr.in/:help
You can add an alias to your .zshrc. For ZSH, you need to add \ to escape a special character like ?.
# weatheralias we='curl wttr.in/Tokyo' #current, narrow, quiet, no Followalias we1='curl wttr.in/Tokyo\?1nqF' #+1day, narrow, quiet, no Followalias we2='curl wttr.in/Tokyo\?2nqF' #+2days, same as above
macOS is based on the Unix operating system and has almost the same commands as Linux. I already mentioned the dir command before. Let me list more commands which you use regularly.
You can display a calendar on your terminal.
# Current month calendar$ cal# Yearly calendar$ cal 2020# Current month + 2 months$ cal -A 2
Try date to display time.
ditto copies files and folders.
Now, you know how to beef up your terminal. I hope this article will boost your productivity when working in your terminal. And you can have some fun with your terminal or check the weather report and calendar.
Find out your favorite themes and plugins to suit your needs.
What do you have in your .zshrc file? Please share your GitHub link.
2021–12–17, Starship and Nerd Fonts.
2021–011–20, M1 Pro chip and fira-code-nerd-font
2021–06–11, M1 chip, autojump
2021–5–23, oh-my-zsh error, for M1 chip, Duplicate Tab
2021–03–30, 2021 version
2021–02–22, Fun with Terminal
2021–02–15, Weather
Get full access to every story on Medium by becoming a member.