# Autocomplete in the Terminal with Zsh

**URL:** https://discourse.nixosstag.fcio.net/t/autocomplete-in-the-terminal-with-zsh/55005
**Category:** Guides
**Created:** [October 27, 2024, 12:54pm UTC](https://discourse.nixosstag.fcio.net/t/autocomplete-in-the-terminal-with-zsh/55005 "2024-10-27T12:54:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![tesy](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/tesy/32/28890_2.png) [@tesy](https://discourse.nixosstag.fcio.net/u/tesy)
#### Post date: [October 27, 2024, 12:54pm UTC](https://discourse.nixosstag.fcio.net/t/autocomplete-in-the-terminal-with-zsh/55005/1 "2024-10-27T12:54:03Z")

</div>

Hey! I spent some time working on terminal autocompletion and wanted to share what I found.

![zsh-final](https://discourse.nixosstag.fcio.net/uploads/default/original/3X/e/2/e2abfb8da402e03f8abd611ff207a2f0dbd4c9ed.webp)

I ended up using Zsh, and I’ve put together a minimal reproducible script for Home Manager to set it up. You can check out the short [blog post](https://tesar.tech/blog/2024-10-21_nix_os_zsh_autocomplete) for more background, motivation, and a few extra details.

```nix
{ config, pkgs, ... }:
{
  home.username = "nixos";
  home.homeDirectory = "/home/nixos";

  home.stateVersion = "24.05"; 

  home.packages = with pkgs; [
    oh-my-posh
    git
  ];

  programs.zsh = {
    enable = true;
    plugins = [
      {
        name = "zsh-autocomplete"; # completes history, commands, etc.
        src = pkgs.fetchFromGitHub {
          owner = "marlonrichert";
          repo = "zsh-autocomplete";
          rev = "762afacbf227ecd173e899d10a28a478b4c84a3f";
          sha256 = "1357hygrjwj5vd4cjdvxzrx967f1d2dbqm2rskbz5z1q6jri1hm3";
        }; # e.g., nix-prefetch-url --unpack https://github.com/marlonrichert/zsh-autocomplete/archive/762afacbf227ecd173e899d10a28a478b4c84a3f.tar.gz
      }
    ];

    oh-my-zsh = {
      enable = true;
      plugins = ["z"];
      extraConfig = ''
                # Required for autocomplete with box: https://unix.stackexchange.com/a/778868
                zstyle ':completion:*' completer _expand _complete _ignored _approximate _expand_alias
                zstyle ':autocomplete:*' default-context curcontext 
                zstyle ':autocomplete:*' min-input 0

                setopt HIST_FIND_NO_DUPS

                autoload -Uz compinit
                compinit

                setopt autocd # cd without writing 'cd'
                setopt globdots # show dotfiles in autocomplete list
      '';
    };

    autosuggestion.enable = true;
    syntaxHighlighting.enable = true;
    history = {
      size = 10000;
    };

    # Extra configurations for Zsh
    initExtra = ''
      # Oh-My-Posh initialization for Zsh
      eval "$(oh-my-posh init zsh --config /mnt/c/Users/toted/OneDrive/prog/oh-my-posh/mytheme.omp.json)"

      # zsh-autocomplete
      bindkey -M menuselect '^M' .accept-line # run code when selected completion
        '';
  };

```

---

<div class="post-metadata">

### Author: ![nutrx](https://discourse.nixosstag.fcio.net/user_avatar/discourse.nixosstag.fcio.net/nutrx/32/17524_2.png) [@nutrx](https://discourse.nixosstag.fcio.net/u/nutrx)
#### Post date: [April 20, 2025, 10:15am UTC](https://discourse.nixosstag.fcio.net/t/autocomplete-in-the-terminal-with-zsh/55005/2 "2025-04-20T10:15:20Z")

</div>

A basic config for NixOS without HomeManager:

```auto
  config.programs.zsh = {
    enable = true;
    enableCompletion = false;
    histSize = 10000;
    ohMyZsh = 
      let
        zsh-autocomplete-src = pkgs.fetchFromGitHub {
          owner = "marlonrichert";
          repo = "zsh-autocomplete";
          rev = "25.03.19";
          sha256 = "sha256-eb5a5WMQi8arZRZDt4aX1IV+ik6Iee3OxNMCiMnjIx4=";
        };
        # remove run-tests.zsh
        zsh-autocomplete = pkgs.runCommand "zsh-autocomplete-plugin" {} ''
          mkdir -p $out
          cp -r ${zsh-autocomplete-src}/. $out
          rm $out/run-tests.zsh
        '';
      in
      {
        enable = true;  
        theme = "robbyrussell";
        custom = "${zsh-autocomplete}";
      };
    shellAliases = { };
  };

```
