Overhaul your Terminal with Zsh + Posh + More

This guide will look at various ways you can improve your productivity by extending the functionality of your terminal, including:

zsh: A powerful shell that extends the feature set of bash.

zsh plugins: Auto-suggestions, completion, syntax highlig…


This content originally appeared on DEV Community and was authored by TimJ

This guide will look at various ways you can improve your productivity by extending the functionality of your terminal, including:

  • zsh: A powerful shell that extends the feature set of bash.
  • zsh plugins: Auto-suggestions, completion, syntax highlighting and more.
  • posh: Custom prompt themes and display contextual information.

Getting Started

  1. Install oh-my-zsh for any Unix based shell or WSL2, which bundles zsh with a set of plugins and themes.
  2. Download a Nerd Font and set your favourite terminal to use it. I like Cascaydia Cove Nerd Font the best.

Zsh Plugins

To enable plugins locate your .zshrc in your $HOME directory, and add to the list of plugins:

plugins=(
  git
  ...
)

Plugins Included with oh-my-zsh

  • sudo: Easily prefix your current or previous commands with sudo by pressing esc twice.
  • dotenv: Automatically load your project ENV variables from .env file when you cd into project root directory. By default, it will prompt before loading, but you can turn this off by adding the following to your .zshrc:
export ZSH_DOTENV_PROMPT=false
  • copypath: Copies the path of given directory or file to the system clipboard.
  • copyfile: Puts the contents of a file in your system clipboard so you can paste it anywhere.
  • copybuffer: This plugin binds the ctrl-o keyboard shortcut to a command that copies the text that is currently typed in the command line ($BUFFER) to the system clipboard.
  • command-not-found: provide suggested packages to be installed if a command cannot be found.
  • web-search: This plugin adds aliases for searching with Google, Wiki, Bing, YouTube and other popular services.
  • history: Provides a couple of convenient aliases for using the history command to examine your command line history.
  • dirhistory: Adds keyboard shortcuts for navigating directory history and hierarchy. Uses alt-arrowkeys by default, so ensure these do not conflict with other shortcuts.
  • colorize: Syntax-highlight file contents of over 300 supported languages and other text formats.
  • colored-man-pages: Adds colors to man pages.
  • magic-enter: This plugin makes your enter key magical, by binding commonly used commands to it. You customize the command it uses, and run specific commands in a Git repo:
MAGIC_ENTER_GIT_COMMAND='git status -sb .'
MAGIC_ENTER_OTHER_COMMAND='ls -la .'
  • jsontools: Handy command line tools for dealing with json data.

Depending on what other software you use, there are plenty of lightweight plugins that improve autocompletion and provide aliases amoung other things. I use:

  • git: Aliases for Git
  • gh: Autocompletions for GitHub cli
  • nvm: Autocompletions for nvm
  • npm: Autocompletions + aliases for npm
  • aws: Automcompletions for aws
  • docker: Automcompletions for docker
  • docker-compose: Autocompletions + aliases for docker compose
  • kubectl" Autocompletions + aliases for kubectl
  • vscode: Aliases for VS Code or VSCodium editor
  • python: Aliases for python
  • pip: Automcompletions for pip
  • tmux: Aliases for tmux

Powerful Plugins

These plugins do not come with oh-my-zsh, so you must clone the repos and place them in ~/ohmyzsh/custom/plugins/ before adding them to the plugins list in the .zshrc.

  • zsh-autosuggestions: Suggests commands as you type based on history and completions, which can then be selected with →. zsh-autosuggestions
    • Autosuggestions can either use your history, or tab completion. Set this with the ZSH_AUTOSUGGEST_STRATEGY variable.
    • Has some compatability issues where the suggestion is not cleared after certain actions. To fix this, add the following to your .zshrc:
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste up-line-or-search down-line-or-search expand-or-complete accept-line push-line-or-edit)
  • zsh-syntax-highlighting: Syntax highlighting for the shell zsh. zsh-syntax-highlighting
  • zsh-autocomplete: Provides suggestions for recent directories and man page, in addition to a history menu. This one is powerful, but might take some getting used to. zsh-autocomplete

