0% found this document useful (0 votes)
24 views

Creating A Bash Completion Script

The document discusses how to create bash completion scripts to enable tab autocompletion for custom commands. It provides examples of using the complete builtin to define completions based on word lists, directories, and command history. The examples demonstrate how to retrieve and filter the list of possible completions using commands like compgen and fc, and populate the COMPREPLY array based on the current command line context accessed via variables like COMP_WORDS and COMP_CWORD. Defining a completion function and sourcing it enables tab completion for the custom command.

Uploaded by

mirandowebs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Creating A Bash Completion Script

The document discusses how to create bash completion scripts to enable tab autocompletion for custom commands. It provides examples of using the complete builtin to define completions based on word lists, directories, and command history. The examples demonstrate how to retrieve and filter the list of possible completions using commands like compgen and fc, and populate the COMPREPLY array based on the current command line context accessed via variables like COMP_WORDS and COMP_CWORD. Defining a completion function and sourcing it enables tab completion for the custom command.

Uploaded by

mirandowebs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

tab

$ git<tab><tab>
git git-receive-pack git-upload-archive
gitk git-shell git-upload-pack
$ git-s<tab>
$ git-shell

complete

1 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

dothis
ls -a
235

dothis 235

dothis

$ dothis <tab><tab>
215 ls
216 ls -la
217 cd ~
218 man history
219 git status
220 history | cut -c 8-

dothis

if [ -z "$1" ]; then
echo "No command number passed"
exit 2
fi

exists=$(fc -l -1000 | grep ^$1 -- 2>/dev/null)

if [ -n "$exists" ]; then
fc -s -- "$1"
else
echo "Command with number $1 was not found in recent history"
exit 2
fi

2 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

fc

chmod +x ./dothis

dothis

install ./dothis ~/bin/dothis

~/bin PATH

dothis

$ dothis
No command number passed

dothis-completion.bash

source source

dothis

now
tomorrow
never

complete complete

#/usr/bin/env bash
complete -W "now tomorrow never" dothis

complete

-W
dothis

3 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

source ./dothis-completion.bash

$ dothis <tab><tab>
never now tomorrow

$ dothis n<tab><tab>
never now

-W

dothis

complete -A directory dothis

dothis

$ dothis <tab><tab>
dir1/ dir2/ dir3/

dothis

dothis

#/usr/bin/env bash
_dothis_completions()
{
COMPREPLY+=("now")
COMPREPLY+=("tomorrow")
COMPREPLY+=("never")
}

complete -F _dothis_completions dothis

-F _dothis_completions
dothis
COMPREPLY

4 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

$ dothis <tab><tab>
never now tomorrow

$ dothis nev<tab><tab>
never now tomorrow

COMPREPLY
COMPREPLY

compgen complete -W
-d

$ compgen -W "now tomorrow never"


now
tomorrow
never
$ compgen -W "now tomorrow never" n
now
never
$ compgen -W "now tomorrow never" t
tomorrow

dothis

COMP_WORDS compspec
COMP_CWORD COMP_WORDS

COMP_LINE

dothis COMP_WORDS[1]

#/usr/bin/env bash
_dothis_completions()
{
COMPREPLY=($(compgen -W "now tomorrow never" "${COMP_WORDS[1]}"))
}

complete -F _dothis_completions dothis

$ dothis
never now tomorrow
$ dothis n
never now

5 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

fc -l -n

fc -l -50

sed

#/usr/bin/env bash
_dothis_completions()
{
COMPREPLY=($(compgen -W "$(fc -l -50 | sed 's/\t//')" -- "${COMP_WORDS[1]}"))
}

complete -F _dothis_completions dothis

