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)?

  • Not that I'm aware of. In Zsh you can iterate over all the possible completions hitting TAB multiple times (i.e. e.g. hitting TAB 4 times will complete ant.txt). It can also be set up so that is possible to iterate backwards for a better navigation. That obviously implies changing the shell to Zsh though. If you consider that a good enough alternative I can write an answer for that. – kos Dec 8 '15 at 4:03

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.

  • Let's pretend you are using cd and cycling over directories. What key do you press to "pick" that directory and start cycling the contents of that directory? Is there another thing to set/bind? – Tony Jul 29 '16 at 21:27
  • An alternative, if you don't want to move your hands too far from keyboard's home row, is to use Ctrl+Alt+P instead of PageUp and Ctrl+Alt+N instead of PageDown, which can be done via bind '"\e\20": menu-complete-backward' and bind '"\e\16": menu-complete'. – Ruslan Sep 12 at 10:03

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.