Make zsh complete arguments from a file

假如想象 提交于 2019-12-03 00:22:58
Francisco

Simple completion needs are better addressed with _describe, it pairs an array holding completion options and a description for them (you can use multiple array/description pairs, check the manual).

(_arguments is great but too complex.)

[...]

First create a file

echo "foo\nbar\nbaz\nwith spac e s\noh:noes\noh\:yes" >! ~/simple-complete

Then create a file _simple somewhere in your $fpath:

#compdef simple

# you may wish to modify the expansion options here
# PS: 'f' is the flag making one entry per line
cmds=( ${(uf)"$(< ~/simple-complete)"} ) 

# main advantage here is that it is easy to understand, see alternative below
_describe 'a description of the completion options' cmds

# this is the equivalent _arguments command... too complex for what it does
## _arguments '*:foo:(${cmds})'

then

function simple() { echo $* }
autoload _simple # do not forget BEFORE the next cmd! 
compdef _simple simple # binds the completion function to a command

simple [TAB]

it works. Just make sure the completion file _simple is placed somewhere in your fpath.

Notice that : in the option list is supposed to be used for separating an option from their (individual) description (oh:noes). So that won't work with _describe unless you quote it (oh\:yes). The commented out _arguments example will not use the : as a separator.

Without changing anything further in .zshrc (I already have autoload -Uz compinit compinit) I added the following as /usr/local/share/zsh/site-functions/_drush

#compdef drush
_arguments "1: :($(/usr/local/bin/aliases-drush.php))"

Where /usr/local/bin/aliases-drush.php just prints a list of strings, each string being a potential first argument for the command drush. You could use ($(< filename)) to complete from filename.

I based this on https://unix.stackexchange.com/a/458850/9452 -- it's surprising how simple this is at the end of the day.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!