Bash – Ignore Auto-Completion on Single Same Filename

autocompletebash

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 the complete builtin.

First, you need to know how completion is defined for ls

$ complete -p ls
complete -F _longopt ls

Then you modify it by adding -o nospace

$ complete -o nospace -F _longopt ls

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 with complete.

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 for ls:

complete -F _longopt a2ps awk base64 bash bc bison cat chroot colordiff cp \
  csplit cut date df diff dir du enscript env expand fmt fold gperf \
  grep grub head irb ld ldd less ln ls m4 md5sum mkdir mkfifo mknod \
  mv netstat nl nm objcopy objdump od paste pr ptx readelf rm rmdir \
  sed seq sha{,1,224,256,384,512}sum shar sort split strip sum tac tail tee \
  texindex touch tr uname unexpand uniq units vdir wc who

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 affect ls 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 this

complete -F _longopt -o nospace a2ps awk base64 bash bc bison cat chroot colordiff cp \
  csplit cut date df diff dir du enscript env expand fmt fold gperf \
  grep grub head irb ld ldd less ln ls m4 md5sum mkdir mkfifo mknod \
  mv netstat nl nm objcopy objdump od paste pr ptx readelf rm rmdir \
  sed seq sha{,1,224,256,384,512}sum shar sort split strip sum tac tail tee \
  texindex touch tr uname unexpand uniq units vdir wc who

If 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.

Related Question