15
Autocomplete custom scripts? (discuss.tchncs.de)

As my time with linux, I created a lot of scripts. Some of them have input parameters and sometimes I just forget this parameters.

So I wonder if there is some way to create autocomplete parameters, like i autocomplete a path by pressing the tab key?

For example a script. ./test.sh can be completed with parameter-one, eg. ./test.sh parameter-one or ./test.sh parameter-two. If i type now ./test.sh followed by tab it should add parameter-one if i press tab again it should change to parameter-two.

How can I do that? I'm on bash…

you are viewing a single comment's thread
view the rest of the comments
[-] unlawfulbooger 11 points 1 month ago

Here's an article that does this: https://iridakos.com/programming/2018/03/01/bash-programmable-completion-tutorial

I have done this for one of my own tools ta, which is a function that switches to a tmux session, or creates it if it doesn't exist:

# switch to existing tmux session, or create it.
# overrides workdir if session name is "Work"
function ta() {
        case "$1" in
                Work) workdir="${HOME}/Work/" ;;
                *) workdir="${HOME}" ;;
        esac
        if tmux has-session -t "$@" &>/dev/null; then
                tmux switch-client -t "$@"
        else
                tmux new-session -A -D -d -c "${workdir}" -s "$@"
                tmux switch-client -t "$@"
        fi
}

# complete tmux sessions
# exclude current session from completion
function _ta_completion() {
        command="${1}"
        completing="${2}"
        previous="${3}"
        [[ "${command}" != 'ta' ]] && return
        current_session="$(tmux display-message -p '#S')"
        IFS=$'\a' COMPREPLY=( $(tmux list-sessions -F '#{session_name}' | grep -i "^${completing}" | grep -v "^${current_session}$"| tr '\n' '\a' ) )
}
# enable completion for ta function
complete -F _ta_completion ta

Usage

$ tmux (starts session "0" by default)
$ ta Personal # create session "Personal" because it doesn't exist
$ ta Work # create session "Work" because it doesn't exist
$ ta <tab> 
0 Personal
$ ta P<tab> -> $ta Personal
$ ta <tab>
0 Work
[-] MoLoPoLY@discuss.tchncs.de 2 points 1 month ago

Many thx. This is exactly what I want. Will try that when I'm batch from vacation.

this post was submitted on 07 Mar 2025
15 points (100.0% liked)

Linux

53324 readers
568 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS