Bash completion

5 views
Skip to first unread message

Alexander Obuhovich

unread,
Aug 29, 2012, 7:00:05 AM8/29/12
to in-portal...@googlegroups.com
Did you use shell (e.g. bash) a lot?
Do you like pressing TAB to auto-complete any command you type?

If answer to both questions is yes, then read following article http://www.debian-administration.org/article/An_introduction_to_bash_completion_part_1, which explains magical ways how to make auto-complete work for you custom commands.

--
Best Regards,

http://www.in-portal.com
http://www.alex-time.com

Dmitry A.

unread,
Sep 4, 2012, 5:18:12 PM9/4/12
to in-portal...@googlegroups.com
We should do one for our do_update project to save time with guessing project name.

DA

Alexander Obuhovich

unread,
Sep 5, 2012, 3:16:14 AM9/5/12
to in-portal...@googlegroups.com
Sure, but that requires knowledge of bash language.

Dmitry A.

unread,
Dec 24, 2012, 1:30:24 AM12/24/12
to in-portal...@googlegroups.com
If I am not mistaken this is already done?

By the way, I want to do this for my SSHA script which should auto-complete users same as SU - does. 

Can you help with this?

DA

Alexander Obuhovich

unread,
Dec 24, 2012, 6:50:26 AM12/24/12
to in-portal...@googlegroups.com
Yes, this was done. Just place attached script under /etc/bash_completion.d folder.

You can easily create new auto-completion scripts based on attached one. I'll explain a bit how it works:
  • at top line function called "_do_update" is declared
  • within that function there is a switch statement for each of parameters (based on position)
  • at the end of the script "complete" command is called with script name (to be auto-completed) and function name, that is responsible for it.
Make sure that you choose unique function name, because it's defined in global scope and is accessible to other completion scripts as well.
bash_completion_do_update

Dmitry A.

unread,
Dec 26, 2012, 11:48:20 PM12/26/12
to in-portal...@googlegroups.com
Alex,

I tried changing a bit your script to for myself so it lists users from current system, but can't get it work. Can you help? 


_ssha() {
local cur opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=""

case "${COMP_CWORD}" in
1)
COMPREPLY=( $( compgen -u -- "$cur" ) )
  ;;
esac
}
complete -F _ssha ssha


DA

Dmitry A.

unread,
Dec 27, 2012, 12:46:42 AM12/27/12
to in-portal...@googlegroups.com
Actually it worked :)

DA

Alexander Obuhovich

unread,
Dec 27, 2012, 2:48:25 AM12/27/12
to in-portal...@googlegroups.com
I bet you needed to re-login for bash to re-read auto-completion scripts.
Reply all
Reply to author
Forward
0 new messages