You are not logged in.

#1 2013-05-16 01:29:03

JohnnyDeacon
Member
From: Colombia
Registered: 2012-01-18
Posts: 81

Useful tab-completion for .bashrc

I was searching for a tab completion in systemd commands such as:

$ systemct <TAB> (this show all possible parameters/flags) and another <TAB> (show all units and itself commands).

Reading posts here and there I found that the following command in .bashrc:

complete -F or -C command

did the trick, but this is awful and painfull  cause I need to implement it in all commands, such as, pacman, man, sudo, etc. Ok, googling, I found this usefull script you can put in your .bashrc:

# completion.bash

_vault_complete() {
  COMPREPLY=()
  local word="${COMP_WORDS[COMP_CWORD]}"
  local completions="$(vault --cmplt "$word")"
  COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}

complete -f -F _vault_complete vault

This will complete all your commands with infinite posibilities. (This example use command vault, you can use whatever you want)

I put the link (well explained) here because there is a way to do it in zshell for those interested on it.

http://goo.gl/RFwgK

I hope this can be help someone.

Cheers

Maybe would be useful to put this script on the bashrc wiki. smile

Last edited by JohnnyDeacon (2013-05-21 13:21:21)

Offline

#2 2013-05-16 01:44:05

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: Useful tab-completion for .bashrc

I realize this is not your script, but I am going to move this to community contributions where others are more likely to look for a script as such.

Last edited by ewaller (2013-05-16 02:00:02)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2013-05-16 01:53:45

JohnnyDeacon
Member
From: Colombia
Registered: 2012-01-18
Posts: 81

Re: Useful tab-completion for .bashrc

Of course this is not my script but I found it very useful. wink

Offline

#4 2013-05-16 02:01:07

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: Useful tab-completion for .bashrc

As to the Wiki:  Heck, it is a wiki.... have at it.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2013-05-16 02:43:59

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Useful tab-completion for .bashrc

Just wondering if you actually tested this first...from what I read on the link you provided, this script fragment provided completion for the 'vault' command only if I'm understanding it correctly. In order to make this work for any command, you would have to repeat that shell fragment for _each_ command. Not sure what this gets you over just using 'complete -cf <cmd>'.

I think the author was just explaining how to create a bash completion script for an arbitrary command that could be included along with that command. Most software packages have those bash completion scripts already included. This could be used for a command that doesn't include that script already. If that's what you intended with your post it wasn't clear to me :-)

Scott

Edit:  I also just noticed that without even having 'complete -cf systemctl' in my .bashrc, bash is still giving me a list of possible systemctl options when I hit <TAB> after typing systemctl. Ooo, nice! Never noticed that before smile

Last edited by firecat53 (2013-05-16 02:50:44)

Offline

#6 2013-05-16 02:59:29

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Useful tab-completion for .bashrc

Yes, Scott is right. If you have bash_completion installed, you can look through /usr/share/bash-completion/completions to see how these hooks are written for all of the different commands.

There is, alas, no silver bullet...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2013-05-16 03:43:09

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Useful tab-completion for .bashrc

<offtopic>
Ahh, I know why I haven't noticed the systemctl completions before...because they don't work after 'sudo systemctl'. Only after 'systemctl'. Grrrr. Any idea how to fix that?

Scott
</offtopic>

Offline

#8 2013-05-16 04:05:03

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Useful tab-completion for .bashrc

??[Veles ~]
??? sudo systemctl list-
list-dependencies  list-jobs          list-unit-files    list-units

Works in bash for me...

??[Shiv ~ ]
??? sudo systemctl list-
list-jobs        -- List jobs
list-unit-files  -- List installed unit files
list-units       -- List units

Works better in zsh smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#9 2013-05-16 04:32:08

bohoomil
Member
Registered: 2010-09-04
Posts: 2,376
Website

Re: Useful tab-completion for .bashrc

firecat53 wrote:

because they don't work after 'sudo systemctl'.

There's a chance you have

complete -cf sudo

in your .bashrc. It's redundant because the same is already done in /usr/share/bash-completion/bash-completion, which is sourced via /etc/bash.bashrc. Comment the line out and try again.


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#10 2013-05-16 06:01:19

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Useful tab-completion for .bashrc

jasonwryan wrote:

Works better in zsh smile

Yes, it is the completion that really was what made me start using zsh.  Though there are countless amazing things about zsh, the completion is nothing short of amazing compared to bash.

Offline

#11 2013-05-16 17:03:03

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Useful tab-completion for .bashrc

bohoomil wrote:

There's a chance you have

complete -cf sudo

in your .bashrc. It's redundant because the same is already done in /usr/share/bash-completion/bash-completion, which is sourced via /etc/bash.bashrc. Comment the line out and try again.

Ahh, thanks!

I've just been avoiding zsh because I have a couple of remote shared hosting servers I interact with fairly regularly and I'm afraid of getting confused or frustrated tongue

Scott

Offline

#12 2013-05-21 13:26:46

JohnnyDeacon
Member
From: Colombia
Registered: 2012-01-18
Posts: 81

Re: Useful tab-completion for .bashrc

firecat53 wrote:

Just wondering if you actually tested this first...from what I read on the link you provided, this script fragment provided completion for the 'vault' command only if I'm understanding it correctly. In order to make this work for any command, you would have to repeat that shell fragment for _each_ command. Not sure what this gets you over just using 'complete -cf <cmd>'.

I think the author was just explaining how to create a bash completion script for an arbitrary command that could be included along with that command. Most software packages have those bash completion scripts already included. This could be used for a command that doesn't include that script already. If that's what you intended with your post it wasn't clear to me :-)

Scott

Edit:  I also just noticed that without even having 'complete -cf systemctl' in my .bashrc, bash is still giving me a list of possible systemctl options when I hit <TAB> after typing systemctl. Ooo, nice! Never noticed that before smile

complete -cf <cmd> does not complete with flags and parameters.

I have noticed about a script called bash-completion on repos that do completion very well, I'll check it out if it works with all commands.

Offline

#13 2013-05-21 14:36:15

GermainZ
Member
Registered: 2013-05-08
Posts: 11

Re: Useful tab-completion for .bashrc

Unless I misunderstood something, getting bash-completion (it's in the official repo) does this for all commands.

Offline

Board footer

Powered by FluxBB