Terminal Customization

Personal Log 2021-05-15

How to make the terminal comfortable. Here are the basic steps I usually follow to setup my Ubuntu (18/20) machines.

Zsh

I use zsh instead of bash.

🖥️ sudo apt install zsh

After installing zsh, make it the default shell.

🖥️ chsh -s $(which zsh)

Close all programs and save documents as needed, then logout from the computer.

🖥️ gnome-session-quit

Verify that the shell has changed.

🖥️ echo $SHELL 📟 /usr/bin/zsh 🖥️ zsh --version 📟 zsh 5.4.2 (x86_64-ubuntu-linux-gnu)

Similar to bash, there is a .zshrc file created for zsh in the home directory that can be used as the entry point for customizing the terminal.

Syntax Highlighting Plugin

This plugin enables syntax highlighting for terminal commands. The user can also install the plugin locally (see installation documentation).

🖥️ sudo apt install zsh-syntax-highlighting

If using apt to install, figure out where the .zsh file for the new plugin went.

🖥️ dpkg -L zsh-syntax-highlighting 📟 <...> 📟 /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

This .zsh file should be sourced at the end of ~/.zshrc. Incorrect commands will now show up in red, and valid commands will be green.

🖥️ echo source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh >> ~/.zshrc

Oh My Zsh

The oh-my-zsh project has nice themes for the terminal. My favourite is the refined theme.

🖥️ sudo apt install git curl 🖥️ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Follow the prompts to install the default options. There should now be a ~/.oh-my-zsh directory, with a custom subdirectory where you can put aliases, themes, and plugins.

The ~/.zshrc file is modified after installing oh-my-zsh. Here is a minimum working example of my ~/.zshrc file:

export ZSH="/home/jen/.oh-my-zsh" ZSH_THEME="refined" HYPHEN_INSENSITIVE="true" DISABLE_UPDATE_PROMPT="true" ENABLE_CORRECTION="true" plugins=(git) source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh source $ZSH/oh-my-zsh.sh

Miniconda and Zsh

When using miniconda to manage Python installations, there is also a section in the ~/.zshrc file to enable the default conda environment on shell startup.

__conda_setup="$('/home/jen/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/home/jen/miniconda3/etc/profile.d/conda.sh" ]; then . "/home/jen/miniconda3/etc/profile.d/conda.sh" else export PATH="/home/jen/miniconda3/bin:$PATH" fi fi unset __conda_setup

To add this code block, install miniconda run the following command:

🖥️ ~/miniconda/bin/conda init zsh

Aliases

Looking at the ~/.oh-my-zsh/oh-my-zsh.sh file, we can see that any file in the ~/.oh-my-zsh/custom directory with a .zsh extension is read at shell startup.

<...> if [[ -z "$ZSH_CUSTOM" ]]; then ZSH_CUSTOM="$ZSH/custom" fi <...> for config_file ($ZSH_CUSTOM/*.zsh(N)); do source $config_file done unset config_file <...>

I typically keep my custom aliases in ~/.oh-my-zsh/custom/jen.zsh. Here are a couple of examples of adding new alias definitions.

🖥️ touch ~/.oh-my-zsh/custom/jen.zsh 🖥️ echo alias c.=\"cd ../\" >> ~/.oh-my-zsh/custom/jen.zsh 🖥️ echo alias gs=\"git status\" >> ~/.oh-my-zsh/custom/jen.zsh 🖥️ cat ~/.oh-my-zsh/custom/jen.zsh 📟 alias c.="cd ../" 📟 alias gs="git status"

Note: the cd command is no longer needed to switch to a new directory when using zsh. However, I still use c. to switch to the upper level directory.