5

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
  • Favourited and upvoted. I'd be interested if some of the wizards much more powerful than me here can come up with some magic! ;-) – Fabby Sep 16 '18 at 15:35
  • Same here. I'm sitting on the same bench as you @Fabby. This is actually something I deal with several times everyday ! – Cbhihe Sep 17 '18 at 7:41
3

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.

  • I heard you, thank you for the answer. I tested it's working perfectly, what I forgot to check does it will complete on a file containing whitespaces? and is there a way that I can do this globally for auto-completion instead of modifying every commands one by one? – αғsнιη Oct 9 '18 at 18:18
  • Yes, it will complete on a file containing whitespaces, as the default completion for ls does. The nospace option only tells Readline not to append a space after the match. Later, I will edit the answer to address your other question. – francescop21 Oct 9 '18 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.