[GH-ISSUE #1653] shell autocompletion #62962

Open
opened 2026-05-03 11:01:52 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @teto on GitHub (Dec 21, 2023).
Original GitHub issue: https://github.com/ollama/ollama/issues/1653

hi, I would like to add shell autocompletion for ollama to my linux distribution.
I dont know go but the cli parser seems to be using https://github.com/spf13/cobra, the same as github CLI gh.
The nix package generates the autocompletion like this:
dbaa028d61/pkgs/applications/version-management/gh/default.nix (L30)
so I tried ollama completion -s zsh hoping it was not shown by ollama --help because hidden but that failed.
Is there a way to generate those files currently and if yes, how, if not, could you add such a subcommand please ? makes writing commands on the prompt easier.

Originally created by @teto on GitHub (Dec 21, 2023). Original GitHub issue: https://github.com/ollama/ollama/issues/1653 hi, I would like to add shell autocompletion for ollama to my linux distribution. I dont know go but the cli parser seems to be using https://github.com/spf13/cobra, the same as github CLI `gh`. The nix package generates the autocompletion like this: https://github.com/teto/nixpkgs/blob/dbaa028d61848fda534a04ce21459b722f0bca81/pkgs/applications/version-management/gh/default.nix#L30 so I tried `ollama completion -s zsh` hoping it was not shown by `ollama --help` because hidden but that failed. Is there a way to generate those files currently and if yes, how, if not, could you add such a subcommand please ? makes writing commands on the prompt easier.
GiteaMirror added the feature request label 2026-05-03 11:01:52 -05:00
Author
Owner

@V4G4X commented on GitHub (Feb 25, 2024):

Bump

<!-- gh-comment-id:1962938332 --> @V4G4X commented on GitHub (Feb 25, 2024): Bump
Author
Owner

@obeone commented on GitHub (May 5, 2024):

Here is a sh version that works somewhat :

https://gist.github.com/obeone/9313811fd61a7cbb843e0001a4434c58

<!-- gh-comment-id:2094566467 --> @obeone commented on GitHub (May 5, 2024): Here is a sh version that works somewhat : https://gist.github.com/obeone/9313811fd61a7cbb843e0001a4434c58
Author
Owner

@nanvenomous commented on GitHub (May 28, 2024):

I bet they disabled the cobra default command because completion is a common term for llm's to not confuse folks. Seems like it would be better to add it back under a flag like ollama --shell-completion zsh.

I think I cannot add the branch because I am not maintainer? but I made a go at it: https://github.com/ollama/ollama/pull/4690

btw I'm looking for a job. if you know anyone that needs golang fullstack, data, or embedded :)

<!-- gh-comment-id:2136231128 --> @nanvenomous commented on GitHub (May 28, 2024): I bet they disabled the cobra default command because `completion` is a common term for llm's to not confuse folks. Seems like it would be better to add it back under a flag like `ollama --shell-completion zsh`. I think I cannot add the branch because I am not maintainer? but I made a go at it: https://github.com/ollama/ollama/pull/4690 btw I'm looking for a job. if you know anyone that needs golang fullstack, data, or embedded :)
Author
Owner

@AdamNiederer commented on GitHub (Jun 23, 2024):

This is a quick and dirty solution for bash completion:

_complete_ollama() {
    local cur prev words cword
    _init_completion -n : || return

    if [[ ${cword} -eq 1 ]]; then
        COMPREPLY=($(compgen -W "serve create show run push pull list ps cp rm help" -- "${cur}"))
    elif [[ ${cword} -eq 2 ]]; then
        case "${prev}" in
            (run|show|cp|rm|push|list)
                WORDLIST=$((ollama list 2>/dev/null || echo "") | tail -n +2 | cut -d "	" -f 1)
                COMPREPLY=($(compgen -W "${WORDLIST}" -- "${cur}"))
                __ltrim_colon_completions "$cur"
                ;;
        esac
    fi
}
complete -F _complete_ollama ollama

Assumes you have the bash-completion package installed. Dump it in your .bashrc

