How do I get bash completion for command aliases?
Clash Royale CLAN TAG#URR8PPP
up vote
41
down vote
favorite
I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :
alias apt-inst='sudo aptitude install'
Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.
bash alias autocomplete
add a comment |Â
up vote
41
down vote
favorite
I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :
alias apt-inst='sudo aptitude install'
Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.
bash alias autocomplete
1
See also stackoverflow.com/questions/342969/â¦
â nschum
Aug 17 '12 at 9:17
add a comment |Â
up vote
41
down vote
favorite
up vote
41
down vote
favorite
I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :
alias apt-inst='sudo aptitude install'
Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.
bash alias autocomplete
I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :
alias apt-inst='sudo aptitude install'
Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.
bash alias autocomplete
bash alias autocomplete
edited Nov 20 '10 at 22:27
Gilles
513k12010171549
513k12010171549
asked Nov 20 '10 at 16:13
levesque
1,16741114
1,16741114
1
See also stackoverflow.com/questions/342969/â¦
â nschum
Aug 17 '12 at 9:17
add a comment |Â
1
See also stackoverflow.com/questions/342969/â¦
â nschum
Aug 17 '12 at 9:17
1
1
See also stackoverflow.com/questions/342969/â¦
â nschum
Aug 17 '12 at 9:17
See also stackoverflow.com/questions/342969/â¦
â nschum
Aug 17 '12 at 9:17
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
26
down vote
accepted
There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:
function make-completion-wrapper ()
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
return 0
"
eval "$function"
echo $function_name
echo "$function"
Use it to define a completion function for your alias, then specify that function as a completer for the alias:
make-completion-wrapper _apt_get _apt_get_install apt-get install
complete -F _apt_get_install apt-inst
I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep
, I always want to skip devices and binary files, so I make an alias for grep
. For adding new commands such as grepbin
, I use a shell script in my ~/bin
folder. If that folder is in your path, it will get autocompleted.
2
Awesome, I was afraid it wouldn't be possible.
â levesque
Nov 20 '10 at 17:10
Note this code has some issues. See my answer for their explanation and resolution.
â Tom Hale
Sep 15 '16 at 13:06
Note: I was using this technique for another command, and I had to usecomplete -o default -F ...
instead ofcomplete -F ...
to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).
â joelittlejohn
Dec 19 '16 at 14:39
add a comment |Â
up vote
15
down vote
Try complete-alias, which solves this problem exactly.
After install it you can use one generic function to complete many aliases like this:
complete -F _complete_alias <myalias1>
complete -F _complete_alias <myalias2>
complete -F _complete_alias <myalias3>
You may want to source the complete_alias file in every bash instance through .bash_profile
or similar.
installation
mkdir ~/.bash_completion.d
curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
> ~/.bash_completion.d/complete_alias
application
source ~/.bash_completion.d/complete_alias
alias container=docker container
complete -F _complete_alias container
container
can now be autocompleted by the original _docker()
completion handler;
$ container l<Tab>
logs ls
$ container s<Tab>
start stats stop
2
Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.
â muru
Dec 24 '16 at 7:55
@muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.
â Cyker
Dec 25 '16 at 12:12
2
the point is to be able to judge whether visiting said README is worthwhile, just from the answer.
â muru
Dec 25 '16 at 12:14
This is an adequate answer and it solves the problem exactly.
â Ã¯ÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
3 hours ago
add a comment |Â
up vote
2
down vote
Here's the code from Shawn J. Goff's answer with some improvements:
- Fixed syntax errors highlighted by
shell-check
, eg the first"
of"$@"
actually ended the function definition string. - Removed the
return 0
so that the return value of the underlying function can be passed back to the caller.
.
# Wraps a completion function, eg for use with an alias.
# Usage:
# make-completion-wrapper <actual completion function> <name of new func.>
# <command name> <list supplied arguments>
# eg.
# alias agi='apt-get install'
# make-completion-wrapper _apt_get _apt_get_install apt-get install
# # defines a function called _apt_get_install (that's $2) that will
# # complete the 'agi' alias.
# complete -F _apt_get_install agi
function make-completion-wrapper
local function_name="$2"
local arg_count=$(( $#-3 ))
local comp_function_name="$1"
shift 2
local function="function $function_name
(( COMP_CWORD += $arg_count ))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
"
eval "$function"
# echo "$function"
export -f make-completion-wrapper
Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.
â joelittlejohn
Dec 19 '16 at 14:38
add a comment |Â
up vote
1
down vote
2018 answer
You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.
For package autocompletion:
complete -c name_of_your_alias
For command autocompletion:
complete -F name_of_your_alias
To check if your alias was added correctly:
complete | grep name_of_your_alias
Finally, to remove an alias from 'complete':
complete -r name_of_your_alias
In your case:
complete -c apt-inst
Please donâÂÂt continue your answer in comments.âÂÂIf you have things to say, edit them into the answer.
â G-Man
May 21 at 3:02
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
26
down vote
accepted
There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:
function make-completion-wrapper ()
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
return 0
"
eval "$function"
echo $function_name
echo "$function"
Use it to define a completion function for your alias, then specify that function as a completer for the alias:
make-completion-wrapper _apt_get _apt_get_install apt-get install
complete -F _apt_get_install apt-inst
I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep
, I always want to skip devices and binary files, so I make an alias for grep
. For adding new commands such as grepbin
, I use a shell script in my ~/bin
folder. If that folder is in your path, it will get autocompleted.
2
Awesome, I was afraid it wouldn't be possible.
â levesque
Nov 20 '10 at 17:10
Note this code has some issues. See my answer for their explanation and resolution.
â Tom Hale
Sep 15 '16 at 13:06
Note: I was using this technique for another command, and I had to usecomplete -o default -F ...
instead ofcomplete -F ...
to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).
â joelittlejohn
Dec 19 '16 at 14:39
add a comment |Â
up vote
26
down vote
accepted
There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:
function make-completion-wrapper ()
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
return 0
"
eval "$function"
echo $function_name
echo "$function"
Use it to define a completion function for your alias, then specify that function as a completer for the alias:
make-completion-wrapper _apt_get _apt_get_install apt-get install
complete -F _apt_get_install apt-inst
I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep
, I always want to skip devices and binary files, so I make an alias for grep
. For adding new commands such as grepbin
, I use a shell script in my ~/bin
folder. If that folder is in your path, it will get autocompleted.
2
Awesome, I was afraid it wouldn't be possible.
â levesque
Nov 20 '10 at 17:10
Note this code has some issues. See my answer for their explanation and resolution.
â Tom Hale
Sep 15 '16 at 13:06
Note: I was using this technique for another command, and I had to usecomplete -o default -F ...
instead ofcomplete -F ...
to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).
â joelittlejohn
Dec 19 '16 at 14:39
add a comment |Â
up vote
26
down vote
accepted
up vote
26
down vote
accepted
There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:
function make-completion-wrapper ()
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
return 0
"
eval "$function"
echo $function_name
echo "$function"
Use it to define a completion function for your alias, then specify that function as a completer for the alias:
make-completion-wrapper _apt_get _apt_get_install apt-get install
complete -F _apt_get_install apt-inst
I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep
, I always want to skip devices and binary files, so I make an alias for grep
. For adding new commands such as grepbin
, I use a shell script in my ~/bin
folder. If that folder is in your path, it will get autocompleted.
There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:
function make-completion-wrapper ()
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
return 0
"
eval "$function"
echo $function_name
echo "$function"
Use it to define a completion function for your alias, then specify that function as a completer for the alias:
make-completion-wrapper _apt_get _apt_get_install apt-get install
complete -F _apt_get_install apt-inst
I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep
, I always want to skip devices and binary files, so I make an alias for grep
. For adding new commands such as grepbin
, I use a shell script in my ~/bin
folder. If that folder is in your path, it will get autocompleted.
edited Nov 28 '12 at 0:16
Gilles
513k12010171549
513k12010171549
answered Nov 20 '10 at 16:48
Shawn J. Goff
28.5k19106132
28.5k19106132
2
Awesome, I was afraid it wouldn't be possible.
â levesque
Nov 20 '10 at 17:10
Note this code has some issues. See my answer for their explanation and resolution.
â Tom Hale
Sep 15 '16 at 13:06
Note: I was using this technique for another command, and I had to usecomplete -o default -F ...
instead ofcomplete -F ...
to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).
â joelittlejohn
Dec 19 '16 at 14:39
add a comment |Â
2
Awesome, I was afraid it wouldn't be possible.
â levesque
Nov 20 '10 at 17:10
Note this code has some issues. See my answer for their explanation and resolution.
â Tom Hale
Sep 15 '16 at 13:06
Note: I was using this technique for another command, and I had to usecomplete -o default -F ...
instead ofcomplete -F ...
to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).
â joelittlejohn
Dec 19 '16 at 14:39
2
2
Awesome, I was afraid it wouldn't be possible.
â levesque
Nov 20 '10 at 17:10
Awesome, I was afraid it wouldn't be possible.
â levesque
Nov 20 '10 at 17:10
Note this code has some issues. See my answer for their explanation and resolution.
â Tom Hale
Sep 15 '16 at 13:06
Note this code has some issues. See my answer for their explanation and resolution.
â Tom Hale
Sep 15 '16 at 13:06
Note: I was using this technique for another command, and I had to use
complete -o default -F ...
instead of complete -F ...
to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).â joelittlejohn
Dec 19 '16 at 14:39
Note: I was using this technique for another command, and I had to use
complete -o default -F ...
instead of complete -F ...
to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).â joelittlejohn
Dec 19 '16 at 14:39
add a comment |Â
up vote
15
down vote
Try complete-alias, which solves this problem exactly.
After install it you can use one generic function to complete many aliases like this:
complete -F _complete_alias <myalias1>
complete -F _complete_alias <myalias2>
complete -F _complete_alias <myalias3>
You may want to source the complete_alias file in every bash instance through .bash_profile
or similar.
installation
mkdir ~/.bash_completion.d
curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
> ~/.bash_completion.d/complete_alias
application
source ~/.bash_completion.d/complete_alias
alias container=docker container
complete -F _complete_alias container
container
can now be autocompleted by the original _docker()
completion handler;
$ container l<Tab>
logs ls
$ container s<Tab>
start stats stop
2
Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.
â muru
Dec 24 '16 at 7:55
@muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.
â Cyker
Dec 25 '16 at 12:12
2
the point is to be able to judge whether visiting said README is worthwhile, just from the answer.
â muru
Dec 25 '16 at 12:14
This is an adequate answer and it solves the problem exactly.
â Ã¯ÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
3 hours ago
add a comment |Â
up vote
15
down vote
Try complete-alias, which solves this problem exactly.
After install it you can use one generic function to complete many aliases like this:
complete -F _complete_alias <myalias1>
complete -F _complete_alias <myalias2>
complete -F _complete_alias <myalias3>
You may want to source the complete_alias file in every bash instance through .bash_profile
or similar.
installation
mkdir ~/.bash_completion.d
curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
> ~/.bash_completion.d/complete_alias
application
source ~/.bash_completion.d/complete_alias
alias container=docker container
complete -F _complete_alias container
container
can now be autocompleted by the original _docker()
completion handler;
$ container l<Tab>
logs ls
$ container s<Tab>
start stats stop
2
Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.
â muru
Dec 24 '16 at 7:55
@muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.
â Cyker
Dec 25 '16 at 12:12
2
the point is to be able to judge whether visiting said README is worthwhile, just from the answer.
â muru
Dec 25 '16 at 12:14
This is an adequate answer and it solves the problem exactly.
â Ã¯ÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
3 hours ago
add a comment |Â
up vote
15
down vote
up vote
15
down vote
Try complete-alias, which solves this problem exactly.
After install it you can use one generic function to complete many aliases like this:
complete -F _complete_alias <myalias1>
complete -F _complete_alias <myalias2>
complete -F _complete_alias <myalias3>
You may want to source the complete_alias file in every bash instance through .bash_profile
or similar.
installation
mkdir ~/.bash_completion.d
curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
> ~/.bash_completion.d/complete_alias
application
source ~/.bash_completion.d/complete_alias
alias container=docker container
complete -F _complete_alias container
container
can now be autocompleted by the original _docker()
completion handler;
$ container l<Tab>
logs ls
$ container s<Tab>
start stats stop
Try complete-alias, which solves this problem exactly.
After install it you can use one generic function to complete many aliases like this:
complete -F _complete_alias <myalias1>
complete -F _complete_alias <myalias2>
complete -F _complete_alias <myalias3>
You may want to source the complete_alias file in every bash instance through .bash_profile
or similar.
installation
mkdir ~/.bash_completion.d
curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
> ~/.bash_completion.d/complete_alias
application
source ~/.bash_completion.d/complete_alias
alias container=docker container
complete -F _complete_alias container
container
can now be autocompleted by the original _docker()
completion handler;
$ container l<Tab>
logs ls
$ container s<Tab>
start stats stop
edited 8 mins ago
ïÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
498312
498312
answered Dec 24 '16 at 7:17
Cyker
1,29411226
1,29411226
2
Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.
â muru
Dec 24 '16 at 7:55
@muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.
â Cyker
Dec 25 '16 at 12:12
2
the point is to be able to judge whether visiting said README is worthwhile, just from the answer.
â muru
Dec 25 '16 at 12:14
This is an adequate answer and it solves the problem exactly.
â Ã¯ÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
3 hours ago
add a comment |Â
2
Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.
â muru
Dec 24 '16 at 7:55
@muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.
â Cyker
Dec 25 '16 at 12:12
2
the point is to be able to judge whether visiting said README is worthwhile, just from the answer.
â muru
Dec 25 '16 at 12:14
This is an adequate answer and it solves the problem exactly.
â Ã¯ÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
3 hours ago
2
2
Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.
â muru
Dec 24 '16 at 7:55
Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.
â muru
Dec 24 '16 at 7:55
@muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.
â Cyker
Dec 25 '16 at 12:12
@muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.
â Cyker
Dec 25 '16 at 12:12
2
2
the point is to be able to judge whether visiting said README is worthwhile, just from the answer.
â muru
Dec 25 '16 at 12:14
the point is to be able to judge whether visiting said README is worthwhile, just from the answer.
â muru
Dec 25 '16 at 12:14
This is an adequate answer and it solves the problem exactly.
â Ã¯ÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
3 hours ago
This is an adequate answer and it solves the problem exactly.
â Ã¯ÃÂþÃÂûðò àðàüðÃÂÃÂûûøý
3 hours ago
add a comment |Â
up vote
2
down vote
Here's the code from Shawn J. Goff's answer with some improvements:
- Fixed syntax errors highlighted by
shell-check
, eg the first"
of"$@"
actually ended the function definition string. - Removed the
return 0
so that the return value of the underlying function can be passed back to the caller.
.
# Wraps a completion function, eg for use with an alias.
# Usage:
# make-completion-wrapper <actual completion function> <name of new func.>
# <command name> <list supplied arguments>
# eg.
# alias agi='apt-get install'
# make-completion-wrapper _apt_get _apt_get_install apt-get install
# # defines a function called _apt_get_install (that's $2) that will
# # complete the 'agi' alias.
# complete -F _apt_get_install agi
function make-completion-wrapper
local function_name="$2"
local arg_count=$(( $#-3 ))
local comp_function_name="$1"
shift 2
local function="function $function_name
(( COMP_CWORD += $arg_count ))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
"
eval "$function"
# echo "$function"
export -f make-completion-wrapper
Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.
â joelittlejohn
Dec 19 '16 at 14:38
add a comment |Â
up vote
2
down vote
Here's the code from Shawn J. Goff's answer with some improvements:
- Fixed syntax errors highlighted by
shell-check
, eg the first"
of"$@"
actually ended the function definition string. - Removed the
return 0
so that the return value of the underlying function can be passed back to the caller.
.
# Wraps a completion function, eg for use with an alias.
# Usage:
# make-completion-wrapper <actual completion function> <name of new func.>
# <command name> <list supplied arguments>
# eg.
# alias agi='apt-get install'
# make-completion-wrapper _apt_get _apt_get_install apt-get install
# # defines a function called _apt_get_install (that's $2) that will
# # complete the 'agi' alias.
# complete -F _apt_get_install agi
function make-completion-wrapper
local function_name="$2"
local arg_count=$(( $#-3 ))
local comp_function_name="$1"
shift 2
local function="function $function_name
(( COMP_CWORD += $arg_count ))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
"
eval "$function"
# echo "$function"
export -f make-completion-wrapper
Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.
â joelittlejohn
Dec 19 '16 at 14:38
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Here's the code from Shawn J. Goff's answer with some improvements:
- Fixed syntax errors highlighted by
shell-check
, eg the first"
of"$@"
actually ended the function definition string. - Removed the
return 0
so that the return value of the underlying function can be passed back to the caller.
.
# Wraps a completion function, eg for use with an alias.
# Usage:
# make-completion-wrapper <actual completion function> <name of new func.>
# <command name> <list supplied arguments>
# eg.
# alias agi='apt-get install'
# make-completion-wrapper _apt_get _apt_get_install apt-get install
# # defines a function called _apt_get_install (that's $2) that will
# # complete the 'agi' alias.
# complete -F _apt_get_install agi
function make-completion-wrapper
local function_name="$2"
local arg_count=$(( $#-3 ))
local comp_function_name="$1"
shift 2
local function="function $function_name
(( COMP_CWORD += $arg_count ))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
"
eval "$function"
# echo "$function"
export -f make-completion-wrapper
Here's the code from Shawn J. Goff's answer with some improvements:
- Fixed syntax errors highlighted by
shell-check
, eg the first"
of"$@"
actually ended the function definition string. - Removed the
return 0
so that the return value of the underlying function can be passed back to the caller.
.
# Wraps a completion function, eg for use with an alias.
# Usage:
# make-completion-wrapper <actual completion function> <name of new func.>
# <command name> <list supplied arguments>
# eg.
# alias agi='apt-get install'
# make-completion-wrapper _apt_get _apt_get_install apt-get install
# # defines a function called _apt_get_install (that's $2) that will
# # complete the 'agi' alias.
# complete -F _apt_get_install agi
function make-completion-wrapper
local function_name="$2"
local arg_count=$(( $#-3 ))
local comp_function_name="$1"
shift 2
local function="function $function_name
(( COMP_CWORD += $arg_count ))
COMP_WORDS=( "$@" $COMP_WORDS[@]:1 )
"$comp_function_name"
"
eval "$function"
# echo "$function"
export -f make-completion-wrapper
edited Apr 13 '17 at 12:37
Communityâ¦
1
1
answered Sep 15 '16 at 13:05
Tom Hale
6,04822777
6,04822777
Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.
â joelittlejohn
Dec 19 '16 at 14:38
add a comment |Â
Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.
â joelittlejohn
Dec 19 '16 at 14:38
Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.
â joelittlejohn
Dec 19 '16 at 14:38
Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.
â joelittlejohn
Dec 19 '16 at 14:38
add a comment |Â
up vote
1
down vote
2018 answer
You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.
For package autocompletion:
complete -c name_of_your_alias
For command autocompletion:
complete -F name_of_your_alias
To check if your alias was added correctly:
complete | grep name_of_your_alias
Finally, to remove an alias from 'complete':
complete -r name_of_your_alias
In your case:
complete -c apt-inst
Please donâÂÂt continue your answer in comments.âÂÂIf you have things to say, edit them into the answer.
â G-Man
May 21 at 3:02
add a comment |Â
up vote
1
down vote
2018 answer
You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.
For package autocompletion:
complete -c name_of_your_alias
For command autocompletion:
complete -F name_of_your_alias
To check if your alias was added correctly:
complete | grep name_of_your_alias
Finally, to remove an alias from 'complete':
complete -r name_of_your_alias
In your case:
complete -c apt-inst
Please donâÂÂt continue your answer in comments.âÂÂIf you have things to say, edit them into the answer.
â G-Man
May 21 at 3:02
add a comment |Â
up vote
1
down vote
up vote
1
down vote
2018 answer
You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.
For package autocompletion:
complete -c name_of_your_alias
For command autocompletion:
complete -F name_of_your_alias
To check if your alias was added correctly:
complete | grep name_of_your_alias
Finally, to remove an alias from 'complete':
complete -r name_of_your_alias
In your case:
complete -c apt-inst
2018 answer
You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.
For package autocompletion:
complete -c name_of_your_alias
For command autocompletion:
complete -F name_of_your_alias
To check if your alias was added correctly:
complete | grep name_of_your_alias
Finally, to remove an alias from 'complete':
complete -r name_of_your_alias
In your case:
complete -c apt-inst
edited May 21 at 2:49
answered May 21 at 2:42
Adrian Lopez
1114
1114
Please donâÂÂt continue your answer in comments.âÂÂIf you have things to say, edit them into the answer.
â G-Man
May 21 at 3:02
add a comment |Â
Please donâÂÂt continue your answer in comments.âÂÂIf you have things to say, edit them into the answer.
â G-Man
May 21 at 3:02
Please donâÂÂt continue your answer in comments.âÂÂIf you have things to say, edit them into the answer.
â G-Man
May 21 at 3:02
Please donâÂÂt continue your answer in comments.âÂÂIf you have things to say, edit them into the answer.
â G-Man
May 21 at 3:02
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f4219%2fhow-do-i-get-bash-completion-for-command-aliases%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
See also stackoverflow.com/questions/342969/â¦
â nschum
Aug 17 '12 at 9:17