gohugo 如何在bash命令下自动补全(tab 提示命令)

gohugo 是用 go 语言编写的一个静态网站生成程序。名是 hugo 支持命令自动补全。当然需要设置一下,这里使用 rockylinux 作为示例。

开启 bash 的自动补全命令

dnf install bash-completion
# 或者
yum install bash-completion

确认下 hugo 是否支持bash自动补全

hugo completion bash
# bash completion V2 for hugo                                 -*- shell-script -*-

__hugo_debug()
{
    if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
        echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
    fi
}

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
__hugo_init_completion()
{
    COMPREPLY=()
    _get_comp_words_by_ref "$@" cur prev words cword
}

# This function calls the hugo program to obtain the completion
# results and the directive.  It fills the 'out' and 'directive' vars.
__hugo_get_completion_results() {
    local requestComp lastParam lastChar args

    # Prepare the command to request completions for the program.
    # Calling ${words[0]} instead of directly hugo allows handling aliases
    args=("${words[@]:1}")
    requestComp="${words[0]} __complete ${args[*]}"

    lastParam=${words[$((${#words[@]}-1))]}
    lastChar=${lastParam:$((${#lastParam}-1)):1}
    __hugo_debug "lastParam ${lastParam}, lastChar ${lastChar}"

重点来了,运行以下命令生成补全脚本并保存到系统目录

hugo completion bash > /etc/bash_completion.d/hugo

重启终端

输入 hugo 点击两次 tab 自动补全

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注