<!-- gh-comment-id:2184527185 --> @AdamNiederer commented on GitHub (Jun 23, 2024): This is a quick and dirty solution for bash completion: ```bash _complete_ollama() { local cur prev words cword _init_completion -n : || return if [[ ${cword} -eq 1 ]]; then COMPREPLY=($(compgen -W "serve create show run push pull list ps cp rm help" -- "${cur}")) elif [[ ${cword} -eq 2 ]]; then case "${prev}" in (run|show|cp|rm|push|list) WORDLIST=$((ollama list 2>/dev/null || echo "") | tail -n +2 | cut -d " " -f 1) COMPREPLY=($(compgen -W "${WORDLIST}" -- "${cur}")) __ltrim_colon_completions "$cur" ;; esac fi } complete -F _complete_ollama ollama ``` Assumes you have the `bash-completion` package installed. Dump it in your .bashrc
Author
Owner

@geroldmeisinger commented on GitHub (Jun 23, 2024):

This is a quick and dirty solution for bash completion:
...
Assumes you have the bash-completion package installed. Dump it in your .bashrc

works. thx!
(ollama serve needs to be running)

<!-- gh-comment-id:2184633463 --> @geroldmeisinger commented on GitHub (Jun 23, 2024): > This is a quick and dirty solution for bash completion: > ... > Assumes you have the `bash-completion` package installed. Dump it in your .bashrc works. thx! (`ollama serve` needs to be running)
Author
Owner

@nicognaW commented on GitHub (Dec 4, 2024):

For zsh, thanks to Obeone's Gist and ocodo's plugin, you can now install a plugin to enable both command and model name completions.

An Antidote instruction is included in the plugin repo, if you use oh-my-zsh, do this:

git clone https://github.com/ocodo/ollama_zsh_completion/ ~/.oh-my-zsh/custom/plugins/ollama

And enable the plugin in your ~/.zshrc like plugins=(... ollama).

<!-- gh-comment-id:2516218197 --> @nicognaW commented on GitHub (Dec 4, 2024): For zsh, thanks to [Obeone's Gist](https://gist.github.com/obeone/9313811fd61a7cbb843e0001a4434c58) and [ocodo's plugin](https://github.com/ocodo/ollama_zsh_completion), you can now install a plugin to enable both command and **model name** completions. An [Antidote](https://github.com/mattmc3/antidote) instruction is included in the plugin repo, if you use oh-my-zsh, do this: ```sh git clone https://github.com/ocodo/ollama_zsh_completion/ ~/.oh-my-zsh/custom/plugins/ollama ``` And enable the plugin in your `~/.zshrc` like `plugins=(... ollama)`.
Author
Owner

@ehrlz commented on GitHub (Apr 27, 2025):

This is a quick and dirty solution for bash completion:

_complete_ollama() {
local cur prev words cword
_init_completion -n : || return

if [[ ${cword} -eq 1 ]]; then
    COMPREPLY=($(compgen -W "serve create show run push pull list ps cp rm help" -- "${cur}"))
elif [[ ${cword} -eq 2 ]]; then
    case "${prev}" in
        (run|show|cp|rm|push|list)
            WORDLIST=$((ollama list 2>/dev/null || echo "") | tail -n +2 | cut -d "	" -f 1)
            COMPREPLY=($(compgen -W "${WORDLIST}" -- "${cur}"))
            __ltrim_colon_completions "$cur"
            ;;
    esac
fi

}
complete -F _complete_ollama ollama
Assumes you have the bash-completion package installed. Dump it in your .bashrc

I've done a more elaborated solution here. I hope it's useful (needs ollama serve running)

<!-- gh-comment-id:2833674884 --> @ehrlz commented on GitHub (Apr 27, 2025): > This is a quick and dirty solution for bash completion: > > _complete_ollama() { > local cur prev words cword > _init_completion -n : || return > > if [[ ${cword} -eq 1 ]]; then > COMPREPLY=($(compgen -W "serve create show run push pull list ps cp rm help" -- "${cur}")) > elif [[ ${cword} -eq 2 ]]; then > case "${prev}" in > (run|show|cp|rm|push|list) > WORDLIST=$((ollama list 2>/dev/null || echo "") | tail -n +2 | cut -d " " -f 1) > COMPREPLY=($(compgen -W "${WORDLIST}" -- "${cur}")) > __ltrim_colon_completions "$cur" > ;; > esac > fi > } > complete -F _complete_ollama ollama > Assumes you have the `bash-completion` package installed. Dump it in your .bashrc I've done a more elaborated solution [here](https://github.com/ehrlz/ollama-bash-completion-plugin). I hope it's useful (needs ollama serve running)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#62962