$ dothis <tab><tab>
632 source dothis-completion.bash 649 source dothis-completion.bash 666 cat ~/.bash_profile
633 clear 650 clear 667 cat ~/.bashrc
634 source dothis-completion.bash 651 source dothis-completion.bash 668 clear
635 source dothis-completion.bash 652 source dothis-completion.bash 669 install ./dothis ~/bin/dothis
636 clear 653 source dothis-completion.bash 670 dothis
637 source dothis-completion.bash 654 clear 671 dothis 6546545646
638 clear 655 dothis 654 672 clear
639 source dothis-completion.bash 656 dothis 631 673 dothis
640 source dothis-completion.bash 657 dothis 150 674 dothis 651
641 source dothis-completion.bash 658 dothis 675 source dothis-completion.bash
642 clear 659 clear 676 dothis 651
643 dothis 623 ls -la 660 dothis 677 dothis 659
644 clear 661 install ./dothis ~/bin/dothis 678 clear
645 source dothis-completion.bash 662 dothis 679 dothis 665
646 clear 663 install ./dothis ~/bin/dothis 680 clear
647 source dothis-completion.bash 664 dothis 681 clear
648 clear 665 cat ~/.bashrc

$ dothis 623<tab>
$ dothis 623 ls 623 ls -la
...
$ dothis 623 ls 623 ls 623 ls 623 ls 623 ls -la

${COMP_WORDS[1]} dothis
623

COMP_WORDS

6 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

#/usr/bin/env bash
_dothis_completions()
{
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi

COMPREPLY=($(compgen -W "$(fc -l -50 | sed 's/\t//')" -- "${COMP_WORDS[1]}"))


}

complete -F _dothis_completions dothis

$ dothis 623<tab>
$ dothis 623 ls -la<tab> # SUCCESS: nothing happens here

dothis

compgen

#/usr/bin/env bash
_dothis_completions()
{
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi

# keep the suggestions in a local variable


local suggestions=($(compgen -W "$(fc -l -50 | sed 's/\t/ /')" -- "${COMP_WORDS[1]}"))

if [ "${#suggestions[@]}" == "1" ]; then


# if there's only one match, we remove the command literal
# to proceed with the automatic completion of the number
local number=$(echo ${suggestions[0]/%\ */})
COMPREPLY=("$number")
else
# more than one suggestions resolved,
# respond with the suggestions intact
COMPREPLY=("${suggestions[@]}")
fi
}

complete -F _dothis_completions dothis

.bashrc

source <path-to-your-script>/dothis-completion.bash

/etc/bash_completion.d/

7 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

\e[34mBlue

COMPREPLY

printf

#/usr/bin/env bash
_dothis_completions()
{
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi

local IFS=$'\n'
local suggestions=($(compgen -W "$(fc -l -50 | sed 's/\t//')" -- "${COMP_WORDS[1]}"))

if [ "${#suggestions[@]}" == "1" ]; then


local number="${suggestions[0]/%\ */}"
COMPREPLY=("$number")
else
for i in "${!suggestions[@]}"; do
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")"
done

COMPREPLY=("${suggestions[@]}")
fi
}

complete -F _dothis_completions dothis

dothis <tab><tab>
...
499 source dothis-completion.bash
500 clear
...
503 dothis 500

DOTHIS_COMPLETION_COMMANDS_NUMBER

8 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

#/usr/bin/env bash
_dothis_completions()
{
if [ "${#COMP_WORDS[@]}" != "2" ]; then
return
fi

local commands_number=${DOTHIS_COMPLETION_COMMANDS_NUMBER:-50}
local IFS=$'\n'
local suggestions=($(compgen -W "$(fc -l -$commands_number | sed 's/\t//')" -- "${COMP_WORDS[1]}"))

if [ "${#suggestions[@]}" == "1" ]; then


local number="${suggestions[0]/%\ */}"
COMPREPLY=("$number")
else
for i in "${!suggestions[@]}"; do
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")"
done

COMPREPLY=("${suggestions[@]}")
fi
}

complete -F _dothis_completions dothis

export DOTHIS_COMPLETION_COMMANDS_NUMBER=5
$ dothis <tab><tab>
505 clear
506 source ./dothis-completion.bash
507 dothis clear
508 clear
509 export DOTHIS_COMPLETION_COMMANDS_NUMBER=5

goto

9 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

10 de 11 05/04/2021 20:33
Creating a bash completion script https://iridakos.com/programming/2018/03/01/bash-programmable-co...

11 de 11 05/04/2021 20:33

You might also like