How to setup zsh autocompletion

Autocompletion enhances the user experience in a shell environment by saving time, improving accuracy, boosting productivity, and facilitating exploration

Autocompletion is a feature in a shell that automatically suggests and completes commands, file names, paths, and other inputs as you type, simply by pressing the Tab key. Autocompletion offers several important benefits in a shell environment:

  • Time-saving: it significantly reduces the amount of typing required. Instead of manually typing out long commands, paths, or file names, you can type a few characters and let the shell complete the rest. This saves time and reduces the likelihood of errors that can occur due to manual input.
  • Efficiency and productivity: By providing suggestions, autocompletion helps users discover available commands, options, and file names without the need to consult documentation or memorize them. It improves efficiency by reducing the cognitive load of remembering exact names or paths, allowing users to work more quickly and productively.
  • Accuracy and avoidance of typos: Autocompletion minimizes typos and syntax errors. When you rely on autocompletion, the shell ensures that the completed input matches existing commands, files, or paths. This reduces the likelihood of making mistakes due to misspellings or incorrect syntax.
  • Exploration and discovery: it can be a helpful tool for exploration and discovery within the shell environment. As you type, the shell suggests available options or completions based on the context. This allows you to explore commands, options, and file system structures without needing to refer to external documentation or execute trial-and-error attempts.
  • Consistency: it promotes consistency across commands and file names. When you rely on autocompletion, you're more likely to use the correct and consistent names, which can be particularly valuable in environments where there are multiple similar commands or files with similar names.
  • Complex argument handling: Autocompletion can assist with handling complex command arguments. In many shells, autocompletion can intelligently suggest and complete arguments for specific commands. This is particularly useful when dealing with commands that require specific options, flags, or arguments, saving you from the burden of remembering or looking up the exact syntax.

Although there are many powerful autocompletion plugins for zsh like the following:

zsh-autocomplete

zsh-autocomplete plugin

zsh-autosuggestions

zsh-autosuggestions plugin
I prefer to have a minimal configuration for the completion system of zsh. That is why I use the following configuration (.zshrc):

autoload -Uz compinit
compinit

zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=2
eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'      
      
It provides autocorrection:
 
zsh autocorrection

In case it cannot automatically correct the miswritten word, it will give you options:

zsh correction options

You can navigate through the options:

zsh navigate correction options

It also has autocompletion for killing a process:

zsh kill

You can find all the configuration options of the zsh completion system in this link.

Popular posts from this blog

How to setup NeoVim configuration file

WebAssembly (Wasm): Fixing the Flaws of Applets

How to query Outlook from PowerShell