Projects / bash programmable completion

bash programmable completion

Since v2.04, bash has allowed you to intelligently program and extend its standard completion behavior to achieve complex command lines with just a few keystrokes. Imagine typing ssh [Tab] and being able to complete on hosts from your ~/.ssh/known_hosts files. Or typing man 3 str [Tab] and getting a list of all string handling functions in the UNIX manual. mount system: [Tab] would complete on all exported file-systems from the host called system, while make [Tab] would complete on all targets in Makefile. This project was conceived to produce programmable completion routines for the most common Linux/UNIX commands, reducing the amount of typing sysadmins and programmers need to do on a daily basis.

Tags Systems Administration Shells Utilities
Licenses GPL
Operating Systems OS Independent
Implementation Unix Shell

Tweet this project Short link

Rss Recent releases

Changes: Many new completion functions have been added, and many existing ones have been improved or optimised. In addition, innumerable bugs have been fixed, including important bash 3.1 compatibility issues. This release is believed to be bash 3.1 compliant.

Changes: The disabling of glob expansion in _filedir() has itself been disabled, as this triggered a bug in bash, which had the annoying effect of temporarily disabling alias expansion. mc completion has been greatly extended. A bug in lilo completion has been fixed. Improvements have been made to iwconfig completion. tcpdump and dhclient completion have been fixed to use the correct interfaces function. MPlayer options should now use dashes, not underscores.

Changes: The patterns for tarball matching were fixed. Evince completion for .pdf files was added. More OpenOffice 2 completions were added. Completion for xine front-ends and kplayer/MPlayer was added.

Changes: New completion has been added for rpm2cpio, ntpdate, getent, id, and cpio. Mutt completion now also works for muttng. A bug was fixed in the _filedir() function, so that literal filenames that appear to be glob patterns are not treated as such. scp completion was fixed for the case where a filename contains shell metacharacters. Broken sudo completion was fixed. More extensions for MPlayer were added. The new open document formats of OpenOffice 2.0 are now supported. Many other minor fixes and enhancements were made.

  • Rrelease-mid
  •  21 Jan 2005 16:14
  • Rrelease-after

Changes: Command metacompletion was slightly broken in the previous release, so that certain completions were not properly returned.

Rss Recent comments

Rcomment-before 26 Jan 2007 09:24 Rcomment-trans remberson Rcomment-after

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

}

Rcomment-before 26 Jan 2007 09:21 Rcomment-trans remberson Rcomment-after

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

Rcomment-before 30 Dec 2006 01:32 Rcomment-trans toolguy Rcomment-after

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)

Rcomment-before 22 Dec 2006 23:24 Rcomment-trans toolguy Rcomment-after

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)

Rcomment-before 22 Dec 2006 03:46 Rcomment-trans toolguy Rcomment-after

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,

Ad9cbe36e08562fbc287a7f7fdac7b2b_thumb

Project Spotlight

SLiteChat

An in-world SL text-only local chat/instant messaging client.

C0dc043e0e80eadced3af3bf103e08ba_thumb

Project Spotlight

Camera Life

A photo gallery system.