Dan*_*anH 6 bash tab-completion
当我输入文件名并双击 Tab 时,会出现一个目录列表,为 Tab 完成建议所有选项。默认情况下,ls
它以与ls -l
. 这可能吗?
我也有兴趣看看是否有任何进一步的定制可以沿着这条路完成,虽然我不确定我想要什么,只是一些想法的例子也会很酷。
简单地说,没有。
原因:
“set”或“shopt”中没有选项来指定选项卡完成建议的输出格式。唯一的例外是COLUMNS环境,但是,您不能将其更改为不同的值。
对于自定义完成(如 --option 完成),您可以使用输出到 stdout/stderr 来覆盖完成功能,以显示类似的内容ls -l
以及完成建议。但是,文件名补全是硬编码的,您不能通过complete
内置文件覆盖它。
这是一个简短的脏示例,用于显示详细信息以及选项卡完成建议。假设您有一个程序foo
,它接受四个选项bar
, barr
, barrr
, car
,脏完成功能将是:
function _foo() {
local cmds=(bar barr barrr car)
local cur n s
if [ $COMP_CWORD = 1 ]; then
cur="${COMP_WORDS[1]}"
n="${#cur}"
# -- dirty hack begin --
echo
cat <<EOT | while read s; do [ "${s:0:n}" = "$cur" ] && echo "$s"; done
bar: choose this option if you feel well
barr: choose this option if you feel hungry
barrr: choose this option if you are starving
car: choose this option if you want a car
EOT
# ++ dirty hack end ++
COMPREPLY=($(compgen -W "${cmds[*]}" "$cur"))
fi
} && complete -F _foo foo
Run Code Online (Sandbox Code Playgroud)
现在您可以在建议之前看到一个简短的帮助文档:
$ foo ba<tab>
bar: choose this option if you feel well
barr: choose this option if you feel hungry
barrr: choose this option if you are starving
r
Run Code Online (Sandbox Code Playgroud)
(最后一行中的单个字符 'r' 是前缀的自动补全ba
。)
并且,当前缀不明确时,完成函数会被评估两次,建议列表放在最后:
$ foo bar<tab><tab>
bar: choose this option if you feel well
barr: choose this option if you feel hungry
barrr: choose this option if you are starving
bar: choose this option if you feel well
barr: choose this option if you feel hungry
barrr: choose this option if you are starving
bar barr barrr
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1418 次 |
最近记录: |