Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Tab Completion of Filenames after Arguments

I have recently moved from Scientific Linux 6 to CentOS 7 and am having an issue with bash tab completion in the new OS.

Software Versions

$ cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

$ uname -r
3.10.0-229.14.1.el7.x86_64

$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

I have a bash script (executable) named ./run_prog.sh that takes a configuration file on the command line using the -c option (or long --config=).

Example of full command:

./run_prog.sh -c=./config/test-new-feature.conf
## or
./run_prog.sh --config=./config/test-new-feature.conf

In previous versions of bash i was able to tab complete directory and filenames after the -c= construct.

Example of expected tab complete (how it worked in SL6):

./run_prog.sh -c=./conf[TAB]
 ## completes to
./run_prog.sh -c=./config/
 ## then type
./run_prog.sh -c=./config/test-n[TAB]
 ## completes to
./run_prog.sh -c=./config/test-new-feature.conf

The new version of bash in CentOS 7 will not complete any filenames after the -c= short option.

Example of broken tab completion in CentOS 7:

./run_prog.sh -c=./conf[TAB]
 ## doesn't complete anything
./run_prog.sh -c=./conf

However if i separate the -c with a space the filename completion works as expected.

Example of working tab completion with space:

./run_prog.sh -c ./conf[TAB]
 ## completes to
./run_prog.sh -c ./config/
 ## then type
./run_prog.sh -c ./config/test-n[TAB]
 ## completes to
./run_prog.sh -c ./config/test-new-feature.conf

Question

How can i get the new version of bash to tab complete filenames like the old version of bash did?

Edit

This script has an long version for the short -c option that is --config. The long version doesn't work either.

./run_prog.sh --config=./conf[TAB]
 ## doesn't complete anything
./run_prog.sh --config=./conf

This makes me thing that bash is getting confused by the lack of spacing between the option switch (-c) and the option value.

My old bash-completion directory

$ ls -1 /media/old_hd/etc/bash_completion.d/
bzr
createrepo.bash
dkms
fcoeadm
fcoemon
gdbus-bash-completion.sh
git
gvfs-bash-completion.sh
lldpad
lldptool
perf
phoronix-test-suite
pk-completion.bash
rpmdevtools.bash-completion
subversion
yum.bash
yum-utils.bash

My new bash_completion directory

$ ls -1 /etc/bash_completion.d/
createrepo
dkms
fcoeadm
fcoemon
genpkgmetadata.py
git
lldpad
lldptool
mergerepo
mergerepo.py
modifyrepo
modifyrepo.py
redefine_filedir
scl.bash
yum
yummain.py
yum-utils.bash
like image 330
nick Avatar asked Nov 10 '15 14:11

nick


People also ask

How do you add a completion tab in bash?

Bash completion is a bash function that allows you to auto complete commands or arguments by typing partially commands or arguments, then pressing the [Tab] key. This will help you when writing the bash command in terminal.

Does bash have tab completion?

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence. [1] If multiple completions are possible, then [Tab] lists them all. Let's see how it works. Tab completion also works for variables and path names.

Where do bash completion files go?

A. Put them in the completions subdir of $BASH_COMPLETION_USER_DIR (defaults to $XDG_DATA_HOME/bash-completion or ~/. local/share/bash-completion if $XDG_DATA_HOME is not set) to have them loaded automatically on demand when the respective command is being completed.


1 Answers

After googling (and help from friends) i've found the solution!

To fix filename auto completion add the following to your ~/.bashrc file:

complete -D -o default

Reference: Bash completion for path in argument (with equals sign present)

like image 186
nick Avatar answered Sep 22 '22 12:09

nick