Compare commits

...

30 Commits

Author SHA1 Message Date
F.B. 6324b074ab
Merge 5359bbf0e7 into b2d0232473 2025-10-28 14:36:39 +00:00
Shohei YOSHIDA b2d0232473
Merge pull request #1165 from zsh-users/add-util-linux
Add more util-linux command completions
2025-10-27 12:26:26 +09:00
Shohei YOSHIDA b66a1aba63
add lslogins completion 2025-10-26 16:44:52 +09:00
Shohei YOSHIDA 667494f334
add lsmem completion 2025-10-26 16:44:48 +09:00
Shohei YOSHIDA 215780cd9c
add lslocks completion 2025-10-26 16:43:49 +09:00
Shohei YOSHIDA 20423a74f8
add lsipc completion 2025-10-26 16:43:42 +09:00
Shohei YOSHIDA 55e9d8e6eb
Merge pull request #1164 from zsh-users/update-rails
update rails completion
2025-10-24 20:02:34 +09:00
Shohei YOSHIDA e51f50f5a5
update rails completion 2025-10-22 14:48:54 +09:00
Shohei YOSHIDA 1b97a1492b
Merge pull request #1163 from zsh-users/add-swap-utils
Add swap utils of util-linux
2025-10-22 00:25:16 +09:00
Shohei YOSHIDA 4a7179ffff
add swaplabel completion 2025-10-20 23:51:42 +09:00
Shohei YOSHIDA 7aeefd0020
add swapoff completion 2025-10-20 23:49:39 +09:00
Shohei YOSHIDA 9e2a5239dc
add swapon completion 2025-10-20 23:47:36 +09:00
Shohei YOSHIDA c47e10641a
Merge pull request #1162 from zsh-users/add-distro-info
Add distro-info completion
2025-10-19 10:17:19 +09:00
Shohei YOSHIDA c49bb88997
Add distro-info completion 2025-10-18 10:13:54 +09:00
Shohei YOSHIDA 5fd23d5376
Merge pull request #1161 from zsh-users/update-node-25.0.0
Update node.js completion to 25.0.0
2025-10-17 00:53:48 +09:00
Shohei YOSHIDA e74df1347f
Update node.js completion to 25.0.0 2025-10-16 11:36:50 +09:00
fishBone000 5359bbf0e7
Forgot to change ToC 2024-09-18 06:07:34 +00:00
fishBone000 a3e89f7c56
Reread to make the doc more fluent 2024-09-18 06:06:48 +00:00
fishBone000 cf57a3266c
Try to fix ToC 2024-09-18 04:52:48 +00:00
fishBone000 41896a6013
Finish most TODOs 2024-09-18 04:43:01 +00:00
fishBone000 0d8667bb6a
Fix Table of Contents
I forgot to change it
2024-09-18 04:17:10 +00:00
fishBone000 79c6d1715f
Finish Other resources 2024-09-18 04:15:31 +00:00
fishBone000 f5e58a5ca7
Finish Tips 2024-09-18 04:12:15 +00:00
fishBone000 6c8cd1b111
Finish Gotchas (things to look out for) 2024-09-18 04:01:09 +00:00
fishBone000 eb711299c5
Finish Testing & debugging 2024-09-18 03:53:13 +00:00
fishBone000 d71c3c84cc
Finish Writing your own completion code 2024-09-18 03:43:52 +00:00
fishBone000 c3f1f7748d
Finish Getting started 2024-09-17 07:40:59 +00:00
fishBone000 45fd6f398a
Fix document file extension 2024-09-17 07:15:24 +00:00
fishBone000 26b538f012
Finish Intro 2024-09-17 07:13:14 +00:00
fishBone000 fd43209f3a
Beginning of zh_cn translation 2024-09-17 06:37:21 +00:00
11 changed files with 1091 additions and 12 deletions

59
src/_distro_info Normal file
View File

