Ubuntu – Select a particular result from completion suggestions


Let's say I have 3 files in a particular directory: abc.txt, aww.txt, ant.txt

If I do: cat a (and press tab to see options) terminal will display the 3 file names: abc.txt, aww.txt, ant.txt

Is there any way to select the nth result rather than typing, by specifying the index number in the results the terminal displayed?

something like: cat a2 to get the second file (aww.txt)?

Best Answer

Assuming you are using bash, either add the following to your ~/.bashrc:

bind '"\e[6~": menu-complete'
bind '"\e[5~": menu-complete-backward'

Or to change the defaults for all programs that use the readline completion library, not just bash, create the file ~/.inputrc containing:

$include /etc/inputrc
"\e[6~": menu-complete
"\e[5~": menu-complete-backward

Now when you open a new terminal, you can press:

  • TAB as normal to see available completions.
  • Pg Dn to select the first completion or change to the next one.
  • Pg Up to select the last completion or change to the previous one.
  • Alt+n Pg Dn to select the nth completion or move forward n completions.

So for your example type Pg DnPg Dn or Alt+2Pg Dn to get the second file aww.txt.

Related Question