Skip to content

Shell Autocompletion

Alex Pasmantier edited this page Jan 5, 2025 · 5 revisions

Television can integrate with your shell to provide smart autocompletion based on the commands you start typing.

tv-shell-integration

Keybindings

  • Ctrl-R: shell history
  • Ctrl-T: smart autocompletion for the current prompt command

Enabling shell integration

Zsh

To enable shell integration for zsh, run:

echo 'eval "$(tv init zsh)"' >> ~/.zshrc

And then restart your shell or run:

source ~/.zshrc

Bash

To enable shell integration for bash, run:

echo 'eval "$(tv init bash)"' >> ~/.bashrc

And then restart your shell or run:

source ~/.bashrc

Fish

To enable shell integration for fish, add:

tv init fish | source

to your is-interactive block in your ~/.config/fish/config.fish file and then restart your shell.

Support for Nushell is coming soon.

Configuring autocompletion

Shell integration works by setting a dedicated shell keybinding that launches tv with the current prompt buffer so that tv may guess which channel (builtin or cable) is the most appropriate.

Which channel gets effectively chosen for different commands can be tweaked in the shell_integration section of the configuration file:

[shell_integration.commands]
"ls" = "dirs"
"cat" = "files"

Each key is a command that will trigger tv with the corresponding channel as value.

Example: say you want the following prompts to trigger the following channels when pressing :

  • git checkout should trigger the git-branches channel
  • ls should trigger the dirs channel
  • cat should trigger the files channel

You would add the following to your configuration file:

[shell_integration.commands]
"git checkout" = "git-branches"
"ls" = "dirs"
"cat" = "files"

The default configuration is as follows:

[shell_integration.commands]

# environment variables
"export" = "env"
"unset" = "env"

# dirs channel
"cd" = "dirs"
"ls" = "dirs"
"rmdir" = "dirs"

# files channel
"cat" = "files"
"less" = "files"
"head" = "files"
"tail" = "files"
"vim" = "files"
"bat" = "files"

# git-branch channel
"git checkout" = "git-branch"
"git branch -d" = "git-branch"

# docker-images channel
"docker run" = "docker-images"

# gitrepos channel
"nvim" = "git-repos"

Automatically executing selection

The shell integration scripts can be obtained (and modified) by running:

tv init zsh

Uncommenting zle accept-line below will automatically execute the command when accepting a suggestion:

_tv_search() {
    emulate -L zsh
    zle -I

    local current_prompt
    current_prompt=$LBUFFER

    local output

    output=$(tv --autocomplete-prompt "$current_prompt" $*)

    zle reset-prompt

    if [[ -n $output ]]; then
        RBUFFER=""
        LBUFFER=$current_prompt$output

        # uncomment this to automatically accept the line
        # (i.e. run the command without having to press enter twice)
        # zle accept-line
    fi
}


zle -N tv-search _tv_search


bindkey '^T' tv-search