@ -0,0 +1,59 @@
#compdef distro-info ubuntu-distro-info debian-distro-info
#
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion for distro-info
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei YOSHIDA (https://github.com/syohex)
#
# ------------------------------------------------------------------------------
_arguments \
'(- *)'{-h,--help}'[display help message and exit]' \
'--date=[data for calculating the version]:date' \
'(-a --all)'{-a,--all}'[list all known versions]' \
'(-y --days)'{-y,--days=}'[display number of days until specified version reaches the specified milestone]:milestone:(created release eol eol-server)' \
'(-d --devel)'{-d,--devel}'[latest development version]' \
'--series=[series to calculate the version for]:series' \
'(-s --stable)'{-s,--stable}'[latest stable version]' \
'--supported[list of all supported version, including development releases]' \
'--unsupported[list of all unsupported stable versions]' \
'(-c --codename)'{-c,--codename}'[print the codename(default)]' \
'(-r --release)'{-r,--release}'[print the release version]' \
'(-f --fullname)'{-f,--fullname}'[print the full name]'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

125
src/_lsipc Normal file
View File

@ -0,0 +1,125 @@
#compdef lsipc
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for lsipc v2.41 (https://github.com/util-linux/util-linux)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------
(( $+functions[_lsipc_output_columns] )) ||
_lsipc_output_columns() {
local -a columns=(
"ID[Resource ID]"
"OWNER[Owner's username or UID]"
"PERMS[Permissions]"
"CUID[Creator UID]"
"CUSER[Creator user]"
"CGID[Creator GID]"
"CGROUP[Creator group]"
"UID[User ID]"
"USER[User name]"
"GID[Group ID]"
"GROUP[Group name]"
"CTIME[Time of the last change]"
# Generic POSIX columns:
"NAME[POSIX resource name]"
"MTIME[Time of last action]"
# System V Shared-memory columns (--shmems):
"SIZE[Segment size]"
"NATTCH[Number of attached processes]"
"STATUS[Status]"
"ATTACH[Attach time]"
"DETACH[Detach time]"
"COMMAND[Creator command line]"
"CPID[PID of the creator]"
"LPID[PID of last user]"
# System V Message-queue columns (--queues):
"USEDBYTES[Bytes used]"
"MSGS[Number of messages]"
"SEND[Time of last msg sent]"
"RECV[Time of last msg received]"
"LSPID[PID of the last msg sender]"
"LRPID[PID of the last msg receiver]"
# System V Semaphore columns (--semaphores):
"NSEMS[Number of semaphores]"
"OTIME[Time of the last operation]"
# POSIX Semaphore columns (--posix-semaphores):
"SVAL[Semaphore value]"
# Summary columns (--global):
"RESOURCE[Resource name]"
"DESCRIPTION[Resource description]"
"LIMIT[System-wide limit]"
"USED[Currently used]"
"USE%[Currently use percentage]"
)
_values -s ',' column $columns
}
_arguments \
'(- *)'{-h,--help}'[display this help]' \
'(- *)'{-V,--version}'[display version]' \
'(-m --shmems)'{-m,--shmems}'[shared memory segments]' \
'(-M --posix-shmems)'{-M,--posix-shmems}'[POSIX shared memory segments]' \
'(-q --queues)'{-q,--queues}'[POSIX shared memory segments]' \
'(-Q --posix-mqueues)'{-Q,--posix-mqueues}'[POSIX message queues]' \
'(-s --semaphores)'{-s,--semaphores}'[semaphores]' \
'(-S --posix-semaphores)'{-S,--posix-semaphores}'[POSIX semaphores]' \
'(-g --global)'{-g,--global}'[info about system-wide usage]' \
\*{-i,--id}'[System V resource identified by id]:id' \
\*{-N,--name}'[POSIX resource identified by name]:name' \
'--noheadings[do not print headings]' \
'--notruncate[do not truncate output]' \
'(-b --bytes)'{-b,--bytes}'[print SIZE in bytes rather]' \
'(-c --creator)'{-c,--creator}'[show creator and owner]' \
'(-e --export)'{-e,--export}'[display in the export-able output format]' \
'(-J --json)'{-J,--json}'[use the JSON output format]' \
'(-n --newline)'{-n,--newline}'[display each piece of information on a new line]' \
'(-l --list)'{-l,--list}'[force list output format]' \
'(-o --output)'{-o,--output=-}'[define the columns to output]:columns:_lsipc_output_columns' \
'(-P --numeric-perms)'{-p,--numeric-perms}'[print numeric permissions]' \
'(-r --raw)'{-r,--raw}'[display in raw mode]' \
'(-t --time)'{-t,--time}'[show attach, detach and change times]' \
'(-y --shell)'{-y,--shell}'[use column names to be usable as shell variables]'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

77
src/_lslocks Normal file
View File

@ -0,0 +1,77 @@
#compdef lslocks
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for lslocks v2.41 (https://github.com/util-linux/util-linux)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------
(( $+functions[_lslocks_output_columns] )) ||
_lslocks_output_columns() {
local -a columns=(
"COMMAND[command of the process holding the lock]"
"PID[PID of the process holding the lock]"
"TYPE[kind of lock]"
"SIZE[size of the locks]"
"INODE[inode number]"
"MAJ\\:MIN[major:minor device number]"
"MODE[lock access mode]"
"M[mandatory state of the lock: 0 (none), 1 (set)]"
"START[relative byte offset of the lock]"
"END[ending offset of the lock]"
"PATH[path of the locked file]"
"BLOCKER[PID of the process blocking the lock]"
"HOLDERS[holders of the lock]"
)
_values -s ',' column $columns
}
_arguments \
'(- *)'{-h,--help}'[display this help]' \
'(- *)'{-V,--version}'[display version]' \
'(-b --bytes)'{-b,--bytes}'[print SIZE in bytes rather]' \
'(-J --json)'{-J,--json}'[use the JSON output format]' \
'(-i --noinaccessible)'{-i,--noinaccessible}'[ignore locks without read permissions]' \
'(-o --output)'{-o,--output=-}'[output columns]:columns:_lslocks_output_columns' \
'(-o --output --output-all)--output-all[output all columns]' \
'(-p --pid)'{-p,--pid}'[display only locks held by this process]:pid:_pids' \
'(-r --raw)'{-r,--raw}'[use the raw output format]' \
'(-u --notruncate)'{-u,--notruncate}'[do not truncate text in columns]' \
'(- *)'{-H,--list-columns}'[list the available columns]'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

107
src/_lslogins Normal file
View File

@ -0,0 +1,107 @@
#compdef lslogins
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for lslogins v2.41 (https://github.com/util-linux/util-linux)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------
(( $+functions[_lslogins_output_columns] )) ||
_lslogins_output_columns() {
local -a columns=(
"USER[user name]"
"UID[user ID]"
"GECOS[full user name]"
"HOMEDIR[home directory]"
"SHELL[login shell]"
"NOLOGIN[log in disabled by nologin(8) or pam_nologin(8)]"
"PWD-LOCK[password defined, but locked]"
"PWD-EMPTY[password not defined]"
"PWD-DENY[login by password disabled]"
"PWD-METHOD[password encryption method]"
"GROUP[primary group name]"
"GID[primary group ID]"
"SUPP-GROUPS[supplementary group names]"
"SUPP-GIDS[supplementary group IDs]"
"LAST-LOGIN[date of last login]"
"LAST-TTY[last tty used]"
"LAST-HOSTNAME[hostname during the last session]"
"FAILED-LOGIN[date of last failed login]"
"FAILED-TTY[where did the login fail?]"
"HUSHED[user's hush settings]"
"PWD-WARN[days user is warned of password expiration]"
"PWD-CHANGE[date of last password change]"
"PWD-MIN[number of days required between changes]"
"PWD-MAX[max number of days a password may remain unchanged]"
"PWD-EXPIR[password expiration date]"
"CONTEXT[the user's security context]"
"PROC[number of processes run by the user]"
)
_values -s ',' column $columns
}
_arguments \
'(- *)'{-h,--help}'[display this help]' \
'(- *)'{-V,--version}'[display version]' \
'(-a --acc-expiration)'{-a,--acc-expiration}'[display info about passwords expiration]' \
'(-c --colon-separate)'{-c,--colon-separate}'[display data in a format similar to /etc/passwd]' \
'(-e --export)'{-e,--export}'[display in an export-able output format]' \
'(-f --failed)'{-f,--failed}"[display data about the user's last failed logins]" \
'(-G --supp-groups)'{-G,--supp-groups}'[display information about groups]' \
'(-g --groups)'{-g,--groups=}'[display users belonging to the given group]:group:_groups' \
'(-L --last)'{-L,--last}"[show info about the user's last login sessions]" \
'(-l --logins)'{-l,--logins}'[display only users from the given logins]:logins' \
'(-n --newline)'{-n,--newline}'[display each piece of information on a new line]'\
'--noheadings[do not print headings]' \
'--truncate[do not truncate output]' \
'(-o --output)'{-o,--output=-}'[output columns]:columns:_lslogins_output_columns' \
'(-o --output --output-all)--output-all[output all columns]' \
'(-p --pwd)'{-p,--pwd}'[display information related to login by password]' \
'(-r --raw)'{-r,--raw}'[display in raw mode]' \
'(-s --system-accs)'{-s,--system-accs}'[display system accounts]' \
'--time-format=[display dates format]:type:(short full iso)' \
'(-u --user-accs)'{-u,--user-accs}'[display user accounts]' \
'(-y --shell)'{-y,--shell}'[use column names to be usable as shell variable identifiers]' \
'(-Z --context)'{-Z,--context}'[display SELinux contexts]' \
'(-z --print0)'{-z,--print0}'[delimit user entries with a nul character]' \
'--wtmp-file[set an alternate path for wtmp]:path:_files' \
'--btmp-file[set an alternate path for btmp]:path:_files' \
'--lastlog[set an alternate path for lastlog]:path:_files' \
'--lastlog2[set an alternate path for lastlog2]:path:_files'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

73
src/_lsmem Normal file
View File

@ -0,0 +1,73 @@
#compdef lsmem
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for lsmem v2.41 (https://github.com/util-linux/util-linux)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------
(( $+functions[_lsmem_output_columns] )) ||
_lsmem_output_columns() {
local -a columns=(
"RANGE[start and end address of the memory range]"
"SIZE[size of the memory range]"
"STATE[online status of the memory range]"
"REMOVABLE[memory is removable]"
"BLOCK[memory block number or blocks range]"
"NODE[numa node of memory]"
"ZONES[valid zones for the memory range]"
)
_values -s ',' column $columns
}
_arguments \
'(- *)'{-h,--help}'[display this help]' \
'(- *)'{-V,--version}'[display version]' \
'(-J --json)'{-J,--json}'[use the JSON output format]' \
'(-P --pairs)'{-P,--pairs}'[use key=value output format]' \
'(-a --all)'{-a,--all}'[list each individual memory block]' \
'(-b --bytes)'{-b,--bytes}'[print SIZE in bytes rather]' \
'(-n --noheadings)'{-n,--noheadings}'[do not print headings]' \
'(-o --output)'{-o,--output=-}'[output columns]:columns:_lsmem_output_columns' \
'(-o --output --output-all)--output-all[output all columns]' \
'(-r --raw)'{-r,--raw}'[use the raw output format]' \
'(-S --split)'{-S,--split}'[split ranges by specified columns]:columns:_lsmem_output_columns' \
'(-s --sysroot)'{-s,--sysroot}'[use the specified directory as system root]:dir:_files -/' \
'--summary=-[print summary information]:when:(never always only)'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

View File

@ -28,7 +28,7 @@
# Description # Description
# ----------- # -----------
# #
# Completion script for Node.js v24.4.0 (https://nodejs.org) # Completion script for Node.js v25.0.0 (https://nodejs.org)
# #
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Authors # Authors
@ -69,10 +69,7 @@ _node_scripts() {
_describe 'scripts' scripts _describe 'scripts' scripts
} }
local curcontext="$curcontext" state line ret=1 _arguments \
typeset -A opt_args
_arguments -C \
'-[script read from stdin (default; interactive mode if a tty)]' \ '-[script read from stdin (default; interactive mode if a tty)]' \
'--[indicate the end of node options]' \ '--[indicate the end of node options]' \
'--abort-on-uncaught-exception[aborting instead of exiting causes a core file to be generated for analysis]' \ '--abort-on-uncaught-exception[aborting instead of exiting causes a core file to be generated for analysis]' \
@ -80,6 +77,8 @@ _arguments -C \
'--allow-child-process[allow use of child process when any permissions are set]' \ '--allow-child-process[allow use of child process when any permissions are set]' \
'--allow-fs-read=[allow permissions to read the filesystem]: :_files' \ '--allow-fs-read=[allow permissions to read the filesystem]: :_files' \
'--allow-fs-write=[allow permissions to write in the filesystem]:_files' \ '--allow-fs-write=[allow permissions to write in the filesystem]:_files' \
'--allow-inspector[allow use of inspector when any permissions are set]' \
'--allow-net[allow use of network when any permissions are set]' \
'--allow-wasi[allow wasi when any permissions are set]' \ '--allow-wasi[allow wasi when any permissions are set]' \
'--allow-worker[allow worker threads when any permissions are set]' \ '--allow-worker[allow worker threads when any permissions are set]' \
'--build-snapshot[generate a snapshot blob when the process exits]' \ '--build-snapshot[generate a snapshot blob when the process exits]' \
@ -110,16 +109,17 @@ _arguments -C \
'--experimental-default-config-file[set config file from default config file]' \ '--experimental-default-config-file[set config file from default config file]' \
'--experimental-eventsource[enable experimental EventSource API]' \ '--experimental-eventsource[enable experimental EventSource API]' \
'--experimental-import-meta-resolve[experimental ES Module import.meta.resolve() support]' \ '--experimental-import-meta-resolve[experimental ES Module import.meta.resolve() support]' \
'--experimental-inspector-network-resource[experimental load network resources via the inspector]' \
'(--loader --experimental-loader)'{--loader,--experimental-loader}'=[Specify the module of a custom ECMAScript Module loader]: :_files' \ '(--loader --experimental-loader)'{--loader,--experimental-loader}'=[Specify the module of a custom ECMAScript Module loader]: :_files' \
'--experimental-network-inspection[enable experimental network inspection support]' \ '--experimental-network-inspection[enable experimental network inspection support]' \
'--experimental-print-required-tla[print pending top-level await]' \ '--experimental-print-required-tla[print pending top-level await]' \
'--experimental-quic[experimental QUIC support]' \
'--experimental-sea-config=[generate a blob that can be embedded into the single executable application]: :_files' \ '--experimental-sea-config=[generate a blob that can be embedded into the single executable application]: :_files' \
'--experimental-test-coverage[enable code coverage in the test runner]' \ '--experimental-test-coverage[enable code coverage in the test runner]' \
'--experimental-test-module-mocks[enable module mocking in the test runner]' \ '--experimental-test-module-mocks[enable module mocking in the test runner]' \
'--experimental-transform-types[enable transformation of TypeScript-onlysyntax into JavaScript code]' \ '--experimental-transform-types[enable transformation of TypeScript-onlysyntax into JavaScript code]' \
'--experimental-vm-modules[experimental ES Module support in vm module]' \ '--experimental-vm-modules[experimental ES Module support in vm module]' \
'--experimental-wasm-modules[experimental ES module support for webassembly modules]' \ '--experimental-worker-inspection[experimental worker inspection support]' \
'--experimental-webstorage[experimental Web Storage API]' \
'--expose-gc[expose gc extension]' \ '--expose-gc[expose gc extension]' \
'--force-context-aware[disable loading non-context-aware addons]' \ '--force-context-aware[disable loading non-context-aware addons]' \
'--force-fips[force FIPS crypto]' \ '--force-fips[force FIPS crypto]' \
@ -145,8 +145,10 @@ _arguments -C \
'--jitless[Disable runtime allocation of executable memory]' \ '--jitless[Disable runtime allocation of executable memory]' \
'--localstorage-file=[file used to persist localStorage data]:file:_files' \ '--localstorage-file=[file used to persist localStorage data]:file:_files' \
'--max-http-header-size=[set the maximum size of HTTP headers]: :number' \ '--max-http-header-size=[set the maximum size of HTTP headers]: :number' \
"--max-old-space-size-percentage=[set V8's max old space size as a percentage of available memory]:memory" \
'--network-family-autoselection-attempt-timeout=[sets the default value for the network family autoselection attempt timeout]:timeout' \ '--network-family-autoselection-attempt-timeout=[sets the default value for the network family autoselection attempt timeout]:timeout' \
'--no-addons[disable loading native addons]' \ '--no-addons[disable loading native addons]' \
'--no-async-context-frame[improve AsyncLocalStorage performance with AsyncContextFrame]' \
'--no-deprecation[silence deprecation warnings]' \ '--no-deprecation[silence deprecation warnings]' \
'--no-experimental-detect-module[when ambiguous modules fail to evaluate, try again to evaluate them as ES modules]' \ '--no-experimental-detect-module[when ambiguous modules fail to evaluate, try again to evaluate them as ES modules]' \
'--no-experimental-global-navigator[expose experimental Navigator API on the global scope]' \ '--no-experimental-global-navigator[expose experimental Navigator API on the global scope]' \
@ -158,7 +160,9 @@ _arguments -C \
'--no-extra-info-on-fatal-exception[hide extra information on fatal exception that causes exit]' \ '--no-extra-info-on-fatal-exception[hide extra information on fatal exception that causes exit]' \
'--no-force-async-hooks-checks[disable checks for async_hooks]' \ '--no-force-async-hooks-checks[disable checks for async_hooks]' \
'--no-global-search-paths[disable global module search paths]' \ '--no-global-search-paths[disable global module search paths]' \
'--no-network-family-autoselection[disable network address family autodetection algorithm]' \
'--no-warnings[silence all process warnings]' \ '--no-warnings[silence all process warnings]' \
'--no-webstorage[disable Web Storage API]' \
'--node-memory-debug[run with extra debug checks for memory leaks in Node.js itself]' \ '--node-memory-debug[run with extra debug checks for memory leaks in Node.js itself]' \
'--openssl-config=[load OpenSSL configuration from the specified file (overrides OPENSSL_CONF)]:file:_files' \ '--openssl-config=[load OpenSSL configuration from the specified file (overrides OPENSSL_CONF)]:file:_files' \
'--openssl-legacy-provider[enable OpenSSL 3.0 legacy provider]' \ '--openssl-legacy-provider[enable OpenSSL 3.0 legacy provider]' \
@ -191,11 +195,13 @@ _arguments -C \
'--test-coverage-include=[include files from coverage report that match this glob pattern]:pattern' \ '--test-coverage-include=[include files from coverage report that match this glob pattern]:pattern' \
'--test-coverage-lines=[the line coverage minimum threshold]:threshold' \ '--test-coverage-lines=[the line coverage minimum threshold]:threshold' \
'--test-force-exit[force test runner to exit upon completion]' \ '--test-force-exit[force test runner to exit upon completion]' \
'--test-global-setup=[specify the path to the global setup file]:file:_files' \
'--test-isolation=[configurations the type of test isolation used in the test runner]:isolation' \ '--test-isolation=[configurations the type of test isolation used in the test runner]:isolation' \
'--test-name-pattern=[run tests whose name matches this regular expression]:pattern' \ '--test-name-pattern=[run tests whose name matches this regular expression]:pattern' \
'--test-only[run tests with "only" option set]' \ '--test-only[run tests with "only" option set]' \
'*--test-reporter=[report test output using the given reporter]:reporter:(tap spec dot)' \ '*--test-reporter=[report test output using the given reporter]:reporter:(tap spec dot)' \
'*--test-reporter-destination=[report given reporter to the given destination]:destination:_files' \ '*--test-reporter-destination=[report given reporter to the given destination]:destination:_files' \
'--test-rerun-failures=[specify the path to the rerun state file]:state file:_files' \
'--test-shard=[run test at specific shard]:shard' \ '--test-shard=[run test at specific shard]:shard' \
'--test-skip-pattern=[run tests whose name do not match this regular expression]' \ '--test-skip-pattern=[run tests whose name do not match this regular expression]' \
'--test-timeout=[specify test runner timeout]' \ '--test-timeout=[specify test runner timeout]' \
@ -238,9 +244,7 @@ _arguments -C \
'--zero-fill-buffers[automatically zero-fill all newly allocated Buffer and SlowBuffer instances]' \ '--zero-fill-buffers[automatically zero-fill all newly allocated Buffer and SlowBuffer instances]' \
'(- 1 *)'{-h,--help}'[print node command line options]' \ '(- 1 *)'{-h,--help}'[print node command line options]' \
'(- 1 *)'{-v,--version}'[print Node.js version]' \ '(- 1 *)'{-v,--version}'[print Node.js version]' \
'*: :_node_args' && ret=0 '*: :_node_args'
return ret
# Local Variables: # Local Variables:
# mode: Shell-Script # mode: Shell-Script

View File

@ -28,7 +28,7 @@
# Description # Description
# ----------- # -----------
# #
# Completion script for Ruby on Rails 7.2.0 (https://rubyonrails.org/). # Completion script for Ruby on Rails 8.1.0 (https://rubyonrails.org/).
# #
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Authors # Authors
@ -296,7 +296,7 @@ _rails_new() {
'(-r --ruby)'{-r,--ruby=}'[Path to the Ruby binary of your choice]:path:_files' \ '(-r --ruby)'{-r,--ruby=}'[Path to the Ruby binary of your choice]:path:_files' \
'(-b --builder)'{-b,--builder=}'[Path to a application builder(can be a filesystem path or URL)]: :_rails_path_or_url' \ '(-b --builder)'{-b,--builder=}'[Path to a application builder(can be a filesystem path or URL)]: :_rails_path_or_url' \
'(-m --template)'{-m,--template=}'[Path to an application template(can be a filesystem path or URL)]: :_rails_path_or_url' \ '(-m --template)'{-m,--template=}'[Path to an application template(can be a filesystem path or URL)]: :_rails_path_or_url' \
'(-d --database)'{-d,--database=}'[Preconfigure for selected database]:database:(mysql trilogy oracle postgresql sqlite3 frontbase ibm_db sqlserver jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc)' \ '(-d --database)'{-d,--database=}'[Preconfigure for selected database]:database:(mysql trilogy oracle postgresql sqlite3 frontbase ibm_db sqlserver jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc mariadb-mysql mariadb-trilogy)' \
--skip-gemfile"[Don't create a Gemfile]" \ --skip-gemfile"[Don't create a Gemfile]" \
--skip-bundle"[Don't run bundle install]" \ --skip-bundle"[Don't run bundle install]" \
'(-G --skip-git)'{-G,--skip-git}'[Skip git init]' \ '(-G --skip-git)'{-G,--skip-git}'[Skip git init]' \
@ -320,6 +320,7 @@ _rails_new() {
--skip-dev-gems'[Skip development gems(e.g. web-console)]' \ --skip-dev-gems'[Skip development gems(e.g. web-console)]' \
--skip-rubocop'[Skip RuboCop setup]' \ --skip-rubocop'[Skip RuboCop setup]' \
--skip-brakeman'[Skip brakeman setup]' \ --skip-brakeman'[Skip brakeman setup]' \
--skip-bundler-audit'[Skip bundler-audit setup]' \
--skip-ci'[Skip GitHub CI files]' \ --skip-ci'[Skip GitHub CI files]' \
--dev'[Setup the application with Gemfile pointing to your Rails checkout]' \ --dev'[Setup the application with Gemfile pointing to your Rails checkout]' \
--devcontainer'[Generate devcontainer files]' \ --devcontainer'[Generate devcontainer files]' \

50
src/_swaplabel Normal file
View File

@ -0,0 +1,50 @@
#compdef swaplabel
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for swaplabel v2.41 (https://github.com/util-linux/util-linux)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------
_arguments \
'(- *)'{-h,--help}'[display help message]' \
'(- *)'{-V,--version}'[display version]' \
'(-L --label)'{-L,--label}'[specify a new label]:label' \
'(-U --uuid)'{-U,--uuid}'[specify a new uuid]:uuid' \
'*::file:_files'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

51
src/_swapoff Normal file
View File

@ -0,0 +1,51 @@
#compdef swapoff
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for swapoff v2.41 (https://github.com/util-linux/util-linux)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------
_arguments \
'(-a --all)'{-a,--all}'[enable all swaps from /etc/fstab]' \
'(- *)'{-h,--help}'[display help message]' \
'(- *)'{-V,--version}'[display version]' \
'-L[synonym for LABEL=<label>]:label' \
'-U[synonym for UUID=<uuid>]:uuid' \
'*::file:_files'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

78
src/_swapon Normal file
View File

@ -0,0 +1,78 @@
#compdef swapon
# ------------------------------------------------------------------------------
# Copyright (c) 2025 Github zsh-users - https://github.com/zsh-users
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for swapon v2.41 (https://github.com/util-linux/util-linux)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Shohei Yoshida (https://github.com/syohex) <syohex@gmail.com>
#
# ------------------------------------------------------------------------------
(( $+functions[_swapon_column] )) ||
_swapon_column() {
local -a columns=(
"NAME[device file or partition path]"
"TYPE[type of the device]"
"SIZE[size of the swap area]"
"USED[bytes in use]"
"PRIO[swap priority]"
"UUID[swap uuid]"
"LABEL[swap label]"
)
_values -s ',' column $columns
}
_arguments \
'(-a --all)'{-a,--all}'[enable all swaps from /etc/fstab]' \
'(-d --discard)'{-d,--discard=-}'[enable swap discards, if supported by device]:policy:(once pages)' \
'(-e --ifexists)'{-e,--ifexists}'[silently skip devices that do not exist]' \
'(-f --fixpgsz)'{-f,--fixpgsz}'[reinitialize the swap space if necessary]' \
\*{-o,--options}'[comma-separated list of swap options]:options' \
'(-p --priority)'{-p,--priority}'[specify the priority of the swap device]:priority' \
'(-s --summary)'{-s,--summary}'[display summary about used swap devices]' \
'(-T --fstab)'{-T,--fstab}'[alternative file to /etc/fstab]:path:_files' \
'--show=-[display summary in definable table]:column:_swapon_column' \
'--noheadings[do not print table heading(with --show)]' \
'--raw[use the raw output format(with --show)]' \
'--bytes[display swap size in bytes in --show output]' \
'(-v --verbose)'{-v,--verbose}'[verbose mode]' \
'(- *)'{-h,--help}'[display help message]' \
'(- *)'{-V,--version}'[display version]' \
'-L[synonym for LABEL=<label>]:label' \
'-U[synonym for UUID=<uuid>]:uuid' \
'*::file:_files'
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et

View File

@ -0,0 +1,454 @@
* 目录
- [[#介绍][介绍]]
- [[#开始][开始]]
- [[#让zsh知道用哪个函数补全命令][让zsh知道用哪个函数补全命令]]
- [[#补全gnu格式命令][补全gnu格式命令]]
- [[#从其它命令复制补全][从其它命令复制补全]]
- [[#编写你自己的补全代码][编写你自己的补全代码]]
- [[#工具函数][工具函数]]
- [[#用_describe编写简单的补全函数][用_describe编写简单的补全函数]]
- [[#用_alternative编写补全函数][用_alternative编写补全函数]]
- [[#用_arguments编写补全函数][用_arguments编写补全函数]]
- [[#用_regex_arguments和_regex_words编写补全函数][用_regex_arguments和_regex_words编写补全函数]]
- [[#用_values_sep_parts和_multi_parts实现复杂补全][用_values、_sep_parts和_multi_parts实现复杂补全]]
- [[#用compadd直接添加补全词][用compadd直接添加补全词]]
- [[#测试与debug][测试与debug]]
- [[#踩坑了吧-需要小心的东西][踩坑了吧 (需要小心的东西)]]
- [[#小贴士][小贴士]]
- [[#其它资源][其它资源]]
> 译注:本文可能有不通顺的地方,或者采用的词汇不是很正确。
> 如果你有更好的主意欢迎提交PR。
* 介绍
Zsh官方的补全函数文档令人费解而且也没提供多少示例。
写这份文档的当下我已经在网上找到了其它几份教程,但是那些教程只涉及了补全系统的一小部分。
这份文档目的在于补全网上其它地方没涵盖的部分,同时附带示例,这样读者就可以学会如何写更高级的补全函数。
我不会展开每一细节,但给你提供的内容和示例足以从零开始。
如果你需要了解更多细节,你可以自行查询[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System][官方文档]]。
还请公开你所创作的任何脚本比如fork这个仓库然后[[id:64bcd501-b0f0-48c7-b8e2-07af708b95ec][pr]])。
此外如果你有任何更多补充内容或对此教程的改进,欢迎做贡献。
* 开始
** 让zsh知道用哪个函数补全命令
补全命令用的补全函数储存于名字以下划线“_”起始的文件这些文件应存于$fpath变量所列出的某目录中。
你可以将下面的代码写入你的~/.zshrc以在$fpath中新增目录
#+BEGIN_SRC sh
fpath=(~/newdir $fpath)
#+END_SRC
一个补全函数文件的第一行长这个样:
#+BEGIN_SRC sh
#compdef foobar
#+END_SRC
这行代码表示这个文件含有补全foobar命令的代码。
多数情况下第一行都采用这个格式,但你也可以用同一个文件补全多个不同的函数。
查阅[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Autoloaded-files][官方文档]]以了解更多细节。
你也可以直接使用compdef命令(比如在你的~/.zshrc文件里)来告诉zsh用哪个函数补全命令
#+BEGIN_SRC sh
> compdef _function foobar
#+END_SRC
或者对多个命令使用同一种补全:
#+BEGIN_SRC sh
> compdef _function foobar goocar hoodar
#+END_SRC
如果你想提供参数的话:
#+BEGIN_SRC sh
> compdef '_function arg1 arg2' foobar
#+END_SRC
查阅[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Functions-4][官方文档]]以了解更多细节。
** 补全gnu格式命令
很多[[https://www.gnu.org/][gnu]]命令以标准化的方式列出选项描述(使用--help选项时
对于这些命令你可以使用_gnu_generic函数自动创建补全比如这样
#+BEGIN_SRC sh
> compdef _gnu_generic foobar
#+END_SRC
或者对多个不同命令使用_gnu_generic
#+BEGIN_SRC sh
> compdef _gnu_generic foobar goocar hoodar
#+END_SRC
你可以把这行代码放进~/.zshrc文件里。
** 从其它命令复制补全
如果你想要让一个命令比如cmd1和另一个已有补全的命令比如cmd2)拥有相同的补全,你可以:
#+BEGIN_SRC sh
> compdef cmd1=cmd2
#+END_SRC
比如当你给一个命令创建了一个助记alias的时候会很有帮助。
* 编写你自己的补全代码
你可以通过阅读已有的补全函数来开始入门。
在我的Linux系统上这些补全函数在/usr/share/zsh/functions/Completion/Unix、
/usr/share/zsh/functions/Completion/Linux和一些其它子目录下。
你会注意到这些文件频繁使用_arguments函数。
该函数是一个工具函数,可用于编写简单的补全函数。
_arguments函数是一个compadd内置函数的包装函数。
compadd内置函数是一个核心函数用于向命令行加入补全词并控制其行为。
不过多数情况下你不需要使用compadd因为有很多更易于使用的工具函数如_arguments和_describe。
对于非常基础的补全_describe函数已经够用了。
** 工具函数
下面是一个工具函数列表,你或许会用到它们。
工具函数的完整列表及使用方法在[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions][]]可供查阅。
这些函数的使用示例在下一节给出。
> 译注从这里开始会出现一些术语如定义specification、描述description、动作action、词汇word等。
> 初次阅读可能会觉得比较困难,尤其是下面的工具函数表格,稍后教程开始一一讲解并给出示例的时候就好懂多了。
> 定义指的是对补全规则的定义,如`_describe 定义`。
> 描述当然指的是对命令行选项和参数的用户友好描述,补全时会显示在屏幕上,如:'-o 输出文件'
> 词汇指的是如同'word'、'-o'、'--help'、'start'等连续的一小段字符串
> 动作指的是匹配成功后执行的操作,比如执行某函数生成补全,或直接补全等等。
> 选项option和参数指给命令提供的选项和参数如`cp -r src dst`中,`-r`为选项,`src`和`dst`为参数。有些选项带参,如`tar -f file ...`中`-f`选项带`file`参数。
> 候选candidate指可能的补全结果如补全`tar -f`时目录下所有文件会被作为候选,补全`systemctl`时`start`、`stop`、`list-units`等指令会被作为候选。
> 还请不要感到灰心,读下去就是了。
*** 用于大部分补全的主要工具函数
| _alternative | 从其它工具函数或shell代码生成补全候选。 |
| _arguments | 指定如何补全一命令的各选项和参数命令选项风格为unix风格。 |
| _describe | 创建由带描述的词汇但不包含动作组成的简单补全。比_arguments更简单。 |
| _gnu_generic | 为带有“--help”选项的命令补全选项。 |
| _regex_arguments | 创建一先用regex表达式匹配命令行参数再执行动作/补全的函数。 |
*** 对单个词汇进行复杂补全的工具函数
| _values | 补全任意词汇(值)及其参数,或逗号分隔的词汇与参数列表。 |
| _combination | 补全值的组合,比如域名和用户名的二元组。 |
| _multi_parts | 对词汇的由符号分隔的多个部分分别补全,比如补全路径:/u/i/sy -> /usr/include/sys |
| _sep_parts | 类似_multi_parts但在补全的不同部分中允许不同的分隔符。 |
| _sequence | 包装另一补全函数,并补全由该函数生成的匹配列表。 |
*** 用于补全特定对象种类的函数
| _path_files | 补全文件目录。用多个选项控制行为。 |
| _files | 使用所有选项调用_path_files除了-g和-/。这些选项取决于file-patterns风格设置。 |
| _net_interfaces | 补全网络接口名称。 |
| _users | 补全用户名 |
| _groups | 补全组名 |
| _options | 补全shell选项名。 |
| _parameters | 补全shell参数/变量名(可用模式匹配限制要补全的参数/变量名)。 |
*** 处理已缓存的补全的函数
如果你有大量的补全,你可以将补全保存于一个缓存文件以快速加载。
| _cache_invalid | 指明补全缓存是否需要重新构建rebuild缓存由标识符指定 |
| _retrieve_cache | 从缓存文件获取补全信息 |
| _store_cache | 储存缓存于缓存文件,缓存由标识符指定 |
*** 其它函数
| _message | 当无补全可生成时显示帮助信息。 |
| _regex_words | 为_regex_arguments命令生成参数。比手写参数更简单。 |
| _guard | 检查被补全的词汇用于_arguments和类似函数的定义的ACTION中。 |
*** 动作Actions
许多工具函数如_arguments、_regex_arguments、_alternative和_values在选项/参数末尾有一个action。
这个action指定如何补全对应的参数。
这些action可以是如下形式之一
| ( ) | 需要提供参数但未生成任何匹配 |
| (ITEM1 ITEM2) | 匹配列表 |
| ((ITEM1\:'DESC1' ITEM2\:'DESC2')) | 匹配列表,带有描述。引号必须和整个定义所使用的引号不同。 |
| ->STRING | 将$state设为STRING然后继续可在调用工具函数后用case语句检查$state的值 |
| FUNCTION | 生成匹配或完成其它操作的函数的函数名比如_files或_message |
| {EVAL-STRING} | 把字符串当作shell代码执行evaluate。可用于带参调用工具函数如_values或_describe |
| =ACTION | 在补全命令行中插入占位词汇不改变补全点。 |
并非所有的action种类都可用于使用action的工具函数。比如->STRING类不可用于_regex_arguments或_alternative函数。
** 用_describe编写简单的补全函数
_describe函数可以用于简单的补全此类补全的选项/参数的位置与顺序无关紧要。
你只需用一个数组参数储存这些选项和其描述然后将数组参数的名作为参数传入_describe。
下面的示例创建补全候选c和d注意代码文件名应为_cmd并且文件存于$fpath所列出的目录之下
#+BEGIN_SRC sh
#compdef cmd
local -a subcmds
subcmds=('c:c命令描述' 'd:d命令描述')
_describe 'command' subcmds
#+END_SRC
你可以像下面一样使用由双横杠分隔的列表,但注意实操时会混合匹配结果,所以不应该用于不同种类的补全候选:
#+BEGIN_SRC sh
local -a subcmds topics
subcmds=('c:c命令描述' 'd:d命令的描述')
topics=('e:e帮助主题的描述' 'f:f帮助主题的描述')
_describe 'command' subcmds -- topics
#+END_SRC
如果两个候选有相同的描述_describe把它们集于一行并确保描述严格按列对齐。
_describe函数可用在_alternative、_arguments或_regex_arguments的ACTION中。
在这种情况下你需要用括号将_describe和参数包起来比如'TAG:DESCRIPTION:{_describe 'values' options}'
** 用_alternative编写补全函数
如同_describe该函数进行简单补全其选项/参数的顺序和位置并不重要。
然而与_describe的固定匹配不同_alternative可进一步调用函数生成补全候选。
此外_alternative允许混合不同种类的补全候选。
关于参数该函数接受一列定义specification每项定义的形式为“TAG:DESCRIPTION:ACTION”即“标签:描述:动作”其中TAG是一个标识补全匹配种类的特殊标签。
DESCRIPTION以标题heading的形式描述补全候选组而ACTION是先前列出的动作种类之一除了->STRING和=ACTION之外
例如:
#+BEGIN_SRC sh
_alternative 'arguments:自定义参数:(a b c)' 'files:文件名:_files'
#+END_SRC
第一个定义增加了补全候选a、b和c而第二个定义调用_files函数以补全文件目录。
我们可以用反斜杠 \ 将不同定义分成几行并给每个自定义参数加入描述文字:
#+BEGIN_SRC sh
_alternative \
'args:自定义参数:((a\:"描述a" b\:"描述b" c\:"描述c"))' \
'files:文件名:_files'
#+END_SRC
如果我们想向_files传递参数我们可以直接写在_files后面
#+BEGIN_SRC sh
_alternative \
'args:自定义参数:((a\:"描述a" b\:"描述b" c\:"描述c"))' \
'files:文件名:_files -/'
#+END_SRC
如要用变量展开创建补全列表,必须用双引号将定义括起来,
如:
#+BEGIN_SRC sh
_alternative \
"dirs:用户目录:($userdirs)" \
"pids:进程ID:($(ps -A o pid=))"
#+END_SRC
在此例子中第一个定义加入$userdirs变量中的词汇第二个定义执行'ps -A o pid='并获取pid表pid表用作补全候选。
实操中我们使用已有的_pids函数而不是像上面那样手写。
我们可以在ACTION中使用_values等其它工具函数以完成更复杂的补全
#+BEGIN_SRC sh
_alternative \
"directories:用户目录:($userdirs)" \
'options:逗号分隔选项: _values -s , letter a b c'
#+END_SRC
该示例补全$userdirs里的项目以及用逗号分隔的、含有a、b和/或c的列表。
注意_values前面的空格。空格不可省略因为_values不能接受标准compadd描述选项。
和_describe一样_alternative可用作ACTION并作为_arguments或_regex_arguments的一部分。
** 用_arguments编写补全函数
只需要调用_arguments函数一次就可以创造出非常智能的补全函数。该函数本身就是用于处理这种带有带参选项的命令的。
如同_alternative函数_arguments接受一列定义字符串参数。
这些定义字符串指定选项和任何对应的选项参数(如:-f 文件名),或命令参数。
简单的选项定义用'-OPT[DESCRIPTION]'(即'-选项[描述]'),比如:
#+BEGIN_SRC sh
_arguments '-s[排序后输出]' '--l[更多输出]' '-l[更多输出]'
#+END_SRC
选项参数可在选项描述后指定,形式用'-OPT[DESCRIPTION]:MESSAGE:ACTION'(即'-选项[描述]:消息:动作',
其中MESSAGE是待显示的信息而ACTION可以是前面的动作Actions章节提到的任何形式。
比如:
#+BEGIN_SRC sh
_arguments '-f[输入文件]:文件名:_files'
#+END_SRC
命令参数定义用'N:MESSAGE:ACTION'(即'N:消息:动作'其中N指定这是第N个命令参数而MESSAGE和ACTION都和前面的一样。
如果N被省略则其仅表示这是在所有已定义的参数之后的下一个参数。如果开头在N后面用的是双冒号则参数非必需。
比如:
#+BEGIN_SRC sh
_arguments '-s[排序后输出]' '1:第一个参数:_net_interfaces' '::可选参数:_files' ':下一个参数:(a b c)'
#+END_SRC
这里第一个参数是网络接口下一个可选参数是一个文件名最后一个参数可以是a、b或c而-s选项可以在任何位置被补全。
_arguments函数允许所有ACTION形式在前面的动作(Actions)章节列出)。
这表示你可以用动作来选择case语句分支
#+BEGIN_SRC sh
_arguments '-m[音乐文件]:文件名:->files' '-f[flags]:flag:->flags'
case "$state" in
files)
local -a music_files
music_files=( Music/**/*.{mp3,wav,flac,ogg} )
_multi_parts / music_files
;;
flags)
_values -s , 'flags' a b c d e
;;
esac
#+END_SRC
在此例子中指向音乐文件的路径被_multi_parts一步步地沿目录下降补全
而flags被_values函数按照逗号分隔列表补全。
我已经介绍了_arguments定义的基础部分你还可以定义互斥选项、重复选项和参数、以+开头的选项等。有关更多细节,查阅[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System][官方文档]]。
同时你也可以看看本文末尾列出的教程,以及[[https://github.com/vapniks/zsh-completions/tree/master/src][src目录]]下的补全函数。
** 用_regex_arguments和_regex_words编写补全函数
如果你的命令行定义比较复杂有多个可能的参数序列那你可能需要_regex_arguments函数。
该函数也适用于有一系列跟着多个参数的关键词的情况。
_regex_arguments创建名字由第一个参数给出的补全函数。
因此你需要先调用_regex_arguments来创建补全函数然后再调用该函数比如
#+BEGIN_SRC sh
_regex_arguments _cmd OTHER_ARGS..
_cmd "$@"
#+END_SRC
OTHER_ARGS即“其它参数”是一序列用于在命令行上匹配和补全词汇的定义。
这些序列可被'|'分隔来表示备选词汇序列。
你可以用任意嵌套深度的括号来指定备选序列,但括号必须带反斜杠前缀,如\( \),或用引号括起来,如'(' ')'。
比如:
#+BEGIN_SRC sh
_regex_arguments _cmd 序列1 '|' 序列2 \( 序列2a '|' 序列2b \)
_cmd "$@"
#+END_SRC
该示例定义一个匹配序列1或序列2后跟着序列2a或序列2b的命令行。这种方式和正则表达式语法类似。
一个序列中的每个定义必须在开头包含一个/ PATTERN/ (即/ 模式/)部分,后跟着可选的':TAG:DESCRIPTION:ACTION'(即':标签:描述:动作')部分。
每个PATTERN是一个匹配一命令行词汇的正则表达式。这些模式按顺序匹配直到某个模式不匹配不匹配的模式将执行对应的ACTION动作以进行补全。
注意,一定要有一个匹配命令自身的模式。
下面有对PATTERN模式更详细的解释。
':TAG:DESCRIPTION:ACTION'的使用方法和_alternative相同只是开头多了个冒号“:”并且前面列出的所有ACTION格式都可用。
例如:
#+BEGIN_SRC sh
_regex_arguments _cmd /$'[^\0]##\0'/ \( /$'word1(a|b|c)\0'/ ':word:first word:(word1a word1b word1c)' '|'\
/$'word11(a|b|c)\0'/ ':word:first word:(word11a word11b word11c)' \( /$'word2(a|b|c)\0'/ ':word:second word:(word2a word2b word2c)'\
'|' /$'word22(a|b|c)\0'/ ':word:second word:(word22a word22b word22c)' \) \)
_cmd "$@"
#+END_SRC
TODO 英文原文和例子有出入
在这个例子中第一个词可以是word1即“词1”下同或者word11后紧跟a、b或c并且如果第一个词含有11则第二个词可以是word2后紧跟a、b或c或一个文件名。
如果感觉太复杂你也可以用更简单的_regex_words函数达到相同效果。
*** 模式
你可能注意到了上个例子中的/ PATTERN/和普通的正则表达式不太一样。
通常使用的是形如$'foo\0'的字符串参数。这是为了让\0表示成null字符而zsh内部用来分隔词汇的也是null字符。
如果不在末尾包含\0的话可能会无法匹配下一个词。如果你要把一个变量的值作为模式的一部分你可以用双括号包起来
这样变量就会展开然后再在后面加个包含null字符的字符串参数比如"$somevar"$'\0'somevar即“某变量”
表示模式用的正则表达式语法和正常的正则表达式不太一样,但我也找不到有关的文档。
不过我还是试图搞清楚了这些特殊字符的意义:
| * | 通配符 - 任何数量的字符 |
| ? | 通配符 - 单个字符 |
| # | 零个或更多的上一个字符(和一般正则表达式里的*一样) |
| ## | 一个或更多的上一个字符(和一般正则表达式里的+一样) |
*** _regex_words
_regex_words函数比_regex_arguments更简单易用。
调用_regex_words后的结果可以存在变量里。
要用_regex_words创建一个定义specification你需要提供一个标签后跟一段描述后跟一个定义不同词汇的列表。
这些定义采用'WORD:DESCRIPTION:SPEC'(即'词汇:描述:定义'的格式WORD即待补全的词DESCRIPTION是对应的描述
SPEC可以是由_regex_words创建的另一个变量以指定当前词后的下一个词汇也可以留空以表示没有更多的词。
比如:
#+BEGIN_SRC sh
_regex_words firstword '第一个词' 'word1a:词a:' 'word1b:词b:' 'word1c:词c'
#+END_SRC
该函数的返回结果将被存入$replyreply即“回复”、“回应”数组里所以我们需要在$reply变化前将结果存进另一个数组里
#+BEGIN_SRC sh
local -a firstword
_regex_words word 'The first word' 'word1a:a word:' 'word1b:b word:' 'word1c:c word'
firstword="$reply[@]"
#+END_SRC
firstword即“第一个词”。
然后我们可以把结果用在_regex_arguments里
#+BEGIN_SRC sh
_regex_arguments _cmd /$'[^\0]##\0'/ "$firstword[@]"
_cmd "$@"
#+END_SRC
注意到我给命令自身也加了模式。
这里还有个更复杂的词汇我们调用_regex_words以匹配不同词汇
#+BEGIN_SRC sh
local -a firstword firstword2 secondword secondword2
_regex_words word1 '第二个词' 'woo:鄧族' 'hoo:不关我事'
secondword=("$reply[@]")
_regex_words word2 '另一个第二个词' 'yee:汝' 'haa:很搞笑!'
secondword2=("$reply[@]")
_regex_words commands '第一个词' 'foo:做foo' 'man:yeah man' 'chu:at chu' # 译注作者在自嗨at chu除了比较像at you外没什么特殊意义
firstword=("$reply[@]")
_regex_words word4 '另一个第一个词' 'boo:吓死某人:$secondword' 'ga:嘤嘤嘤:$secondword'\
'loo:上厕所:$secondword2'
firstword2=("$reply[@]")
_regex_arguments _hello /$'[^\0]##\0'/ "${firstword[@]}" "${firstword2[@]}"
_hello "$@"
#+END_SRC
在这个例子中第一个词可以是"foo"、"man"、"chu"、"boo"、"ga"或"loo"。
如果第一个词是"boo"或"ga",那下一个词可以是"woo"或"hoo"
而如果第一个词是"loo"则第二个词可以是"yee"或"haa",其它情况下没有第二个词。
_ip函数是_regex_words的一个好用例。
** 用_values、_sep_parts和_multi_parts实现复杂补全
_values、_sep_parts和_multi_parts可以单独使用也可以作为_alternative、_arguments或_regex_arguments定义里的ACTION。可以看看下面的例子。
查阅[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System][官方文档]]以了解更多信息。
空格分隔的mp3文件列表
#+BEGIN_SRC sh
_values 'mp3文件' ~/*.mp3
#+END_SRC
逗号分隔的会话id列表
#+BEGIN_SRC sh
_values -s , '会话id' "${(uonzf)$(ps -A o sid=)}"
#+END_SRC
补全foo@news:woo、foo@news:laa或bar@news:woo等
#+BEGIN_SRC sh
_sep_parts '(foo bar)' @ '(news ftp)' : '(woo laa)'
#+END_SRC
补全MAC地址一次补全一个字节
#+BEGIN_SRC sh
_multi_parts : '(00:11:22:33:44:55 00:23:34:45:56:67 00:23:45:56:67:78)'
#+END_SRC
** 用compadd直接添加补全词
你可以使用内置的compadd函数增加补全词以获得更细致的控制。
这个函数有各种选项控制如何显示补全以及补全时如何替换命令行上的文字。
阅读[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System][官方文档]]以获得更多细节。
这里我只给出几个简单的示例。
向补全列表里加入若干词汇:
#+BEGIN_SRC sh
compadd foo bar blah
#+END_SRC
同上但显示注释:
#+BEGIN_SRC sh
compadd -X '一些注释' foo bar blah
#+END_SRC
同上但在补全前自动插入"what_"前缀
#+BEGIN_SRC sh
compadd -P what_ foo bar blah
#+END_SRC
同上但补全后自动插入"_todo"后缀:
#+BEGIN_SRC sh
compadd -S _todo foo bar blah
#+END_SRC
同上但在后缀后打空格时自动移除"_todo"后缀:
#+BEGIN_SRC sh
compadd -P _todo -q foo bar blah
#+END_SRC
向补全数组$wordsarraywordsarray即“词数组”加入词汇
#+BEGIN_SRC sh
compadd -a wordsarray
#+END_SRC
* 测试与debug
重新加载补全函数:
#+BEGIN_SRC sh
> unfunction _func
> autoload -U _func
#+END_SRC
这些函数会提供有用的信息。
如果默认按键没有用你可以尝试Alt+x然后再输入命令名。
| 函数 | 默认按键 | 作用 |
|-----------------+--------------------+----------------------------------------------------------------|
| _complete_help | Ctrl+x h | 在当前光标位置补全时显示有关上下文名称、标签和补全函数的信息 |
| _complete_help | Alt+2 Ctrl+x h | 同上但显示更多信息 |
| _complete_debug | Ctrl+x ? | 执行正常补全但跟踪补全系统执行的shell命令并存入一个临时文件 |
* 踩坑了吧 (需要小心的东西)
记得在补全函数的文件开头加那行#compdef
_arguments或_regex_arguments的定义中要使用正确的引号
如果定义中有变量要展开,用双引号,其它情况用单引号,
并且记得在ITEM项目描述处用不同的引号。译注见[[#动作Actions][动作Actions]]章节)
_arguments、_alternative、_regex_arguments等的定义处要在正确的地方使用正确数量的冒号“:”。
使用_regex_arguments时要记得在开头写匹配命令的模式不需要加入匹配动作action
记得在_regex_arguments的任何PATTERN模式参数后加上null字符$'\0'
* 小贴士
有时一个子命令后只会跟一个选项这时zsh会在tab在子命令后按下时自动补全。如果你想要在补全前先列出选项和描述
你可以向ACTION动作加入另一个空选项比如\:),如':TAG:DESCRIPTION:((opt1\:"opt1描述" \:))'
注意这只对在定义参数中使用ACTION的工具函数_arguments、_regex_arguments等有效。
* 其它资源
[[https://wikimatze.de/writing-zsh-completion-for-padrino/][]]是个展示_arguments函数的基本使用方法的教程短小精悍
而[[https://web.archive.org/web/20190411104837/http://www.linux-mag.com/id/1106/][]]是_arguments函数的稍稍更进阶的教程。
[[https://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System][]]是zshcompsys手册页man page