Zsh autocompletion: Difference between revisions

From wikinotes
 
(5 intermediate revisions by the same user not shown)
Line 30: Line 30:
</blockquote><!-- Locations -->
</blockquote><!-- Locations -->


= Loading Completions =
= Notes =
<blockquote>
<blockquote>
<source lang="bash">
{|
fpath=(~/.zsh/completion $fpath) # add completion dir to $fpath
| [[zsh completion configuration]]
autoload -U compinit            # init autocompletion
|}
compinit
</blockquote><!-- Configuration -->
</source>
 
The cleanest way to reload your autocompletion script is:
 
* open a new terminal
* <code>autoload -U ~/.zsh/completion/_yourcompleter:t</code>
</blockquote><!-- Loading Completions -->
 


= Syntax =
= Syntax =
Line 51: Line 43:
|-
|-
| [[zsh completion testing]]
| [[zsh completion testing]]
|-
| [[zsh completion examples]]
|-
|-
!colspan=1| Completer Functions
!colspan=1| Completer Functions
Line 62: Line 56:
|}
|}
</blockquote><!-- Syntax -->
</blockquote><!-- Syntax -->
= Syntax =
<blockquote>
== Basics ==
<blockquote>
Now that we have created <code>~/.zsh/completion</code>, and have added it to <code>$fpath</code>,
you can start creating autocompletion files. First, some things to know:
* zsh autocompletion scripts are written in SHELL. <code>_arguments</code> 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.
</blockquote><!-- basics -->
== Filesystem and Setup ==
<blockquote>
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>'
<source lang="bash">
touch ~/.zsh/completion/_hello
#### ~/.zsh/completion/_hello
#compdef hello
...
####
</source>
</blockquote><!-- filesystem and setup -->
== Actions ==
<blockquote>
<source lang="bash">
_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.)
</source>
</blockquote><!-- Actions -->

Latest revision as of 22:58, 24 July 2021