Home

Awesome

Autocomplete for Zsh

This plugin for Zsh adds real-time type-ahead autocompletion to your command line, similar to what you find desktop apps. While you type on the command line, available completions are listed automatically; no need to press any keyboard shortcuts. Press <kbd>Tab</kbd> to insert the top completion or <kbd></kbd> to select a different one.

Additional features:

Enjoy using this software? Become a sponsor! 💝

Requirements

Recommended:

Minimum:

Installation & setup

Note: In this manual, % represents the command line prompt. If you see it in front of a command, it means you should run it on the command line, not put it in a script.

First, install Autocomplete itself. Here are some way to do so:

After installing, make the following modifications to your shell config:

Finally, restart your shell. Here's two ways to do so:

Updating

If you installed manually, run:

% git -C ~autocomplete pull

Otherwise, simply use your package manager or plugin manager's update mechanisms.

Uninstalling

  1. Revert the actions you took to install.
  2. Restart your shell.

Keyboard shortcuts

mainemacsvicmdOn the command lineIn the menus
<kbd>Enter</kbd><br><kbd>Return</kbd>Exit menu text search or exit menu
<kbd>Tab</kbd>Insert first listed menu itemExit menu text search or exit menu
<kbd>Shift</kbd><kbd>Tab</kbd>Insert substring occurring in all listed completionsExit menu text search or exit menu
<kbd></kbd><kbd>Ctrl</kbd><kbd>N</kbd><kbd>J</kbd>Cursor down or enter completion menuChange selection
<kbd></kbd><kbd>Ctrl</kbd><kbd>P</kbd><kbd>K</kbd>Cursor up or enter history menuChange selection
<kbd>Alt</kbd><kbd></kbd><kbd>Alt</kbd><kbd>N</kbd><kbd>Ctrl</kbd><kbd>N</kbd>Enter completion menuNext section
<kbd>Alt</kbd><kbd></kbd><kbd>Alt</kbd><kbd>P</kbd><kbd>Ctrl</kbd><kbd>P</kbd>Enter history menuPrevious section
<kbd>PgDn</kbd>Page down
<kbd>PgUp</kbd>Page up
<kbd>Ctrl</kbd><kbd>X</kbd> <kbd>/</kbd>Toggle recent path search
<kbd>Ctrl</kbd><kbd>R</kbd><kbd>/</kbd>Toggle history searchStart menu text search or go to previous match
<kbd>Ctrl</kbd><kbd>S</kbd><kbd>?</kbd>Start menu text searchStart menu text search or go to next match
<kbd>Ctrl</kbd><kbd>Space</kbd><kbd>V</kbd>Toggle selection modeAdd another item
<kbd>Ctrl</kbd><kbd>-</kbd><br><kbd>Ctrl</kbd><kbd>/</kbd><kbd>U</kbd>Undo last item
<kbd>Ctrl</kbd><kbd>G</kbd>Undo all added items

Caveats

Configuration

The following are the most commonly requested ways to configure Autocomplete's behavior. To use any of these, add the code shown to your .zshrc file and modify it there, then restart you shell.

Reassign keys

You can use Zsh's bindkey command, after loading Autocomplete, to customize your keybindings. Below are some examples of what you can do with this.

Make <kbd>Tab</kbd> and <kbd>Shift</kbd><kbd>Tab</kbd> cycle completions on the command line

This makes <kbd>Tab</kbd> and <kbd>Shift</kbd><kbd>Tab</kbd>, when pressed on the command line, cycle through listed completions, without changing what's listed in the menu:

bindkey              '^I'         menu-complete
bindkey "$terminfo[kcbt]" reverse-menu-complete

Make <kbd>Tab</kbd> and <kbd>Shift</kbd><kbd>Tab</kbd> go to the menu

This makes <kbd>Tab</kbd> and <kbd>Shift</kbd><kbd>Tab</kbd>, when pressed on the command line, enter the menu instead of inserting a completion:

bindkey              '^I' menu-select
bindkey "$terminfo[kcbt]" menu-select

Make <kbd>Tab</kbd> and <kbd>Shift</kbd><kbd>Tab</kbd> change the selection in the menu

This makes <kbd>Tab</kbd> and <kbd>Shift</kbd><kbd>Tab</kbd> move the selection in the menu right and left, respectively, instead of exiting the menu:

bindkey -M menuselect              '^I'         menu-complete
bindkey -M menuselect "$terminfo[kcbt]" reverse-menu-complete

Make <kbd></kbd> and <kbd></kbd> always move the cursor on the command line

This makes <kbd></kbd> and <kbd></kbd> always move the cursor on the command line, even when you are in the menu:

bindkey -M menuselect  '^[[D' .backward-char  '^[OD' .backward-char
bindkey -M menuselect  '^[[C'  .forward-char  '^[OC'  .forward-char

