Zsh autocompletion: Difference between revisions

From wikinotes
Line 86: Line 86:
</source>
</source>
</blockquote><!-- filesystem and setup -->
</blockquote><!-- filesystem and setup -->
== Special Filetypes ==
<blockquote>
Standard executables are recognized on for autocompletion ('git' uses '_git').
This gets complicated with aliases and scripts with extensions (ex: script.py).
You can manually tell compinit to be used for certain files with the compinit
command.
<source lang="bash">
compdef _wsearch wsearch.py
#### _wsearch
#compdef wsearch wsearch.py -p *.py                            ## the '-p' flag to compinit makes it a regex match
</source>
</blockquote><!-- Special Filetypes -->


== Actions ==
== Actions ==

Revision as of 22:47, 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
zsh completion _arguments

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.)