Comments for bash programmable completion

12 Nov 2013 07:49 ranr

where to download from?

19 May 2010 04:04 joodas

Hi, you might wanna have a look at ClearCase completion script. http://jan.tomka.name/project/clearcase-completion-bash

26 Jan 2007 09:24 remberson

svn mod: listing targets of imported build files
in bash_completion _ant()

change:

local cur prev buildfile i

to:

local cur prev buildfile imports i f

add:

imports=(

$( awk -F'"' '/<import file="/ {print $2}' \

$buildfile )

$( awk -F"'" "/<target name='/ "'{print $2}' \

$buildfile )

)

for f in ${imports[@]}; do

if [[ -e $f ]]; then

COMPREPLY=( ${COMPREPLY[@]} \

$( awk -F'"' '/<target name="/ {print $2}' \

$f | grep "^$cur" | grep -v '^-' )

$( awk -F"'" "/<target name='/ "'{print $2}' \

$f | grep "^$cur" | grep -v '^-' )

$( awk -F'"' '/<target [^n]/ {if ($1 ~ /name=/) { print $2 } else if ($3 ~ /name=/) {print $4} else if ($5 ~ /name=/) {print $6}}' \

$f | grep "^$cur" | grep -v '^-' ) )

fi

done

change:

have complete-ant-cmd.pl && \

complete -C complete-ant-cmd.pl -F _ant $filenames ant || \

complete -F _ant $filenames ant

}

to:

have complete-ant-cmd.pl && \

complete -C complete-ant-cmd.pl -F _ant $filenames ant || \

complete -F _ant ant

}

26 Jan 2007 09:21 remberson

cd mod allowing ignore directories
Allows one not to have to deal with '.svn' and

'CVS' directories.

in .bashrc

cdignore=( CVS .svn )

in bash_completion _cd() add:

