From ce1a0eaa58f7ad07c5389996a90c4b02bdf573fc Mon Sep 17 00:00:00 2001 From: johannst Date: Mon, 13 Apr 2020 19:02:58 +0000 Subject: deploy: 43e402ba2320ced7972d33c9442b2745afe230f6 --- print.html | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 132 insertions(+), 6 deletions(-) (limited to 'print.html') diff --git a/print.html b/print.html index d1b2fbb..66f1ebe 100644 --- a/print.html +++ b/print.html @@ -83,7 +83,7 @@ @@ -469,19 +469,145 @@ COMP_CWORD # index into COMP_WORDS with current cursor position # out COMPREPLY # array with possible completions -

compgen builtin is used to generate possible matches for word out of possible options. -The syntax is as follows:

+

The compgen builtin is used to generate possible matches by comparing word +against words generated by option.

compgen [option] [word]
 
 # usefule options:
+# -W <list>    specify list of possible completions
+# -d           generate list with dirs
+# -f           generate list with files
+# -u           generate list with users
+# -e           generate list with exported variables
 
-# -W <list>    compare against word-list
+# compare "f" against words "foo" "foobar" "bar" and generate matches
 compgen -W "foo foobar bar" "f"
 
-# -d    compare against dir names
-# -f    compare against file names
+# compare "hom" against file/dir names and generate matches
 compgen -d -f "hom"
 
+

Example

+

Skeleton to copy/paste for writing simple completions.

+

Assume a program foo with the following interface:

+
foo -c green|red|blue -s low|high -f <file> -h
+
+

The completion handler could be implemented as follows:

+
function _foo() {
+    local curr=$2
+    local prev=$3
+
+    local opts="-c -s -f -h"
+    case $prev in
+        -c) COMPREPLY=( $(compgen -W "green red blue" -- $curr) );;
+        -s) COMPREPLY=( $(compgen -W "low high" -- $curr) );;
+        -f) COMPREPLY=( $(compgen -f -- $curr) );;
+        *)  COMPREPLY=( $(compgen -W "$opts" -- $curr) );;
+    esac
+}
+
+complete -F _foo foo
+
+

zsh(1)

+

Keybindings

+

Change input mode:

+
bindkey -v              change to vi keymap
+bindkey -e              change to emacs keymap
+
+

Define key-mappings:

+
bindkey                 list mappings in current keymap
+bindkey in-str cmd      create mapping for `in-str` to `cmd`
+bindkey -r in-str       remove binding for `in-str`
+
+# C-v <key>             dump <key> code, which can be used in `in-str`
+# zle -l                list all functions for keybindings
+# man zshzle(1)         STANDARD WIDGETS: get description of functions
+
+

Completion

+

Installation

+

Completion functions are provided via files and need to be placed in a location +covered by $fpath. By convention the completion files are names as _<CMD>.

+

A completion skeleton for the command foo, stored in _foo

+
#compdef _foo foo
+
+function _foo() {
+	...
+}
+
+

Alternatively one can install a completion function explicitly by calling compdef <FUNC> <CMD>.

+

Completion Variables

+

Following variables are available in Completion functions:

+
$words              # array with command line in words
+$#words             # number words
+$CURRENT            # index into $words for cursor position
+$words[CURRENT-1]   # previous word (relative to cursor position)
+
+

Completion Functions

+ +

Completion with _describe

+
_describe MSG COMP
+
+ +
function _foo() {
+    local -a opts
+    opts=('bla:desc for bla' 'blu:desc for blu')
+    _describe 'foo-msg' opts
+}
+compdef _foo foo
+
+foo <TAB><TAB>
+ -- foo-msg --
+bla  -- desc for bla
+blu  -- desc for blu
+
+

Completion with _arguments

+
_arguments SPEC [SPEC...]
+
+

where SPEC can have one of the following forms:

+ +

Available actions

+
(op1 op2)   list possible matches
+->VAL       set $state=VAL and continue, `$state` can be checked later in switch case
+FUNC        call func to generate matches
+{STR}       evaluate `STR` to generate matches
+
+

Example

+

Skeleton to copy/paste for writing simple completions.

+

Assume a program foo with the following interface:

+
foo -c green|red|blue -s low|high -f <file> -d <dir> -h
+
+

The completion handler could be implemented as follows in a file called _foo:

+
#compdef _foo foo
+
+function _foo_color() {
+    local colors=()
+    colors+=('green:green color')
+    colors+=('red:red color')
+    colors+=('blue:blue color')
+    _describe "color" colors
+}
+
+function _foo() {
+    _arguments                              \
+        "-c[define color]:color:->s_color"  \
+        "-s[select sound]:color:(low high)" \
+        "-f[select file]:file:_files"       \
+        "-d[select dir]:fir:_files -/"      \
+        "-h[help]"
+
+    case $state in
+        s_color) _foo_color;;
+    esac
+}
+

tmux(1)

Terminology: