zsh-autocomplete

repository·main·Indexed 27 days ago

https://github.com/marlonrichert/zsh-autocomplete

A Zsh plugin providing real-time, type-ahead autocompletion similar to modern desktop applications. It automates the Zsh completion system and enhances navigation through menus and history. The plugin supports multiple keymaps (main, emacs, vicmd) and is highly configurable via zstyle for timing, input requirements, and common substring insertion.

Tokens
2.5K
Snippets
11
Records
14
Agent score
42%

What's inside zsh-autocomplete

  1. Reassign keybindings using bindkey

    main

    You can customize keybindings by using Zsh's bindkey command after loading Autocomplete. Common customizations include:

    • Cycle completions with Tab/Shift-Tab: Cycles through completions without changing the menu.
    • Enter the menu with Tab/Shift-Tab: Instead of inserting a completion, enters the selection menu.
    • Change selection in menu: Use Tab/Shift-Tab to move through the menu items.
    • Move cursor with arrows: Ensures arrow keys move the cursor even when inside a menu.
    • Submit command with Enter: Makes Enter always submit the command line, even when in a menu.
    • Restore Zsh-default history shortcuts: Restores standard Emacs or Vi-style history navigation.
    # Make <kbd>Tab</kbd> and <kbd>Shift</kbd><kbd>Tab</kbd> cycle completions
    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
    bindkey              '^I' menu-select
    bindkey "$terminfo[kcbt]" menu-select
    
    # Make <kbd>Enter</kbd> always submit the command line
    bindkey -M menuselect '^M' .accept-line
  2. Configure zsh-autocomplete in .zshrc

    main

    After installation, you must modify your .zshrc to integrate the plugin correctly:

    1. Remove any existing calls to compinit.
    2. Add the following line near the top of your .zshrc, specifically before any calls to compdef:
    source /path/to/zsh-autocomplete/zsh-autocomplete.plugin.zsh
    1. Restart your shell by opening a new terminal or running:
    % exec zsh
  3. Install zsh-autocomplete

    main

    You can install zsh-autocomplete using a package manager for stable releases, or via Git/Plugin Managers for the latest features.

    Package Managers

    Available via: Homebrew, Nix, pacman, Plumage, and Portage (as app-shells/zsh-autocomplete).

    Latest Commit (main branch)

    • Arch Linux (AUR): Use yay -S zsh-autocomplete-git.
    • Zsh Plugin Manager: Use a manager like [Znap] to install marlonrichert/zsh-autocomplete.
    • Manual Git Clone:
      % git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git
    % git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git
  4. OS-Specific Configuration for zsh-autocomplete

    main

    Depending on your operating system or shell manager, additional configuration is required to prevent conflicts with the built-in Zsh completion system.

    Ubuntu

    Add this to your .zshenv file:

    skip_global_compinit=1

    Nix

    Add this to your home.nix file:

    programs.zsh.enableCompletion = false;
  5. Configure autocompletion timing and input requirements

    main

    Adjust how Autocomplete triggers to balance responsiveness and performance:

    • delay: Wait X seconds after typing stops before fetching completions (default is ~0.05s).
    • timeout: Maximum time (in seconds) to wait for completion to finish before timing out (default is 1s).
    • min-input: Minimum number of characters required before autocompletion starts.
    • ignored-input: A pattern that, if matched by the current word, prevents completions from showing.
    zstyle ':autocomplete:*' delay 0.1
    zstyle ':autocomplete:*' timeout 2.0
    zstyle ':autocomplete:*' min-input 3
    zstyle ':autocomplete:*' ignored-input '..##'
  6. Configure space and semicolon behavior

    main

    Autocomplete allows you to control when spaces and semicolons are automatically inserted during completion.

    • Add space after completions: By default, spaces are added after executables, aliases, functions, builtins, reserved-words, and commands. Set to '*' to always add a space, or an empty list to never add one.
    • Disable history semicolons: By default, Autocomplete adds a semicolon to history lines to support Ctrl+Space. Disable this using add-semicolon no.
  7. Configure the number of lines shown in menus

    main

    You can override the default limits for how many lines are displayed in completion lists and history menus using zstyle -e (to allow dynamic values like $LINES).

    • Global override: Affects all listings.
    • Context-specific overrides: Can be applied to recent-paths, history-incremental-search-backward, or history-search-backward.
    # Override default for all listings (e.g., 1/3 of screen height)
    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
  8. Customize the common substring message format

    main

    You can change how the common substring is displayed using zstyle. The default format uses %d to represent the substring. You can use Zsh prompt escape sequences like %B (bold), %F (foreground color), %K (background color), %S (standout), and %U (underline) to style it.

    builtin zstyle ':autocomplete:*:unambiguous' format \
        $'%{\e[0;2m%}%Bcommon substring:%b %0F%11K%d%f%k'
  9. Configure common substring insertion

    main

    You can configure Autocomplete to first insert the longest sequence of characters that is common to all completions (the 'unambiguous' substring) before inserting the actual completions.

    To enable this for all widgets, history widgets, or menu search, use zstyle ':autocomplete:*:complete*:*' insert-unambiguous yes.

    To insert only the longest prefix instead of the full substring, add the following matcher list configuration:

    zstyle ':completion:*:*' matcher-list 'm:{[:lower:]-}={[:upper:]_}' '+r:|[.]=**'
    # 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