问题
I have a bunch of ZSH functions which are pretty verbose, and I'd like to use zsh completion on them. Example:
qq-enum-dns-txfr-host
qq-enum-dns-brute-rev
qq-enum-dns-tcpdump
qq-enum-web-php-lfi-logfile
qq-enum-smb-tcpdump
qq-enum-web-php-ffuf-common-php
qq-enum-ftp-tcpdump
When I type qq-tcp
Tab I'd like to get completions of:
qq-enum-dns-tcpdump
qq-enum-smb-tcpdump
qq-enum-ftp-tcpdump
All the documentation I can find deals with how to complete arguments to a given command or function, but doesn't say how to use the substring (not prefix) completion to figure out which function to use in the first place.
I guess I could refactor so that each function is instead an argument to a qq
command. But is there a simpler way I'm missing?
回答1:
You can configure the completion system extensively with the zstyle builtin. By default, completion only tries to add a suffix, but you can change this with a matcher-list style. To apply the style to all completions:
zstyle ':completion:*' matcher-list …
To apply the style only when the context is a command, and only for commands that are functions:
zstyle ':completion:*:*:-command-:*:functions matcher-list …
What goes after matcher-list
is a list of completion matching control specifications. Zsh tries them in order, and stops as soon as one finds a match. Use the empty specification to just add a suffix; you'll usually want this first except in very specific contexts where this makes it hard to reach some completions. You can use an r
specification to allow partial completion to the left of punctuation characters: r:|[-+./_]=*
means that wherever the string to complete contains one of the characters -+./_
, a matching completion can have anything (anything matching *
, which matches everything) to the left of this punctuation character. For example, qq-tcp
Tab completes to anything matching qq*-tcp*
(the final *
is from regular suffix completion).
zstyle ':completion:*' matcher-list '' 'r:|[-+./_]=*'
You can activate this through the interactive basic customization interface compinstall
, under “Matching control”, as “partial-word completion”.
回答2:
In your ~/.zshrc
, you can declare aliases which will be taken in account for the completion
alias qq-tcpdump-enum-dns='qq-enum-dns-tcpdump'
alias qq-tcpdump-enum-smb='qq-enum-smb-tcpdump'
alias qq-tcpdump-enum-ftp='qq-enum-ftp-tcpdump'
(Then you can source ~/.zshrc
in already open terminals to get the aliases available.)
来源:https://stackoverflow.com/questions/60705510/completing-command-name-itself-in-zsh