bash自动补全:添加可能完成的说明 [英] bash autocompletion: add description for possible completions

查看:138
本文介绍了bash自动补全:添加可能完成的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使bash自动完成看起来像Cisco IOS Shell中的那样?

Is it possible to make bash auto-completion look like in Cisco IOS shell?

我的意思是为每个完成情况添加简短说明,例如:

I mean to add short descriptions for each completion, like this:

telnet 10.10.10. (TAB Pressed)
 10.10.10.10 - routerA
 10.10.10.11 - routerB

其中10.10.10.10和10.10.10.11是可能的补全 和路由器routerB只是描述(不执行).

where 10.10.10.10 and 10.10.10.11 are possible completions and routerA & routerB just descriptions (not to be executed).

我知道bash可以用"complete -W"来完成命令,但是它能够为它们打印描述吗?

I know that bash can complete commands with "complete -W", but is it able to print descriptions for them?

推荐答案

我对此有一个解决方案,它不需要两次按TAB或回显任何其他信息.关键是要检查是否只有一个补全,然后将该补全剥离到有效部分,通常是通过删除注释"定界符之后的最大匹配后缀.完成OP的示例:

I have a solution to this that does not require pressing TAB more than twice or echoing any extra information. The key is to check whether there is only one completion, then strip that completion down to the valid portion, usually by removing the largest matching suffix after your "comment" delimiter. To accomplish the OP's example:

_telnet() {
  COMPREPLY=()
  local cur
  cur=$(_get_cword)
  local completions="10.10.10.10 - routerA
10.10.10.11 - routerB
10.20.1.3 - routerC"

  local OLDIFS="$IFS"
  local IFS=$'\n'
  COMPREPLY=( $( compgen -W "$completions" -- "$cur" ) )
  IFS="$OLDIFS"
  if [[ ${#COMPREPLY[*]} -eq 1 ]]; then #Only one completion
    COMPREPLY=( ${COMPREPLY[0]%% - *} ) #Remove ' - ' and everything after
  fi
  return 0
}
complete -F _telnet -A hostnames telnet

这将提供您要查找的确切输出,并且当只有一个可能的完成时,注释将在完成前从中删除.

This gives the exact output you're looking for, and when there is only one possible completion, the comment is stripped from it before completing.

这篇关于bash自动补全:添加可能完成的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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