Showing only the substrings of COMPREPLY bash completion options to the user

In a bash completion script, suppose COMPREPLY=(aa/ba/ aa/bb/). When the script is invoked, the completion options looks like this to the user:

$ foo aa/b<TAB> aa/ba/ aa/bb/

However, I want to have a bit more control over how these options are displayed. In particular, I want to show only a substring of each COMPREPLY option to the user, similar to how directory completion works in bash now:

$ foo aa/b<TAB> ba/ bb/

Is there any way of doing this in bash?

-------------Problems Reply------------

I was having the same problem and I fixed it by adjusting how I bound the completion function to the command. I know this works when you are dealing with actual files in the filesystem, I think it will work with any sort of file path like options, but I'm not sure.


complete -F _fubar fubar


complete -o filenames -F _fubar fubar

For more details: Programmable Completion Builtins

This piece of code taken from debian sid /etc/bash_completion should help:

# Remove colon-word prefix from COMPREPLY items
local colon_word=${1%${1##*:}}
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do

Category:bash Views:0 Time:2010-10-17

Related post

  • Properly handling spaces and quotes in bash completion 2009-07-17

    What is the correct/best way of handling spaces and quotes in bash completion? Here’s a simple example. I have a command called words (e.g., a dictionary lookup program) that takes various words as arguments. The supported ‘words’ may actually contai

  • Bash Completion: What can we do with it, what lies in the future 2009-12-13

    Bash lets you complete commands names and names of files in the arguments with the TAB key. But why not also common options to commands? Why not, even better, a completion system that tells you what an option does, too? I heard of programmable comple

  • Bash completion for Maven escapes colon 2010-05-10

    I added bash completion for Maven following the docs: Everything works well except for goals that use a colon. For instance, instead of mvn eclipse:eclipse completion escapes the colon

  • A confusion about ${array[*]} versus ${array[@]} in the context of a Bash completion 2010-07-27

    I'm taking a stab at writing a Bash completion for the first time, and I'm a bit confused about about the two ways of dereferencing Bash arrays (${array[@]} and ${array[*]}). Here's the relevant chunk of code (it works, by the way, but I would like t

  • Accesssing bash completions for specific commands programmatically 2010-08-19

    I'm trying to write a small command launcher application, and would like to use bash's tab completions in my own completion system. I've been able to get a list of completions for general commands using compgen -abck. However, I would also like to ge

  • Multi Level Bash Completion 2011-03-14

    I currently have a Bash completion file which completes a single parameter from a list of allowed commands for a script (called "pbt"). This is the working Bash Completion file: _pbt_complete() { local cur goals COMPREPLY=() cur=${COMP_WORDS[COMP_CWO

  • How to cache a variable used inside a Bash Completion Script for the current session 2011-03-16

    Inside my Bash Completion file, i'm looking up completion-strings by an external script, which takes some time (1-2 seconds). Since these strings mostly stay the same for the rest of the time the current shell runs, i want to cache them and when the

  • Bash completion programming , '@' is always removed in completion string 2011-03-27

    I'm doing a bash completion for Gentoo , which search for packages , sets ( portage 2.2 new feature ) , but i met a problem when i'm using the following command: emerge @pres And i'm supposed to get @preserved-libs , but bash turns it into: emerge pr

  • Preserve Bash Completion for all commands that are prepended by a custom command 2011-04-21

    I have a script which allows to execute Bash processes in the background, i called it "backy". Programs I want to run in background I call like this: backy long-running-script param1 param2 The problem is now that I loose the Bash completion for long

  • Bash completion doesn't work for MSYS bash. Regex syntax error 2012-02-02

    I tried to install bash-completion for MSYS bash, but it seems it contains some syntax errors. It fails with the following message bash: /usr/local/share/bash-completion/bash_completion: line 625: syntax error in conditional expression: unexpected to

  • Unit Test for Bash completion script 2012-02-04

    I would like to write a Unit Test for a (rather complex) Bash completion script, preferrably with Python - just something that gets the values of a Bash completion programmatically. The test should look like this: def test_completion(): # trigger_com

  • bash completion _parse_help broken? 2012-02-27

    In order to get easy bash completion for my scripts' command line switches, I have been exploring the _parse_help function in /etc/bash_completion (debian sid, bash v4.2-1, bash-completion v1:1.99-3). My script generates a help message in what I beli

  • Bash completion for make with generic targets in a Makefile 2009-02-05

    I have a Makefile where most of my targets are created generically through a canned sequence. It seems that bash completion only suggests completions for normal targets, e.g. target_name: #[email protected] and not for generic targets. Is there any way to make bash

  • Writing a custom bash-completion rule 2010-07-13

    I have directories full of files with the same prefix, which I want to be able to quickly open in vim. For example, I might have: $ ls * bar: bar_10 bar_20 bar_30 foo: foo_10 foo_20 foo_30 What I want is to be able to be in one of these directories a

  • ssh bash completion file 2010-08-28

    for git I use this bash completion file you know a good bash ssh completion file? // or other goods bash completion files ? --------------Solutions------------- The b

  • bash completion of makefile target 2010-11-15

    Suppose I have a simple makefile like: hello: echo "hello world" bye: echo "bye bye" Then in bash I want something like: make h < tab > so it can complete to make hello I found a simple way like creating empty files hello and bye but I'm lookin

  • Install Bash completion together with distutils / pip 2012-03-30

    I have created a simple Python module and want to distribute it with pip. I also want to install a Bash completion file together with the module. I'm installing the module with Python 2.7.1+ and pip 0.8.2. I have this setup( name='jenkinsmo

  • How do I modify cygwin's PS1 for git bash completion? 2014-01-15

    Here is my current PS1: $ echo $PS1 \[\e]0;\w\a\]\n\[\e[32m\]\[email protected]\h \[\e[33m\]\w\[\e[0m\]\n\$ I have installed git bash completion and it isn't showing the current branch in my command prompt. I think this needs to be edited but I've got no idea what

  • Taming XCode's auto-complete options 2010-04-07

    I am fairly new to XCode and the Objective-C language. When I am instantiating a class, for example an NSMutableArray, XCode will provide a whole lot of auto-complete options. Even for an empty class which simply extends an NSObject has many options,

Copyright (C), All Rights Reserved.

processed in 0.159 (s). 11 q(s)