To take things further, I recommend checking out this curated list of plugins.

Themes + Custom Prompts

We can improve our prompt to contain additional information, such as execution time, performance metrics, and even things like your current Git branch. Set your theme by adding the following to your .zshrc (set to random until you find something you like):

ZSH_THEME="mytheme"

I like agnoster best:
agnoster
As nice as our prompt looks, we can go further with oh-my-posh:
clean-detailed

  • Follow installation instructions and find a theme you like. Some favourites of mine are clean-detailed, blueish, or nu4a.
  • Add the path to your chosen theme to .zshrc:
eval "$(oh-my-posh --init --shell zsh --config ~/.mytheme.omp.json)"

Other customization to add to your zshrc

For reference, this is what my .zshrc looks like.

  • Custom aliases. Some I like to use for networking things:
alias get-ports="netstat -tulnp | grep LISTEN"
alias get-router="ip route | grep default"
alias get-ip="hostname -I"

 We can check that a command exists before setting the alias. For example, we can check docker is installed:

if [ -x "$(command -v docker)" ]; then
    alias dw="watch \"docker ps --format \\\"table {{.Names}}\t{{.Status}}\\\" -a\""
fi
  • Custom keybinds. For example: bindkey '^H' backward-kill-word binds ctrl-bksp to delete by word. Find keycodes here. Those familiar with vim shortcuts can add bindkey -v to their .zshrc. Be wary of conflicts with other keybinds/plugins!
  • Change the default colors for certain directories by adding the following to my .zshrc (go here for color codes):
zstyle ':completion:*:default' list-colors \
  "ow=30;43"

 Alternatively, you can export a complete color scheme from file:

eval "$(dircolors ~/.dircolors)";
  • Disable auto zsh updates: zstyle ':omz:update' mode disabled
  • Set preferred editior for ssh, for example:
if [[ -n $SSH_CONNECTION ]]; then
  export EDITOR='vim'
else
  export EDITOR='code'
fi
  • Store all your environment variables in another file: export $(cat ~/.my_env)
  • You can even automatically install packages, for example you can install nvm:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Next Steps


This content originally appeared on DEV Community and was authored by TimJ


Print Share Comment Cite Upload Translate Updates
APA

TimJ | Sciencx (2022-04-02T05:23:10+00:00) Overhaul your Terminal with Zsh + Posh + More. Retrieved from https://www.scien.cx/2022/04/02/overhaul-your-terminal-with-zsh-posh-more/

MLA
" » Overhaul your Terminal with Zsh + Posh + More." TimJ | Sciencx - Saturday April 2, 2022, https://www.scien.cx/2022/04/02/overhaul-your-terminal-with-zsh-posh-more/
HARVARD
TimJ | Sciencx Saturday April 2, 2022 » Overhaul your Terminal with Zsh + Posh + More., viewed ,<https://www.scien.cx/2022/04/02/overhaul-your-terminal-with-zsh-posh-more/>
VANCOUVER
TimJ | Sciencx - » Overhaul your Terminal with Zsh + Posh + More. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/02/overhaul-your-terminal-with-zsh-posh-more/
CHICAGO
" » Overhaul your Terminal with Zsh + Posh + More." TimJ | Sciencx - Accessed . https://www.scien.cx/2022/04/02/overhaul-your-terminal-with-zsh-posh-more/
IEEE
" » Overhaul your Terminal with Zsh + Posh + More." TimJ | Sciencx [Online]. Available: https://www.scien.cx/2022/04/02/overhaul-your-terminal-with-zsh-posh-more/. [Accessed: ]
rf:citation
» Overhaul your Terminal with Zsh + Posh + More | TimJ | Sciencx | https://www.scien.cx/2022/04/02/overhaul-your-terminal-with-zsh-posh-more/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.