【问题标题】:Hints for custom bash completion自定义 bash 完成的提示
【发布时间】:2017-08-21 08:54:08
【问题描述】:

我正在开发一个自定义的bash 完成命令,以从调度系统(LSF、PBS、SLURM)中捕获作业 ID。我已经有了基本功能,但现在我想用我在运行zsh 时看到的“提示”来扩展它。

例如,当我在下面的 grep 示例中按 TAB 时,我得到:

grep -<TAB>
     --after-context          -A           -- specify lines of trailing context
     --basic-regexp           -G           -- use basic regular expression
     --before-context         -B           -- specify lines of leading context
...

-- 之后的第三列是我想添加到我自己的 bash 完成中的内容。它的正确技术术语是什么?提示? compgen 是否提供了执行此操作的功能?

我附上了我当前的工作示例,它仅提供 ID。该示例使用 LSF。

# LSF Job ID completion
function _mycurrentjobs()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(bjobs -noheader -u $USER -o JOBID)" -- $cur))
    return 0
}
complete -F _mycurrentjobs bkill bjobs bstatus bpeek bstop bresume

提供 ID 和我想要的提示的命令是: bjobs -noheader -u $USER -o "JOBID JOB_NAME"

【问题讨论】:

标签: linux bash zsh tab-completion bash-completion


【解决方案1】:

在查看有关主机完成bash autocompletion: add description for possible completions 的类似帖子后,我或多或少地得到了正确的行为。我在我的作业 ID 查询命令中使用 - 作为分隔符

function _mycurrentjobs()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local OLDIFS="$IFS"
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "$(bjobs -noheader -u $USER \
        -o "JOBID JOB_NAME delimiter='-'")" -- $cur))
    IFS="$OLDIFS"
    if [[ ${#COMPREPLY[*]} -eq 1 ]]; then   #Only one completion
        COMPREPLY=( ${COMPREPLY[0]%%-*} )   #Remove the separator and everything after
    fi
    return 0
}
complete -F _mycurrentjobs bkill bjobs bstatus bpeek bstop bresume

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多