Zsh completion basics: Difference between revisions

From wikinotes
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
= File Format =
= Simple Example =
<blockquote>
<blockquote>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 11: Line 11:
   '-o[output file]::outfile:_files' \    # <-- flag param (with 'file' value)
   '-o[output file]::outfile:_files' \    # <-- flag param (with 'file' value)
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- File Format -->
</blockquote><!-- Simple Example -->


= Actions =
= Actions =
Line 26: Line 26:
# some useful actions:
# some useful actions:
_normal      # complete a word
_normal      # complete a word
_parameters  # one or more normal args
_files        # complete filepaths
_files        # complete filepaths
_user        # complete from OS users
_user        # complete from OS users
_groups      # complete from OS groups
_groups      # complete from OS groups
_parameters  # ?
</syntaxhighlight>
</syntaxhighlight>


Line 35: Line 35:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
_arguments \
_arguments \
   '1:name:(alex will maize)'
   '1:name:(alex will maize)' \                                        # completion choices for positional arg
  {-c,--component}":component:(mail calendar contacts tasks memos)" \  # completion choices for keyword arg
 
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Actions -->
</blockquote><!-- Actions -->

Latest revision as of 20:21, 5 November 2023

Simple Example

# compdef backup_user                  # <-- program(s) to complete

_arguments \
  '1:firstname:_normal' \              # <-- positional param 1
  '2:file:_normal'                     # <-- positional param 2
  '3::foo:_normal'                    # <-- optional positional param 3 (2x ::)
  {-h,--help}'[show help]' \          # <-- flag param (no value)
  '-o[output file]::outfile:_files' \    # <-- flag param (with 'file' value)

Actions

Actions are used in params that require a followup value param.
They determine the types of completions that will be suggested.

There is a large variety of builtin functions like these:

# usage
_arguments \
  '1:name:_normal'

# some useful actions:
_normal      # complete a word
_parameters  # one or more normal args
_files        # complete filepaths
_user        # complete from OS users
_groups      # complete from OS groups

You can also provide an array of choices

_arguments \
  '1:name:(alex will maize)' \                                         # completion choices for positional arg
  {-c,--component}":component:(mail calendar contacts tasks memos)" \  # completion choices for keyword arg