Skip to content
This repository has been archived by the owner on Jun 15, 2018. It is now read-only.

Commit

Permalink
Function to print autocompletions
Browse files Browse the repository at this point in the history
  • Loading branch information
l0b0 committed Feb 7, 2012
1 parent c354d40 commit 73f670e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .bash_aliases
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,35 @@ valid_ipv4()

}

completions()
{
# Print autocompletions
# Examples:
#
# List all user-related Git configuration settings:
# completions git config '' | grep user
#
# List all available commands:
# completions
if [ $# -eq 0 ]
then
set -- ''
fi
local COMP_LINE="$*"
local COMP_WORDS=("$@")
local COMP_CWORD=${#COMP_WORDS[@]}
((COMP_CWORD--))
local COMP_POINT=${#COMP_LINE}
local COMP_TYPE=9
local COMP_KEY=9
local COMP_WORDBREAKS='"'"'><=;|&(:"
_command_offset 0
for result in "${COMPREPLY[@]}"
do
echo "$result"
done
}

if [ -r "$HOME/.bash_aliases_local" ]
then
source "$HOME/.bash_aliases_local"
Expand Down
1 change: 1 addition & 0 deletions .bash_history
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ cd ~/personal/
cd ~/settings/
chmod u+x test.sh
cmp -b $(which arch) $(which uname)
completions git config '' | grep user
./configure
./configure --help | less
count .
Expand Down

2 comments on commit 73f670e

@yuyichao
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might also want to local COMPREPLY

@l0b0
Copy link
Owner Author

@l0b0 l0b0 commented on 73f670e Feb 8, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Here's the corresponding shell output:

$ bash
$ ( set -o posix ; set ) | grep -o ^COMP[^=]*
COMP_WORDBREAKS
$ completions '' >/dev/null
[snip]
$ ( set -o posix ; set ) | grep -o ^COMP[^=]*
COMPREPLY
COMP_WORDBREAKS

After adding local COMPREPLY, it prints only

COMP_WORDBREAKS

at the second run. See e7ccc9a.

Please sign in to comment.