zsh-autocomplete
🤖 Real-time type-ahead completion for Zsh. Asynchronous find-as-you-type autocompletion.
Top Related Projects
Fish-like autosuggestions for zsh
Additional completion definitions for Zsh.
Replace zsh's default completion selection menu with fzf!
Feature-rich syntax highlighting for ZSH
🙃 A delightful community-driven (with 2,300+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, python, etc), 140+ themes to spice up your morning, and an auto-update tool that makes it easy to keep up with the latest updates from the community.
A Zsh theme
Quick Overview
zsh-autocomplete is a plugin for Zsh that provides real-time, as-you-type autocompletion suggestions. It enhances the default Zsh completion system by offering more intuitive and responsive autocompletion, making command-line interactions faster and more efficient.
Pros
- Real-time suggestions appear as you type, reducing the need for manual tab completion
- Integrates seamlessly with existing Zsh configurations and other plugins
- Customizable appearance and behavior to suit individual preferences
- Improves productivity by reducing typing errors and speeding up command entry
Cons
- May slow down terminal performance on older systems or with very large completion lists
- Requires some initial setup and configuration to work optimally
- Can be overwhelming for new users due to the constant display of suggestions
- Might conflict with other completion-related plugins or custom configurations
Getting Started
To install zsh-autocomplete, follow these steps:
-
Clone the repository into your Zsh plugins directory:
git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git $ZSH_CUSTOM/plugins/zsh-autocomplete
-
Add the plugin to your
.zshrc
file:plugins=(... zsh-autocomplete)
-
Restart your Zsh session or source your
.zshrc
file:source ~/.zshrc
For optimal performance, it's recommended to add the following line to your .zshrc
file before loading the plugin:
zstyle ':autocomplete:*' min-input 1
This will set the minimum number of characters required before autocompletion suggestions appear.
Competitor Comparisons
Fish-like autosuggestions for zsh
Pros of zsh-autosuggestions
- Lightweight and focused on providing suggestions based on command history
- Simple setup and configuration
- Works well with other ZSH plugins and frameworks
Cons of zsh-autosuggestions
- Limited to suggesting based on command history only
- Lacks advanced features like menu completion or real-time completion
Code Comparison
zsh-autosuggestions:
# Basic usage
source /path/to/zsh-autosuggestions.zsh
# Customize suggestion color
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
zsh-autocomplete:
# Basic usage
source /path/to/zsh-autocomplete.plugin.zsh
# Customize menu style
zstyle ':completion:*:default' menu select=0
Key Differences
- zsh-autocomplete offers a more comprehensive completion system, including real-time suggestions and menu-based completion
- zsh-autosuggestions focuses solely on providing suggestions based on command history
- zsh-autocomplete may have a steeper learning curve due to its advanced features
- zsh-autosuggestions is generally easier to set up and use out of the box
Performance Considerations
- zsh-autosuggestions tends to have a smaller impact on shell startup time
- zsh-autocomplete may introduce some latency in command-line interactions due to its more complex features
Integration with Other Tools
- Both plugins can be used alongside other ZSH frameworks and plugins
- zsh-autocomplete may require more configuration to work seamlessly with certain setups
Additional completion definitions for Zsh.
Pros of zsh-completions
- Extensive collection of completion definitions for various commands and tools
- Community-driven with contributions from many users
- Lightweight and focused solely on providing completions
Cons of zsh-completions
- Requires manual configuration and integration with your Zsh setup
- Does not provide real-time autocompletion features
- May require occasional updates to keep completions current
Code comparison
zsh-completions:
# In .zshrc
fpath=(path/to/zsh-completions/src $fpath)
autoload -U compinit && compinit
zsh-autocomplete:
# In .zshrc
source path/to/zsh-autocomplete/zsh-autocomplete.plugin.zsh
zsh-completions focuses on providing a wide range of completion definitions that users can integrate into their existing Zsh setup. It requires manual configuration and doesn't offer real-time autocompletion.
zsh-autocomplete, on the other hand, provides a more comprehensive autocompletion experience with real-time suggestions and easier setup. However, it may have a higher performance impact and could potentially conflict with other Zsh plugins or customizations.
The choice between these two depends on whether you prefer a lightweight solution with extensive completions (zsh-completions) or a more feature-rich autocompletion experience (zsh-autocomplete).
Replace zsh's default completion selection menu with fzf!
Pros of fzf-tab
- Utilizes fzf for fuzzy finding, offering more powerful and flexible search capabilities
- Provides a visual selection menu for completions, enhancing user experience
- Supports custom keybindings and actions for selected items
Cons of fzf-tab
- Requires fzf to be installed separately, adding a dependency
- May have a steeper learning curve due to additional configuration options
- Potentially slower for simple completions compared to zsh-autocomplete
Code Comparison
zsh-autocomplete:
zstyle ':autocomplete:*' min-input 1
zstyle ':autocomplete:*' insert-unambiguous yes
zstyle ':autocomplete:*' widget-style menu-select
fzf-tab:
zstyle ':fzf-tab:*' fzf-command fzf
zstyle ':fzf-tab:*' fzf-preview 'bat --color=always --style=numbers --line-range=:500 {}'
zstyle ':fzf-tab:*' switch-group ',' '.'
Both plugins aim to enhance Zsh's tab completion system, but they take different approaches. zsh-autocomplete focuses on providing a more streamlined and automatic completion experience, while fzf-tab leverages the power of fzf to offer a more interactive and customizable completion interface. The choice between the two depends on personal preferences and workflow requirements.
Feature-rich syntax highlighting for ZSH
Pros of fast-syntax-highlighting
- Offers real-time syntax highlighting for commands as you type
- Supports a wide range of Zsh syntax elements and plugins
- Highly customizable with various themes and highlighting styles
Cons of fast-syntax-highlighting
- Focuses solely on syntax highlighting, lacking autocomplete functionality
- May have a steeper learning curve for customization compared to zsh-autocomplete
Code comparison
fast-syntax-highlighting:
# Enable the plugin
source /path/to/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh
# Customize highlighting styles
FAST_HIGHLIGHT_STYLES[alias]='fg=blue,bold'
FAST_HIGHLIGHT_STYLES[command]='fg=green,bold'
zsh-autocomplete:
# Enable the plugin
source /path/to/zsh-autocomplete/zsh-autocomplete.plugin.zsh
# Customize autocomplete behavior
zstyle ':autocomplete:*' min-input 2
zstyle ':autocomplete:*' widget-style menu-select
Summary
fast-syntax-highlighting excels in providing real-time syntax highlighting for Zsh, offering extensive customization options and support for various syntax elements. However, it lacks the autocomplete functionality provided by zsh-autocomplete. On the other hand, zsh-autocomplete focuses on enhancing the shell's autocomplete features but doesn't offer syntax highlighting. The choice between the two depends on whether you prioritize syntax highlighting or improved autocompletion in your Zsh environment.
🙃 A delightful community-driven (with 2,300+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, python, etc), 140+ themes to spice up your morning, and an auto-update tool that makes it easy to keep up with the latest updates from the community.
Pros of Oh My Zsh
- Extensive plugin ecosystem with a wide range of pre-configured options
- Large, active community providing support and contributions
- Includes themes for customizing the shell appearance
Cons of Oh My Zsh
- Can be slower to load due to its large codebase and numerous features
- May require more configuration to achieve desired functionality
- Potential for conflicts between plugins or with existing user configurations
Code Comparison
Oh My Zsh (example plugin activation):
plugins=(git docker kubectl)
source $ZSH/oh-my-zsh.sh
zsh-autocomplete (installation and activation):
source path/to/zsh-autocomplete.plugin.zsh
Summary
Oh My Zsh is a comprehensive framework for managing Zsh configuration, offering a vast array of plugins and themes. It provides a rich set of features out-of-the-box but may impact shell startup time and require more setup.
zsh-autocomplete, on the other hand, is a focused plugin that enhances Zsh's autocomplete functionality. It's lightweight and easy to integrate, but lacks the extensive feature set and community support of Oh My Zsh.
The choice between the two depends on whether you prefer a full-featured framework (Oh My Zsh) or a specialized autocomplete solution (zsh-autocomplete) that can be combined with other plugins of your choice.
A Zsh theme
Pros of powerlevel10k
- Highly customizable and feature-rich prompt theme
- Fast performance, even with complex configurations
- Excellent out-of-the-box experience with configuration wizard
Cons of powerlevel10k
- Focused solely on prompt customization, not general Zsh functionality
- May require more setup time for advanced configurations
Code comparison
powerlevel10k:
source ~/powerlevel10k/powerlevel10k.zsh-theme
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
zsh-autocomplete:
source ~/zsh-autocomplete/zsh-autocomplete.plugin.zsh
# Optional: Customize autocomplete behavior
zstyle ':autocomplete:*' min-input 2
zstyle ':autocomplete:*' widget-style menu-select
Key differences
- Purpose: powerlevel10k is a prompt theme, while zsh-autocomplete enhances Zsh's autocomplete functionality
- Scope: powerlevel10k focuses on visual customization, zsh-autocomplete improves command-line interaction
- Integration: powerlevel10k works well with other Zsh plugins, zsh-autocomplete may conflict with some completion plugins
Both projects enhance the Zsh experience but serve different purposes. powerlevel10k provides a highly customizable and performant prompt, while zsh-autocomplete improves the shell's autocomplete functionality. Users may choose one or both depending on their specific needs and preferences.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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 Tab to insert the top completion or â to select a different one.
Additional features:
- Out-of-the-box configuration of Zsh's completion system
- Multi-line history search
- Completion of recent directories
- Useful keyboard shortcuts
- Easy to configure
Enjoy using this software? Become a sponsor! ð
Requirements
Recommended:
- Tested to work with Zsh 5.8 and newer.
Minimum:
- Should theoretically work with Zsh 5.4, but I'm unable to test that.
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:
- To use only releases (instead of the
main
branch), installzsh-autocomplete
with a package manager. As of this writing, this package is available through Homebrew, Nix,pacman
, Plumage, and (asapp-shells/zsh-autocomplete
) Portage. - To always use the latest commit on the
main
branch, do one of the following:- Use
pacman
to installzsh-autocomplete-git
. - Use a Zsh plugin manager to install
marlonrichert/zsh-autocomplete
. (If you don't have a plugin manager yet, I recommend using Znap.) - Clone the repo directly:
% git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git
- Use
After installing, make the following modifications to your shell config:
- In your
.zshrc
file:- Remove any calls to
compinit
. - Add near the top, before any calls to
compdef
:source /path/to/zsh-autocomplete/zsh-autocomplete.plugin.zsh
- Remove any calls to
- When using Ubuntu, add to your
.zshenv
file:skip_global_compinit=1
- When using Nix, add to your
home.nix
file:programs.zsh.enableCompletion = false;
Finally, restart your shell. Here's two ways to do so:
- Open a new tab or window in your terminal.
- Replace the current shell with a new one:
% exec zsh
Updating
If you installed manually, run:
% git -C ~autocomplete pull
Otherwise, simply use your package manager or plugin manager's update mechanisms.
Uninstalling
- Revert the actions you took to install.
- Restart your shell.
Keyboard shortcuts
main | emacs | vicmd | On the command line | In the menus |
---|---|---|---|---|
Enter Return | Exit menu text search or exit menu | |||
Tab | Insert first listed menu item | Exit menu text search or exit menu | ||
ShiftTab | Insert substring occurring in all listed completions | Exit menu text search or exit menu | ||
â | CtrlN | J | Cursor down or enter completion menu | Change selection |
â | CtrlP | K | Cursor up or enter history menu | Change selection |
Altâ | AltN | CtrlN | Enter completion menu | Next section |
Altâ | AltP | CtrlP | Enter history menu | Previous section |
PgDn | Page down | |||
PgUp | Page up | |||
CtrlX / | Toggle recent path search | |||
CtrlR | / | Toggle history search | Start menu text search or go to previous match | |
CtrlS | ? | Start menu text search | Start menu text search or go to next match | |
CtrlSpace | V | Toggle selection mode | Add another item | |
Ctrl- Ctrl/ | U | Undo last item | ||
CtrlG | Undo all added items |
Caveats
main
is whichever keymap was aliased tomain
when Autocomplete was sourced.- By default, this is
emacs
. - If you run
bindkey -v
before sourcing Autocomplete, thenmain
will beviins
when Autocomplete installs keybindings.
- By default, this is
- Plugins or other scripts that you load after loading Autocomplete may override these bindings.
If you find that some shortcuts don't work as expected, then you can fix them by
- changing the order in which you source your plugins or by
- running
bindkey
commands in your dotfiles after you source your plugins.
- Depending on your terminal, not all keybindings might be available to you.
- Instead of Alt, your terminal might require you to press Escape, Option or Meta.
- In the menus, the bindings listed under
vicmd
require you to press Alt for each, instead of just once. - The bindings listed under
emacs
andvicmd
are always both active in the menus, no matter which keymap you actually use. This is a limitation of Zsh. - What any other keys do while you're in a menu depends on the keymap from which you opened the menu. See the Zsh manual section on menu selection for more info.
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 Tab and ShiftTab cycle completions on the command line
This makes Tab and ShiftTab, 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 Tab and ShiftTab go to the menu
This makes Tab and ShiftTab, when pressed on the command line, enter the menu instead of inserting a completion:
bindkey '^I' menu-select
bindkey "$terminfo[kcbt]" menu-select
Make Tab and ShiftTab change the selection in the menu
This makes Tab and ShiftTab 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 â and â always move the cursor on the command line
This makes â and â 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 Enter always submit the command line
This makes Enter 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 Enter submit the command line straight from the menu
By default, pressing Enter in the menu search exits the search and pressing it otherwise in the menu exits the menu. If you instead want to make Enter 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 CtrlR 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 PgUp and PgDn to scroll through the excess lines. (Note: On some terminals, you have to additionally hold Shift 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.
Top Related Projects
Fish-like autosuggestions for zsh
Additional completion definitions for Zsh.
Replace zsh's default completion selection menu with fzf!
Feature-rich syntax highlighting for ZSH
🙃 A delightful community-driven (with 2,300+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, python, etc), 140+ themes to spice up your morning, and an auto-update tool that makes it easy to keep up with the latest updates from the community.
A Zsh theme
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot