44

If I have several directories, like:

afoo abar

sometimes 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

muru
  • 193,181
  • 53
  • 473
  • 722
TifatulS
  • 1,265
  • 3
  • 12
  • 13

4 Answers4

61

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-backward

then exit your terminal (or remote terminal like putty) and open it again...

Examples:

  1. When you have 3 files: file1, file2 and file3 and you type:

    e fTabTabTab

    it'll cycle through:

    e file1
    e file2
    e file3
    

    and when you want to cycle backwards, just hit Shift+Tab

  2. When you type:

    very-complicated-command with lots of command line parameters
    

    and 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! ;-)

Fabby
  • 34,341
  • 38
  • 97
  • 191
  • 2
    +1, interesting, but: 1. `~/.inputrc` might be preferable over `/etc/inputrc`, and 2. I think you can set this in `bash` directly: http://unix.stackexchange.com/q/55203/70524, http://unix.stackexchange.com/a/16926/70524 – muru Apr 11 '15 at 07:45
  • This is quite nice, thanks (and have my upvote). Is there a universal way to show the options it'll loop through, which combines the best of both worlds? I like the tab+tab possibility with folders _and_ subcommands, etc. so I don't have to remember them all (e.g. `git branch `. However, if I could see a list _and_ tab through its items, that would be great! For directories and files `ls` is an option to see what items are available. However, for subcommands it is not that easy or obvious, unfortunately. – Erik Dec 21 '16 at 08:39
  • This works also on PuTTy! Just had to restart it once :) – Niko Föhr Nov 05 '17 at 20:33
  • @Erik: Apparently, what you asked for [is possible](https://unix.stackexchange.com/a/55632/90054) but not going to edit my answer to keep things simple here. (deleted old comment that it is not possible) – Fabby Sep 04 '18 at 16:14
  • How to put this **TAB** markdown? @Fabby – sh.3.ll Jun 12 '20 at 08:52
  • @sh.3.ll https://lmgtfy.com/?q=stack+overflow+markdown&s=t – Fabby Jun 12 '20 at 20:19
23

After the 1st tab you need to insert more letters. So if you type

cd a

and 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
Fabby
  • 34,341
  • 38
  • 97
  • 191
Rinzwind
  • 293,910
  • 41
  • 570
  • 710
  • 2
    IMHO: This is the right way. I personally find the windows behavior very annoying. Consider the case where you have a lot of files starting with `a` and you need the last one. When you accidentally press tab after `a` you have to cycle through the list of all possible completions to get to the right one. – Tobias May 04 '18 at 07:59
  • @Tobias: when you accidentally hit [Tab] too early [using the other system](https://askubuntu.com/a/607903/344926), there is still [Ctrl][K]... **;-)** – Fabby Sep 04 '18 at 16:17
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).

Arjuna Deva
  • 131
  • 2
1

Fig for linux is in beta: https://fig.io/user-manual/linux

It works very well and has many other features, Ubuntu is one of their supported distros.

Arjuna Deva
  • 131
  • 2