Zsh autocompletion: Difference between revisions

From wikinotes
Line 51: Line 51:
|-
|-
| [[zsh completion testing]]
| [[zsh completion testing]]
|-
!colspan=1| Completer Functions
|-
|-
| [[zsh completion _arguments]]
| [[zsh completion _arguments]]
|-
| [[zsh completion _describe]]
|-
| [[zsh completion _alternative]]
|-
|-
|}
|}

Revision as of 22:49, 24 July 2021

Documentation

official docs http://zsh.sourceforge.net/Doc/Release/Completion-System.html

Tutorials

completion funcs https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org
intro to autocomp https://askql.wordpress.com/2011/01/11/zsh-writing-own-completion/
intermediate autocomp http://www.linux-mag.com/id/1106/

Locations

/usr/share/zsh/functions/Completion/*/* completion scripts (see $fpath)

Loading Completions

fpath=(~/.zsh/completion $fpath) # add completion dir to $fpath
autoload -U compinit             # init autocompletion
compinit

The cleanest way to reload your autocompletion script is:

  • open a new terminal
  • autoload -U ~/.zsh/completion/_yourcompleter:t


Syntax

zsh completion basics
zsh completion testing
Completer Functions
zsh completion _arguments
zsh completion _describe
zsh completion _alternative

Syntax

Basics

Now that we have created ~/.zsh/completion, and have added it to $fpath, you can start creating autocompletion files. First, some things to know:

  • zsh autocompletion scripts are written in SHELL. _arguments is a function written in shellscript, and each argument you add to it is an argument against the shell script. That should give you some hints about the expected syntax, and how it is expanded.
  • zsh autocompletions do not need to be paired with any actual function. It compares the word typed on the shell against the list.
  • zsh autocompletion scripts are sourced once, when the shell is first entered. You can reload them using a custom function (see above)

I highly recommend reading Completion/Unix/_zpool. It is fairly short, and very understandable.

Filesystem and Setup

Each program has it's own autocompletion script. It is named after the program with a prefix of '_'. The first line in the program should be '#compdef <programName>'

touch ~/.zsh/completion/_hello

#### ~/.zsh/completion/_hello
#compdef hello

...
####

Actions

_normal                --> A normal argument. Expects a word (this seems to be the default)
_files                 --> Complete filepaths
_net_interfaces        --> Net Interfaces
_users                 --> users
_groups                --> groups
_parameters            --> expect a word argument to follow (can be file, word... pretty much anything. Can be restricted.)