if [ -z "${CDPATH:-}" ] || [[ "$cur" == ?(.)?(.)/* ]]; then

_filedir -d

if [[ -n "$cdignore" ]]; then

k=0

a=()

for f in ${COMPREPLY[@]}; do

i=0

for v in ${cdignore[@]}; do

if [[ "$f" == "$v" ]]; then

i=1

fi

done

if [[ $i -eq 0 ]]; then

a[k++]=$f

fi

done

COMPREPLY=( ${a[@]} )

fi

return 0

fi

30 Dec 2006 01:32 toolguy

Re: Example of broken completion (mixing env variable and path)
A bit more work on it to get rid of the redunant loop yields:

%_cd()
%{
> local IFS=$'\t\n' cur=${COMP_WORDS[COMP_CWORD]} i j k
%
%# (start of changes)
> if [[ "$cur" == ?(\\)\$* ]]; then
> local v vars
> vars=($(for v in $(compgen -v -X !${cur#?(\\)$}*); do if [ -d "$(eval echo \$$v)" ]; then echo \$$v; fi; done))
> if (( ${#vars[*]} == 1 )); then
> # A single completion. Expand it.
> COMPREPLY=($(eval echo $vars))
> return 0
> fi
> # No single completion. Then just list the variables being directories.
> COMPREPLY=(${vars[*]});
> return 0
> fi
>
%# old stuff ...
%# # try to allow variable completion
%# if [[ "$cur" == ?(\\)\$* ]]; then
%# COMPREPLY=( $( compgen -v -P '$' -- "${cur#?(\\)$}" ) )
%# return 0
%# fi
%# (unchanged from here)

22 Dec 2006 23:24 toolguy

Re: Example of broken completion (mixing env variable and path)
Working a bit more on it, the follwoing code does the same more elegantly:

_cd()

{

local IFS=$'\t\n' cur=${COMP_WORDS[COMP_CWORD]} i j k

# Start of modifications

if [[ "$cur" == ?(\\)\$* ]]; then

local v vars=$(compgen -v -X !${cur#?(\\)$}*)

COMPREPLY=(`for v in $vars; do if [ -d "$(eval echo \\$$v)" ]; then eval echo \\$$v; fi; done`)

if (( ${#COMPREPLY[*]} == 1 )); then

return 0

fi

# No single completion. Then just list the variables being directories.

COMPREPLY=(`for v in $vars; do if [ -d "$(eval echo \\$$v)" ]; then echo \\$$v; fi; done`);

return 0

fi

# old stuff removed

# # try to allow variable completion

# if [[ "$cur" == ?(\\)\$* ]]; then

# COMPREPLY=( $( compgen -v -P '$' -- "${cur#?(\\)$}" ) )

# return 0

# fi

#

# (unchanged from here)

22 Dec 2006 03:46 toolguy

Re: Example of broken completion (mixing env variable and path)
Having experienced the same problem, I hacked the _cd function in bash completion:

cd()

{

local IFS=$'\t\n' cur=${COMP_WORDS[COMP_CWORD]} i j k

# Hack start

if [[ "$cur" == ?(\\)\$* ]]; then

local v cur1=${cur#?(\\)$}

# "set -o posix" makes "set" only show variables, not functions

local envs=`set -o posix; for v in $(set | /bin/sed 's/^\(.*\)=.*/\1/g' | /bin/grep "^$cur1"); do if [ -d "$(eval echo \\$$v)" ]; then eval echo \\$$v; fi; done`

COMPREPLY=($( compgen -W "$envs" ));

if (( ${#COMPREPLY[*]} == 1 )); then

return 0

fi

fi

# Hack end

# try to allow variable completion

if [[ "$cur" == ?(\\)\$* ]]; then

COMPREPLY=( $( compgen -v -P '$' -- "${cur#?(\\)$}" ) )

return 0

fi

(... rest as before)

While this certainly isn't perfect, it will complete unique variables. I know the code is horrible, and anyone with more insight in using "compgen", "sed" etc. are hereby encouraged to make a better solution.

> WIth a traditional bash, it often

> happens that I type :

> cd $PWD/ [and press TAB]

> so that it completes into

> cd /my/current/path

> and then I quit the shell.

>

> Now the directory where I left the shell

> is in the history.

> When starting a new shell, a Ctrl-R cd

> (or even up arrow)

> brings me back to the dir I was working

> before. I can open

> many terminals and jump right into the

> without editing any

> cdpath or the like. This is standard

> bash behavior.

>

> With bash_completion activated,

> cd $PWD/ [TAB]

> "fails" (it flashed and does

> nothing),

> so the happy behaviour used for years is

> broken.

>

> Any idea how to modify bash_completion

> so that this

> appreciated behaviour still works ?

> Thanks.

>

> Regards,

23 Jun 2006 04:21 MindBoosterNoori

xine completion
Hi there,


I did a small file for xine completion, that can be downloaded here (http://talkerspt.no-ip.org/~mbooster/software/xine). If you're interested, it would be cool if you could put that file on the contrib directory of bash-completion...


Best regards.

04 Jan 2006 14:31 ahalsey

Very useful idea, but am experiencing too many difficulties
First off, I want to thank the author for developing this extension to bash. When it works, it really makes working at the command line much more pleasant and enhances productivity.

I am using bash 3.00.16 and bash-completion version 20050721.

It mostly works for me, but I am encountered some difficulties and decided to uninstall it.

1. The yum completion didn't know of the new 'localinstall' command token. To get past this I manually typed 'localinstall'. But then when I pressed TAB to do a filename completion on the .rpm file I was trying to install, the completion choices where still offering yum's sub-command tokens instead of filename completions. How do I inform bash-completion about the new 'localinstall' token?

2. The Yum situation became worse when I tried to use it with sudo.

3. Glob expansion when pressing TAB stopped working. That is, I used to be able to type:

$ ls *.c<TAB>

and this would change to:

$ ls myfile.c

This useful behavior returned when I uninstalled bash-completion.

4. It seems under-documented. Documentation at website didn't help me figure out where the yum completions where coming from. I don't know if this is a yum bug or a bash-completion bug. Is there a man page for it?

5. I would urge the author to move the project to a site that allowed better collaboration and bug tracking.

Allen Halsey

04 Aug 2005 13:25 jes5199

apachectl
i found that i've been using the apachectl command pretty often, and it's possible commands are real simple: start, stop, restart, etc

well, this is perfect for complete -W , but i'm lazy, so i wrote a regexp to extract the command list from the help screen (using sed, it could probably be done entirely in bash, but i think comfortably in pipes and filters rather than bash-syntax functions)

complete -W "`apachectl help | sed -n 's/^\([a-zA-Z1-9_]\{1,\}\)[ ].*/\1/p'`" apachectl

Screenshot

Project Spotlight

ReciJournal

An open, cross-platform journaling program.

Screenshot

Project Spotlight

Veusz

A scientific plotting package.