If I have several directories, like:
afoo abarsometimes my terminal will refuse autocomplete when I press tab (e.g. "cd a" then tab), and print the list of directories instead. Sometimes it even throws a noisy, annoying sound. Any idea how to make it autocomplete in cases like this? E.g it can show abar first, and then afoo if I press tab again. I saw this is the case in windows, or some applciation in Ubuntu
3 Answers
Something that is a life-saver for me is to have bash cycle through the possibilities instead of showing a dumb list.
As bash is using readline for its auto-completion, add the following lines to ~/.inputrc
Once you're satisfied and have thoroughly tested below solution for a few days/weeks, cut and paste (don't copy!) the same settings from ~/.inputrc to /etc/inputrc which contains the system-wide settings, making this available to all users on your system (including guest).
The codez:
# mappings to have up and down arrow searching through history:
"\e[A": history-search-backward
"\e[B": history-search-forward
# mappings to have left and right arrow go left and right:
"\e[C": forward-char
"\e[D": backward-char
# mapping to have [Tab] and [Shift]+[Tab] to cycle through all the possible completions:
"\t": menu-complete
"\e[Z": menu-complete-backwardthen exit your terminal (or remote terminal like putty) and open it again...
Examples:
When you have 3 files:
file1,file2andfile3and you type:e fTabTabTabit'll cycle through:
e file1 e file2 e file3and when you want to cycle backwards, just hit Shift+Tab
When you type:
very-complicated-command with lots of command line parametersand next time you need the same command, you just type:
very↑and it'll type for you:
very-complicated-command with lots of command line parameters
This will save you a ton of time in bash! ;-)
6After the 1st tab you need to insert more letters. So if you type
cd aand hit tab you get nothing and after a second tab (immediately following) you get a list of the names starting with a and then need to insert an f to have it auto complete the remainder so
cd atabtabftabtab
will result in
cd afoo 2 To do it in Bash add the fllowing to your bash file:
# make tab cycle through commands after listing
bind '"\t":menu-complete'
bind "set show-all-if-ambiguous on"
bind "set completion-ignore-case on"
bind "set menu-complete-display-prefix on"Works nicely. (Taken from here as mentioned in a comment by @muru).