使zsh来自文件的完整参数 [英] Make zsh complete arguments from a file

查看:97
本文介绍了使zsh来自文件的完整参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

zsh很棒,但是它的完成系统非常多样化.而且该文档缺少很好的示例.是否有用于完成特定应用程序的模板.完成会从文件中获取其匹配数据,并以换行符分隔吗?

zsh is great but its completion system is very diverse. And the documentation lacks good examples. Is there a template for completing for a specific application. The completion would get its match data from a file, separated by newlines?

我尝试修改一个较旧的示例,该示例采用匹配数据实时":

I tried modifying an older example of mine that takes match data "live":

~ % cat .zsh/completers/_jazzup 
#compdef jazz_up
_arguments "2: :(`mpc lsplaylists|sed -e 's# #\\\\ #g'`)"

我可以在其中提供cat my_file而不是mpc调用,依此类推,但是有没有更优雅的方式来完成此简单任务呢?而且这种完成是针对特定位置的:您能否提供一个示例,在该示例中,zsh会在识别出程序名称后的任何时候尝试完成?

I could supply cat my_file there instead of mpc invocation and so on but would there be a more elegant way to do this simple task? And that completion there is placement-specific: can you provide an example where zsh would attempt to complete at any point after the program name is recognized?

匹配数据将包含空格,依此类推,补全应转义WS.例子:

The match data will have whitespaces and so on, the completion should escape the WS. Example of that:

Foo bar
Barbaric
Get it (42)

现在,如果将为命令Say配置该完成,则应该从zsh中得到这种行为:

Now if that completion would be configured for a command Say, we should get this kind of behaviour out of zsh:

$ Say Fo<TAB>
$ Say Foo\ bar
$ Say Ge<TAB>
$ Say Get\ it\ \(42\)

推荐答案

使用_describe可以更好地解决简单的完成需求,它可以对包含完成选项及其描述的数组进行配对(您可以使用多个数组/描述对,检查手册).

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很棒,但是太复杂了.)

(_arguments is great but too complex.)

[...]

首先创建一个文件

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

然后在$fpath中的某个位置创建文件_simple:

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})'

然后

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

simple [TAB]

有效.只要确保将完成文件_simple放置在fpath中的某个位置即可.

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

请注意,选项列表中的:应该用于将选项与其(单独的)描述(oh:noes)分开.因此,除非您引用oh\:yes,否则它对_describe不起作用.带注释的_arguments示例将不使用:作为分隔符.

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.

这篇关于使zsh来自文件的完整参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