1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
# zsh(1)
## Keybindings
Change input mode:
```zsh
bindkey -v change to vi keymap
bindkey -e change to emacs keymap
```
Define key-mappings:
```zsh
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
```
Access edit buffer in zle widget:
```zsh
$BUFFER # Entire edit buffer content
$LBUFFER # Edit buffer content left to cursor
$RBUFFER # Edit buffer content right to cursor
# create zle widget which adds text right of the cursor
function add-text() {
RBUFFER="some text $RBUFFER"
}
zle -N add-text
bindkey "^p" add-text
```
## Parameter
Default value:
```zsh
# default value
echo ${foo:-defval} # defval
foo=bar
echo ${foo:-defval} # bar
```
Alternative value:
```zsh
echo ${foo:+altval} # ''
foo=bar
echo ${foo:+altval} # altval
```
Check variable set, error if not set:
```zsh
echo ${foo:?msg} # print `msg` and return errno `1`
foo=bar
echo ${foo:?msg} # bar
```
Sub-string `${var:offset:length}`:
```zsh
foo=abcdef
echo ${foo:1:3} # bcd
```
Trim prefix `${var#prefix}`:
```zsh
foo=bar.baz
echo ${foo#bar} # .baz
```
Trim suffix `${var%suffix}`:
```zsh
foo=bar.baz
echo ${foo%.baz} # bar
```
Substitute pattern `${var/pattern/replace}`:
```zsh
foo=aabbccbbdd
echo ${foo/bb/XX} # aaXXccbbdd
echo ${foo//bb/XX} # aaXXccXXdd
# replace prefix
echo ${foo/#bb/XX} # aabbccbbdd
echo ${foo/#aa/XX} # XXbbccbbdd
# replace suffix
echo ${foo/%bb/XX} # aabbccbbdd
echo ${foo/%dd/XX} # aabbccbbXX
```
> Note: `prefix`/`suffix`/`pattern` are expanded as pathnames.
## Variables
```zsh
# Variable with local scope
local var=val
# Read-only variable
readonly var=bal
```
Indexed arrays:
```zsh
arr=(aa bb cc dd)
echo $arr[1] # aa
echo $arr[-1] # dd
arr+=(ee)
echo $arr[-1] # ee
echo $arr[1,3] # aa bb cc
```
Associative arrays:
```zsh
typeset -A arr
arr[x]='aa'
arr[y]='bb'
echo $arr[x] # aa
```
Tied arrays:
```zsh
typeset -T VEC vec=(1 2 3) '|'
echo $vec # 1 2 3
echo $VEC # 1|2|3
```
Unique arrays (set):
```
typeset -U vec=(1 2 3)
echo $vec # 1 2 3
vec+=(1 2 4)
echo $vec # 1 2 3 4
```
### Expansion Flags
Join array to string `j:sep:`:
```zsh
foo=(1 2 3 4)
echo ${(j:-:)foo} # 1-2-3-4
echo ${(j:\n:)foo} # join with new lines
```
Split string to array `s:sep`:
```zsh
foo='1-2-3-4'
bar=(${(s:-:)foo}) # capture as array
echo $bar # 1 2 3 4
echo $bar[2] # 2
```
Upper/Lower case string:
```zsh
foo=aaBB
echo ${(L)foo} # aabb
echo ${(U)foo} # AABB
```
Key/values in associative arrays:
```zsh
typeset -A vec; vec[a]='aa'; vec[b]='bb'
echo ${(k)vec} # a b
echo ${(v)vec} # aa bb
echo ${(kv)vec} # a aa b bb
# Iterate over key value pairs.
for k v in ${(kv)vec)}; do ...; done
```
## I/O redirections
See [bash - I/O redirection](bash.md#io-redirection)
## Process substitution
Process substitution allows to redirect the stdout of multiple processes at
once.
```bash
vim -d <(grep foo bar) <(grep foo moose)
```
## Argument parsing with `zparseopts`
```zsh
zparseopts [-D] [-E] [-A assoc] specs
```
Arguments are copied into the associative array `assoc` according to `specs`.
Each spec is described by an entry as `opt[:][=array]`.
- `opt` is the option without the `-` char. Passing `-f` is matched against `f`
opt, `--long` is matched against `-long`.
- Using `:` means the option will take an argument.
- The optional `=array` specifies an alternate storage container where this
option should be stored.
> Documentation can be found in `man zshmodules`.
### Example
```zsh
#!/bin/zsh
function test() {
zparseopts -D -E -A opts f=flag o: -long:
echo "flag $flag"
echo "o $opts[-o]"
echo "long $opts[--long]"
echo "pos $1"
}
test -f -o OPTION --long LONG_OPT POSITIONAL
# Outputs:
# flag -f
# o OPTION
# long LONG_OPT
# pos POSITIONAL
```
## Regular Expressions
Zsh supports regular expression matching with the binary operator `=~`.
The match results can be accessed via the `$MATCH` variable and
`$match` indexed array:
- `$MATCH` contains the full match
- `$match[1]` contains match of the first capture group
```zsh
INPUT='title foo : 1234'
REGEX='^title (.+) : ([0-9]+)$'
if [[ $INPUT =~ $REGEX ]]; then
echo "$MATCH" # title foo : 1234
echo "$match[1]" # foo
echo "$match[2]" # 1234
fi
```
## Trap Handling
```zsh
trap "<CMDs>" <SIG>/EXIT
# Show current trap handler.
trap -p
# List signal names.
trap -l
```
#### Example: Run handler only on error exit
```zsh
trap 'test $? -ne 0 && echo "run exit trap"' EXIT
# -> no print
exit 0
# -> print
exit 1
```
#### Example: Mutex in shell script
For example if a script is triggered in unsynchronized, we may want to ensure
that a single script instance runs.
```zsh
# open file=LOCK with fd=100
exec 100>LOCK
# take exclusive lock, wait maximal for 3600s
flock -w 3600 -x 100 || { echo "flock timeout"; exit 1; }
# eg automatically release lock when script exits
trap "flock -u 100" EXIT
```
## 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`
```zsh
#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:
```zsh
$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
- `_describe` simple completion, just words + description
- `_arguments` sophisticated completion, allow to specify actions
#### Completion with [`_describe`][zsh-comp-fn]
```zsh
_describe MSG COMP
```
- `MSG` simple string with header message
- `COMP` array of completions where each entry is `"opt:description"`
```zsh
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`][zsh-comp-fn]
```zsh
_arguments SPEC [SPEC...]
```
where `SPEC` can have one of the following forms:
- `OPT[DESC]:MSG:ACTION` for option flags
- `N:MSG:ACTION` for positional arguments
Available actions
```zsh
(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:
```zsh
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`:
```zsh
#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]:sound:(low high)" \
"-f[select file]:file:_files" \
"-d[select dir]:dir:_files -/" \
"-h[help]"
case $state in
s_color) _foo_color;;
esac
}
```
### Example with optional arguments
For this example we assume that the command `foo` takes at least three optional
arguments such as
```zsh
foo arg1 arg2 arg3 [argN..]
```
```zsh
function _foo() {
_arguments \
"1:opt 1:(a b c)" \
":opt next:(d e f)" \
"*:opt all:(u v w)"
}
```
Explanation:
- `1:MSG:ACTION` sets completion for the **first** optional argument
- `:MSG:ACTION` sets completion for the **next** optional argument
- `*:MSG:ACTION` sets completion for the optional argument where none of the
previous rules apply, so in our example for `arg3, argN..`.
> `_files` is a zsh builtin utility function to complete files/dirs see
> - [zsh completion functions][zsh-comp-fn]
> - [zsh completion utility functions][zsh-comp-utility-fn]
[zsh-comp-fn]: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions
[zsh-comp-utility-fn]: https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#utility-functions
|