💾 Archived View for frosch03.de › 2013-02-10-ZSH-Compeltion.gmi captured on 2021-12-04 at 18:04:22. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

giving frogblog zsh completion

I always was annoyed, that working with the frogblog was a pain. One has to type the correct subject name into the command line. Therefore you must first get a list of all the subjects by frogblog list.

I wondered always, how to enable completion inside my console. Because i use zsh as my shell, i show here how to enable completion for frogblog in zsh.

Completion Folders

The folder where zsh looks for completions are listed in the $fpath variable. You can add the completion file in one of the existing folders. I created ~/.zsh/complete and added the path to the $fpath. Here are the additional lines for .zshrc:

fpath=(~/.zsh/complete $fpath) 

Also make sure that the compinit is autoloaded (what ever that means) and that the right completion menu is known.

autoload -U compinit compinit

zstyle ':completion:*' menu select=2

Completion file for frogblog

Completion files are named with a starting '\_'. Here is the content of ~/.zsh/complete/_frogblog

_frogblog() {
    local curcontext="\$curcontext" state line
    typeset -A opt_args

    _arguments \
        '1: :->action'\
        '*: :->subject'

    case \$state in
    action)
        _arguments '1:actions:(list publish delete)'
    ;;
    *)
        case \$words[2] in
        list)
        ;;
        publish)
            compadd "\$@" `ls *.hlog`
        ;;
        delete)
            compadd "\$@" `frogblog list | xargs`
        ;;

        *)
            _files
        esac
    esac
}

_frogblog "$@"

The _arguments need to be first an action, which can be of the list (list publish delete). For every one of these actions there are different argument to be submitted.

If you want to know more how this mechanism actually works, have a look at the page i found the most useful information about zsh completion: ZSH -- Writing own completion functions(1).

(1): http://askql.wordpress.com/2011/01/11/zsh-writing-own-completion/