Overhaul your Terminal with Zsh + AutoComplete + More (2024)

Overhaul your Terminal with Zsh + AutoComplete + More (1)

TimJ

Posted on • Updated on

#productivity #linux #bash #wsl

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.
  • prompts + themes: Customize a clean prompt that displays contextual information, either using Powerlevel10k or Posh.

We'll go from this:

Overhaul your Terminal with Zsh + AutoComplete + More (3)

To this:

Overhaul your Terminal with Zsh + AutoComplete + More (4)

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 (though be wary of the security implications of this):
  • 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 among other things. I use:

  • git: Aliases for Git
  • gh: Completions for GitHub cli
  • git-auto-fetch: Periodically run git fetch
  • nvm: Completions for nvm
  • npm: Completions + aliases for npm
  • aws: Completions for aws
  • docker: Completions for docker
  • docker-compose: Completions + aliases for docker compose
  • kubectl" Completions + 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 .Overhaul your Terminal with Zsh + AutoComplete + More (5)
    • 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.Overhaul your Terminal with Zsh + AutoComplete + More (6)
  • zsh-autocomplete: Provides completion suggestions below the prompt, in addition to a history menu. This one is powerful, but might take some getting used to. Like with autosuggestions, you can choose whether you want to use normal completions (directories, man, etc.) or history search. I like to have autocomplete for normal completions and autosuggestions for history.Overhaul your Terminal with Zsh + AutoComplete + More (7) If you're like me and don't like the history menu, but want all the other features, you can change the keybinds to use a simple history search in your .zshrc (make sure this is after the plugins have been loaded):
bindkey '\e[A' history-beginning-search-backwardbindkey '\eOA' history-beginning-search-backwardbindkey '\e[B' history-beginning-search-forwardbindkey '\eOB' history-beginning-search-forwardzle -A {.,}history-incremental-search-forwardzle -A {.,}history-incremental-search-backward

 Similarly, to use the default tab completion, set the following options:

zstyle ':autocomplete:*' widget-style menu-selectbindkey -M menuselect '\r' accept-line

 I also don't like the completions to move the prompt around too much, especially for a multiline prompt. We can change this with:

zstyle ':autocomplete:*' list-lines 7

 The plugin also has some issues with the prompt "" showing on some completions and eating inputs. Add this to your .zshrc as a workaround (however this sometimes drops completions entirely, but I still prefer this over loosing inputs):

zstyle ':completion:*' menu select=long

 For a full list of config options, go here.

  • zsh-z: Tool to jump quickly to directories that you have visited frequently.
  • zsh-direnv: Alternative to dotenv, which also provides the use of a .envrc with utility functions.

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:
Overhaul your Terminal with Zsh + AutoComplete + More (8)
As nice as our prompt looks, we can take things further.
We have 2 options:

Option A: Powerlevel10k

Powerlevel10k is a highly customizable theme for zsh. It includes some nice features such as an instant prompt while plugins are loading, and a configuration wizard to get you started.

This is what my prompt looks like:

Overhaul your Terminal with Zsh + AutoComplete + More (9)

I've modified the configuration a bit to use custom icons for specific directories and cleaned up the git status a bit. See my .p10k.zsh

Option B: oh-my-posh

oh-my-posh:
Overhaul your Terminal with Zsh + AutoComplete + More (10) provides a wide range of themes to choose from, though is missing some features of Powerlevel10k such as instant prompt.

  • 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 for .zshrc

For reference, see my complete .zshrc

  • Disable auto zsh updates: zstyle ':omz:update' mode disabled
  • Print ... while waiting for completions to load (doesn't work with zsh-autocomplete): COMPLETION_WAITING_DOTS="true"
  • Custom aliases. For oh-my-zsh, it is recommended to put aliases in a different file: ~/.oh-my-zsh/custom/aliases.zsh. 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"

 You can also create an alias function with parameters. For example, to backup a file:

bak() { cp $1{,.bak}}

 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

 See all my aliases in my aliases.zsh. Note that for git aliases, it is best to define them in your .gitconfig.

  • 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)";

  • Set preferred editor for specific files:
alias -s {cs,ts,html,json,xml,md}=code

  • Set preferred editior for ssh, for example:
if [[ -n $SSH_CONNECTION ]]; then export EDITOR='vim'else export EDITOR='code'fi

  • Organize all your environment variables in another file: export $(cat ~/.my_env)
  • You can 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

