Normally when I pass a filename which is the only file there in current directory with auto-completion by Tab to the command come before it then press multiple Tab(mistakenly), the same filename complete multiple times and I should delete or cancel and start again.
Is there any way to disable this behavior (without creating another file nor hidden file)?
ls <Tab>filename <Tab>filename <Tab>filename
Best Answer
One solution is to use the
nospace
option for thecomplete
builtin.First, you need to know how completion is defined for
ls
Then you modify it by adding
-o nospace
Thus, even if you hit Tab ↹ multiple times, there will be no match and no argument will be added to the command line.
Side effect: you need to manually append a space at the end of the line if you want to add another argument to
ls
.To avoid this side effect you could modify the
_longopt
function in a way that prevents it from matching the same argument twice (it might not be straightforward).How to apply this to other commands and to make it permanent
Appending a trailing space after a match is the default behavior. I don't know if there's a way other than using
-o nospace
when specifying completion rules withcomplete
.Moreover, keep in mind, that completion rules are specified somewhere, even for builtin commands (on my laptop in this folder:
/usr/share/bash-completion
). If you look at the file/usr/share/bash-completion/bash-completion
you'll find where completions are defined forls
:You see that the same rules apply for a whole bunch of different commands (such as
cp
,mv
,rm
...), so if you modify this by adding-o nospace
, the changing will affectls
and all this other commands.If you're not comfortable with modifying global settings (you'll need root privileges), you can write them to
~/.bash_completion
(create this file, if not present), like thisIf you wish to modify completions for other commands not listed here, just look up for them in
/usr/share/bash-completion/bash-completion
file or/usr/share/bash-completion/completions
folder.