How To Set Up History-Based Autocompletion in Zsh
Tags: zsh, linux, terminal, macos
Published: 2021-07-28 10:27:40

---
title: How To Set Up History-Based Autocompletion in Zsh
published: true
description: (Spoiler: No need to install Oh My Zsh framework)
tags: zsh, linux, terminal, macos
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49cnu3btaote9zcoy1j1.png
---

Yes, [Oh My Zsh](https://ohmyz.sh/) is awesome! That’s the first thing I installed when I switched from Bash to Zsh and I used it for a few years.

Past that time, I realized that in my daily use, the only features I was taking advantage was:

- **Autocompletion** and **history-based autocompletion** using the arrow keys.

- The fancy **multi-line** and **colorful user prompt** showing the **working directory**, and the **switching color** after the **fail/success** of the previous **command execution**.

- The **git repository info** at the **user prompt**.

- The `z` command, provided by the **[ZSH-z](https://github.com/agkozak/zsh-z) plugin**.

I couldn’t help myself but thinking that the Oh My Zsh framework was much more than I needed. I decided to remove Oh My Zsh and reset Zsh from scratch, so I could configure and install only the features I needed.

**This is a four-part post series explaining how to set up those features on a fresh new Zsh installation:**

Customize Zsh Pt.1 - Autocompletion 👈

[Customize Zsh Pt.2 - User Prompt](https://alldrops.info/posts/cli-drops/2021-07-26_customize-zsh-part-2/)

[Customize Zsh Pt.3 - Git Info](https://alldrops.info/posts/cli-drops/2021-07-26_customize-zsh-part-3/)

[Customize Zsh Pt.4 - ZSH-z Plugin](https://alldrops.info/posts/cli-drops/2021-07-26_customize-zsh-part-4/)

------

### Autocompletion

Zsh has a powerful completion system built-in by default. You need to load and initialize to take advantage of it. [There is a lot to learn about it](https://thevaluable.dev/zsh-completion-guide-examples/) if you feel like going deeper but this post’s objectives are:

- To enable the standard autocompletion.

- To set up history-based autocompletion.

#### How to Set Up

After a Zsh fresh install it runs a helper for the first time you log into the shell:

```
This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

--- Type one of the keys in parentheses --- 
```

Avoid using the helper and apply the settings inside the .zshrc file, so:

- Type `0` to exit the Zsh helper creating a blank `.zshrc` file in your `$HOME` directory.

- To load and initialize the Zsh completion system, open the `.zshrc` file in your code editor and add the following line at the top of the file:

```
# AUTOCOMPLETION

# initialize autocompletion
autoload -U compinit && compinit
```

_(Learn more: `man zshcompsys` and go to `Use of compinit`)_

- To allow history-based autocompletion, first, apply some configurations to improve Zsh’s history management by adding the following lines to the `.zshrc` file:

```
# history setup
setopt SHARE_HISTORY
HISTFILE=$HOME/.zhistory
SAVEHIST=1000
HISTSIZE=999
setopt HIST_EXPIRE_DUPS_FIRST
```

_(Read options and parameters descriptions: `man zshoptions` and `man zshparam`)_

- With Zsh history set, create key bindings to use up and down arrow keys to navigate history for the provided command:

```
# autocompletion using arrow keys (based on history)
bindkey '\e[A' history-search-backward
bindkey '\e[B' history-search-forward
```

The code above assumes that `[A` is the value your terminal emulator sends for the keyboard up arrow, and that `[B` is the value for the keyboard down arrow.

You can double check by pressing `<Ctrl>` + `v` and `<up-arrow>` in your Zsh prompt, [see more here](https://unix.stackexchange.com/questions/116562/key-bindings-table?rq=1).

_(Learn more: `man zshzle` and go to `ZLE BUILTINS` for key bindings, and go to `history-search-`.)_

- Source the `.zshrc` file again, in the Zsh shell type:

```
source ~/.zshrc
```

Autocompletion is ready to go!

![gif01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpqkdh6uxfk6hv7qbro3.gif)

The final `.zshrc` file must look like this:

```
# AUTOCOMPLETION

# initialize autocompletion
autoload -U compinit
compinit

# history setup
setopt APPEND_HISTORY
setopt SHARE_HISTORY
HISTFILE=$HOME/.zhistory
SAVEHIST=1000
HISTSIZE=999
setopt HIST_EXPIRE_DUPS_FIRST
setopt EXTENDED_HISTORY

# autocompletion using arrow keys (based on history)
bindkey '\e[A' history-search-backward
bindkey '\e[B' history-search-forward

# GENERAL

# (bonus: Disable sound errors in Zsh)

# never beep
setopt NO_BEEP
```

> _With those simple steps autocompletion is ready and your Zsh shell is becoming more powerful._
>
> _Part 2 explores how to apply a simple configuration to improve the user prompt._

------

### Next:

#### [Customize Zsh Pt.2 - User Prompt](https://alldrops.info/posts/cli-drops/2021-07-26_customize-zsh-part-2/)

------

#### Useful links & references:

- [Zsh Documentation](https://man.archlinux.org/listing/extra/zsh/) (man -k zsh to list each Zsh man page section)

- [key bindings table?](https://unix.stackexchange.com/questions/116562/key-bindings-table?rq=1)

- [A Guide to the Zsh Completion With Examples](https://thevaluable.dev/zsh-completion-guide-examples/)

------

#### Footnotes:

- _This post appeared first at [alldrops.info](https://alldrops.info/)._

- _[Follow me on Twitter](https://twitter.com/rossijonas) to get more posts like this and other quick tips in your feed._

- _If you have any doubts or tips about this post, I’d appreciate knowing and discussing it in the comments section._

- _As English is not my native language, I apologize for the errors. Corrections are welcome._

- _Big thanks 🙌 to [Eric Nielsen](https://twitter.com/ericbn8) for pointing out some improvements in Zsh history settings._