Overhaul your Terminal with Zsh + AutoComplete + More (2024)

FAQs

How do I add autocomplete to zsh? ›

zsh-autocomplete adds real-time type-ahead autocompletion to Zsh. Find as you type, then press Tab to insert the top completion, Shift Tab to insert the bottom one, or ↓ / PgDn to select another completion.

How do you use zsh Suggestions? ›

As you type commands, you will see a completion offered after the cursor in a muted gray color. If you press the right-arrow key or End with the cursor at the end of the buffer, it will accept the suggestion, replacing the contents of the command line buffer with the suggestion.

How do you make fish like zsh? ›

How to make zsh like fish
  1. Install oh-my-zsh. sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
  2. Clone necessary plugins. ...
  3. Add plugins to ~/.zshrc as. ...
  4. Fix background theme issues (Not necessary depends on your theme) ...
  5. Restart zsh.

How do you update oh my zsh plugins? ›

Usage. Add autoupdate to the plugins=() list in your ~/. zshrc file and you're done. The updates will be executed automatically as soon as the oh-my-zsh updater is started.

How do I turn on autocomplete in zsh Mac? ›

To enable the plugin, open the file in a text editor of your choice. For the purposes of this tutorial, I will be using VIM. After navigating down through the file, you should see a plugins section. Here, all that is needed is adding zsh-autosuggestions to the plugins list, just like I have shown above.

Does bash completion work with zsh? ›

for ZSH users

Zsh can handle bash completions functions. The latest development version of zsh has a function bashcompinit, that when run will allow zsh to read bash completion specifications and functions.

Is fish better than zsh? ›

Fish, or the “Friendly Interactive Shell,” is the most user-friendly and interactive shell, in my opinion. It is much more customizable than Zsh and Bash. It has a ton of cool features like consistent syntax, nice tab completion and syntax highlighting, is easy to pick up and use, and has excellent runtime help.

How do I set zsh as default on Mac? ›

I was able to get this working by doing the following:
  1. Go to System Preferences.
  2. Click on "Users & Groups"
  3. Click the lock to make changes.
  4. Right click the current user -> Advanced options.
  5. Change the login shell to /bin/zsh in the dropdown.
  6. Open a new terminal and verify with echo $SHELL.

How do I know if I have oh my zsh? ›

“how to check if oh my zsh is installed” Code Answer
  1. if [ -d ~/.oh-my-zsh ]; then.
  2. echo "oh-my-zsh is installed"
  3. else.
  4. echo "oh-my-zsh is not installed"
  5. fi.

How do I update my zsh Mac? ›

How to Update bash to zsh in MAC - YouTube

What is zsh on Mac? ›

The Z shell (also known as zsh ) is a Unix shell that is built on top of bash (the default shell for macOS) with additional features. It's recommended to use zsh over bash . It's also highly recommended to install a framework with zsh as it makes dealing with configuration, plugins and themes a lot nicer.

How do I install oh my zsh plugins? ›

Install Plugins

Custom plugins can be installed at ~/. oh-my-zsh/custom/plugins . To use a plugin, you can simply add it to the plugins list in your ~/. zshrc file.

How do I enable autocomplete in Terminal Mac? ›

How to Enable Autocomplete in Mac Terminal
  1. Type in terminal nano ~/.inputrc.
  2. Paste the following on separate lines.
  3. set completion-ignore-case on set show-all-if-ambiguous on TAB: menu-complete.
  4. Hit control+O to save changes to .inputrc followed by control+X to exit nano.
27 Aug 2018

How do I auto fill in Mac terminal? ›

How to Enable Autocomplete in Mac Terminal
  1. Hit control+O to save changes to .inputrc followed by control+X to exit nano.
  2. Open a new Terminal window or tab to open a new session with autocomplete enabled.
  3. Type and hit the tab key.
27 Aug 2018

What is Compinit zsh? ›

Use of compinit

When run, it will define a few utility functions, arrange for all the necessary shell functions to be autoloaded, and will then re-bind all keys that do completion to use the new system.

Which is better bash or zsh? ›

Zsh is more interactive and customizable than Bash. Zsh has floating-point support that Bash does not possess. Hash data structures are supported in Zsh that are not present in Bash. The invocation features in Bash is better when comparing with Zsh.

What is zsh autoload? ›