Make <kbd>Enter</kbd> always submit the command line

This makes <kbd>Enter</kbd> always submit the command line, even when you are in the menu:

bindkey -M menuselect '^M' .accept-line

Restore Zsh-default functionality

Autocomplete overrides the behavior of some of Zsh's built-in keyboard widgets. To use the original widget instead, prefix it with a .:

bindkey '^R' .history-incremental-search-backward
bindkey '^S' .history-incremental-search-forward

Pass arguments to compinit

If necessary, you can let Autocomplete pass arguments to compinit as follows:

zstyle '*:compinit' arguments -D -i -u -C -w

First insert the common substring

You can make any completion widget first insert the longest sequence of characters that will complete to all completions shown, if any, before inserting actual completions:

# all Tab widgets
zstyle ':autocomplete:*complete*:*' insert-unambiguous yes

# all history widgets
zstyle ':autocomplete:*history*:*' insert-unambiguous yes

# ^S
zstyle ':autocomplete:menu-search:*' insert-unambiguous yes

Insert prefix instead of substring

When using the above, if you want each widget to first try to insert only the longest prefix that will complete to all completions shown, if any, then add the following:

zstyle ':completion:*:*' matcher-list 'm:{[:lower:]-}={[:upper:]_}' '+r:|[.]=**'

Note, though, that this will also slightly change what completions are listed initially. This is a limitation of the underlying implementation in Zsh.

Make <kbd>Enter</kbd> submit the command line straight from the menu

By default, pressing <kbd>Enter</kbd> in the menu search exits the search and pressing it otherwise in the menu exits the menu. If you instead want to make <kbd>Enter</kbd> always submit the command line, use the following:

bindkey -M menuselect '\r' .accept-line

Add or don't add a space after certain completions

When inserting a completion, a space is added after certain types of completions. The default list is as follows:

zstyle ':autocomplete:*' add-space \
    executables aliases functions builtins reserved-words commands

Modifying this list will change when a space is inserted. If you change the list to '*', a space is always inserted. If you put no elements in the list, then a space is never inserted.

Start each command line in history search mode

This will make Autocomplete behave as if you pressed <kbd>Ctrl</kbd><kbd>R</kbd> at the start of each new command line:

zstyle ':autocomplete:*' default-context history-incremental-search-backward

Wait for a minimum amount of input

To suppress autocompletion until a minimum number of characters have been typed:

zstyle ':autocomplete:*' min-input 3

Wait with autocompletion until typing stops for a certain amount of seconds

Normally, Autocomplete fetches completions after you stop typing for about 0.05 seconds. You can change this as follows:

zstyle ':autocomplete:*' delay 0.1  # seconds (float)

Don't show completions if the current word matches a pattern

For example, this will stop completions from showing whenever the current word consists of two or more dots:

zstyle ':autocomplete:*' ignored-input '..##'

Change the max number of lines shown

By default, Autocomplete lets the history menu fill half of the screen, and limits all real-time listings to a maximum of 16 lines. You can change these limits as follows:

# Note: -e lets you specify a dynamically generated value.

# Override default for all listings
# $LINES is the number of lines that fit on screen.
zstyle -e ':autocomplete:*:*' list-lines 'reply=( $(( LINES / 3 )) )'

# Override for recent path search only
zstyle ':autocomplete:recent-paths:*' list-lines 10

# Override for history search only
zstyle ':autocomplete:history-incremental-search-backward:*' list-lines 8

# Override for history menu only
zstyle ':autocomplete:history-search-backward:*' list-lines 2000

Note that for all real-time listings, the maximum number of lines is additionally capped to the number of lines that fit on screen. However, there is no such limit for the history menu. If that generates more lines than fit on screen, you can simply use <kbd>PgUp</kbd> and <kbd>PgDn</kbd> to scroll through the excess lines. (Note: On some terminals, you have to additionally hold <kbd>Shift</kbd> or, otherwise, it will scroll the terminal buffer instead.)

Use a custom backend for recent directories

Autocomplete comes with its own backend for keeping track of and listing recent directories (which uses part of cdr under the hood). However, you can override this and supply Autocomplete with recent directories from any source that you like. To do so, define a function like this:

+autocomplete:recent-directories() {
  <code>
  typeset -ga reply=( <any number of absolute paths> )
}

Add a backend for recent files

Out of the box, Autocomplete doesn't track or offer recent files. However, it will do so if you add a backend for it:

+autocomplete:recent-files() {
  <code>
  typeset -ga reply=( <any number of absolute paths> )
}

Troubleshooting

Try the steps in the bug report template.

Author

© 2020-2023 Marlon Richert

License

This project is licensed under the MIT License. See the LICENSE file for details.