The autoload command essentially instructs Zsh to register a function (called clock) using the contents of the clock file that is located in a directory somewhere along the fpath.

What is Zstyle? ›

The Zsh module “zstyle” allows you to configure settings for a specific Zsh module or widget.

Which is faster Zsh or fish? ›

With all these glorifying points of Fish, one thing to note is that Fish scripts are slow, slower than Zsh. Thus, anyone who wants to work with faster scripts can choose Zsh with all the plugins installed.

Which shell is best? ›

Top 5 Open-Source Shells for Linux
  1. Bash (Bourne-Again Shell) The full form of the word “Bash” is “Bourne-Again Shell,” and it is one of the best open-source shells available for Linux. ...
  2. Zsh (Z-Shell) ...
  3. Ksh (Korn Shell) ...
  4. Tcsh (Tenex C Shell) ...
  5. Fish (Friendly Interactive Shell)

Is Zsh syntax same as Bash? ›

Bash lacks syntax highlighting and auto-correction features. Zsh has syntax highlighting and auto-correction features. In bash keybinding is done using '. inputrc' and 'bind builtin'.

Why did Apple change to zsh? ›

One of the primary reasons why Apple switched to zsh is because it is closer to the functionality of the standard bash. If you are familiar with the standard bash, you will be glad to know that there is an Apple ZSH prompts which looks similar to the one you will find in Microsoft Outlook.

Does my Mac use bash or zsh? ›

Zsh is only the default shell on newly created user accounts, so any existing accounts you have on an upgraded Mac will still use Bash by default unless you change it.

How do I make zsh my default Terminal? ›

How to set ZSH as the default shell in your terminal.
  1. sudo vim ~/.bashrc.
  2. exec zsh.
  3. chsh -s $(which zsh)
1 Apr 2020

How is zsh pronounced? ›

Peter Sobot on Twitter: "Apparently, pronouncing "zsh" "zish" is a Canadian thing. Americans pronounce it "zeesh." #ZisZedNotZee" / Twitter.

What is the difference between zsh and oh-my-zsh? ›

When you install ZSH, you're simply downloading new software and instructing your terminal to use it (rather than bash) to process commands and run scripts. oh-my-zsh allows you to manage your zsh configurations, themes, and plugins to customize the appearance and functionality of your shell.

Can I install zsh on Windows? ›

There are 2 options to install zsh on Windows. We can either install zsh on WSL or replace the git bash shell with zsh. See Git Bash vs WSL.

How do I know if zsh is installed on my Mac? ›

macOS. Most versions of macOS ship with zsh pre-installed. You can check if this is the case and if so, which version you are running using the command: zsh --version . If the version is 4.3.

How do I uninstall zsh from my Mac? ›

“uninstall zsh macos” Code Answer's
  1. rm -rf ~/.oh-my-zsh.
  2. rm ~/.zshrc.
  3. cp ~/.zshrc.pre-oh-my-zsh ~/. zshrc.
  4. source ~/.zshrc.

Can I use homebrew with zsh? ›

Homebrew comes with completion definitions for the brew command. Some packages also provide completion definitions for their own programs. zsh , bash and fish are currently supported.

Is my shell bash or zsh? ›

Update your Terminal preferences to open the shell with the command /bin/bash , as shown in the screenshot above. Quit and restart Terminal. You should see “hello from bash”, but if you run echo $SHELL , you will see /bin/zsh .

Where is zsh installed? ›

oh-my-zsh directory. At installation, it resides at the base of your home directory, but modern Linux convention, as defined by the Free Desktop specification, is to place directories that extend the functionality of applications in the ~/. local/share directory. You can change it in ~/.

How do I start zsh? ›

Open the terminal, click Terminal -> Preferences and for the setting “Shells open with”, select the option “Command (complete path)” and enter “/bin/zsh”. Close and launch a new Terminal window. The shell should be zsh. You will be prompted to update your default shell to zsh.

What are plugins for zsh? ›

Oh My Zsh is an open source framework for Zshell with many themes and plugins.
...
  • Zsh Autosuggestions. ...
  • Sudo. ...
  • Web Search. ...
  • Copydir. ...
  • Copyfile. ...
  • Copybuffer. ...
  • Dirhistory. ...
  • Zsh Reload.
19 Jul 2021

What do oh my zsh plugins do? ›

Oh My Zsh comes bundled with plugins, which allow you to take advantage of functionality of many sorts to your shell just by enabling them. They are each documented in the README file in their respective plugins/ folder. NOTE: elements in zsh arrays are separated by whitespace (spaces, tabs, newlines...).

How do I install oh my zsh on Mac? ›

You can install this via the command-line with either curl or wget.
  1. Install oh-my-zsh via curl.
  2. Install oh-my-zsh via wget.

Where are Zsh plugins stored? ›

Custom plugins can be installed at ~/. oh-my-zsh/custom/plugins . To use a plugin, you can simply add it to the plugins list in your ~/. zshrc file.

Is fish better than Zsh? ›

Fish, or the “Friendly Interactive Shell,” is the most user-friendly and interactive shell, in my opinion. It is much more customizable than Zsh and Bash. It has a ton of cool features like consistent syntax, nice tab completion and syntax highlighting, is easy to pick up and use, and has excellent runtime help.

How do I open a .zshrc file on a Mac? ›

zshrc doesn't exist by default in macOS so you need to create it. The ~/ translates to your user's home directory and the .
...
Steps for creation:
  1. Open Terminal.
  2. Type touch ~/. zshrc to create the respective file. ( touch command will create the . zshrc in your current directory but it will be hidden)
  3. Hit Return.

What is Zsh autoload? ›

The autoload command essentially instructs Zsh to register a function (called clock) using the contents of the clock file that is located in a directory somewhere along the fpath.

Is zsh better than bash? ›

Zsh is more interactive and customizable than Bash. Zsh has floating-point support that Bash does not possess. Hash data structures are supported in Zsh that are not present in Bash. The invocation features in Bash is better when comparing with Zsh.

What is the difference between zsh and bash? ›

A shell is an environment in which various commands can be executed, it provides an interface between the user and the UNIX system.
...
Table of Difference between Bash and Zsh.
BashZsh
It doesn't have many themes and plug-in support.Has plenty of plug-in's and themes.
7 more rows
2 Jun 2022

Why do people use oh my zsh? ›

It is the most popular framework for managing Zsh configuration, plugins, and themes. It will help you transition from using your current shell to Zsh in the easiest way possible. Although Oh My Zsh can do many other things, it is the most famous for its ability to easily manage a ton of themes and plugins.

Which is faster Zsh or fish? ›

With all these glorifying points of Fish, one thing to note is that Fish scripts are slow, slower than Zsh. Thus, anyone who wants to work with faster scripts can choose Zsh with all the plugins installed.

Which shell is best? ›

Top 5 Open-Source Shells for Linux
  1. Bash (Bourne-Again Shell) The full form of the word “Bash” is “Bourne-Again Shell,” and it is one of the best open-source shells available for Linux. ...
  2. Zsh (Z-Shell) ...
  3. Ksh (Korn Shell) ...
  4. Tcsh (Tenex C Shell) ...
  5. Fish (Friendly Interactive Shell)

Is fish slower than Zsh? ›

Initial performance

Fish's performance is a lot better than ZSH's, and very similar to Bash. The results are very impressive: Fish is only a couple ms slower than Bash, but almost 4x faster than ZSH.

What is zsh Mac? ›

The Z shell (also known as zsh ) is a Unix shell that is built on top of bash (the default shell for macOS) with additional features. It's recommended to use zsh over bash . It's also highly recommended to install a framework with zsh as it makes dealing with configuration, plugins and themes a lot nicer.

How do I reset my zsh Mac? ›

To reset your zsh settings to default, just delete your . zshrc file by clicking it once and then pressing ⌘ ⌫ (Cmd-Backspace).

Where is Zshrc stored Mac? ›

On all OS (Mac, Windows, Linux) machines/servers, the . bashrc file typically lives at ~/. bashrc .

What does Compinit do in zsh? ›

Use of compinit

When run, it will define a few utility functions, arrange for all the necessary shell functions to be autoloaded, and will then re-bind all keys that do completion to use the new system.

What is ZLE zsh? ›

The Zsh Line Editor (ZLE) is the command prompt where you can write and edit your commands. The main keymap is the set of keystrokes which is loaded by default when Zsh is launched. The global keymap is the one used to edit commands in Zsh.

What is Vcs_info? ›

Zsh ships vcs_info , a function fetching information about the VCS state for the current directory and populating a variable that can be used in a shell prompt. It supports several VCS , including Git and SVN.

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6060

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.