12

When I press escape 4 times in a bash terminal, it displays something like this:

-bash-4.1$
Display all 2837 possibilities? (y or n)
:
!
./
[
[[
]]
{
}
411toppm
a2p
ac
accept
accton
aclocal
aclocal-1.11
acpi_listen

What is this feature, and how are these entries found? (On the second esc press, the terminal gives an audible alert.)

flow2k
  • 561
  • 1
  • 8
  • 19

2 Answers2

12
$ bind -p | grep 'complete$'
"\C-i": complete
"\M-\e": complete

This shows that the default key binding of Meta+Esc (and Ctrl+i) in Emacs command line editing mode is the Readline function complete. The Meta key is usually Esc on keyboards without an explicit Meta key. The Readline documentation for this function says

Attempt to perform completion on the text before point. The actual completion performed is application-specific. Bash, for instance, attempts completion treating the text as a variable (if the text begins with $), username (if the text begins with ~), hostname (if the text begins with @), or command (including aliases and functions) in turn. If none of these produces a match, filename completion is attempted. Gdb, on the other hand, allows completion of program functions and variables, and only attempts filename completion under certain circumstances.


Regarding your comment to Anthon's answer: No, pressing Esc twice is not the same as pressing Tab generally (unless it's in a program that maps them both to the same action, as Readline does by default). However Ctrl+i is the same as Tab, just like Ctrl+[ is the same as Esc. This means that you can do completion with Ctrl+[ Ctrl+[ in bash if you wish, as long as double Esc is bound to the Readline complete function. This is handy if you're working at a VT220 terminal, for example, which lacks the Escape key:

German VT220 keyboard

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • Kusalananda, thank you for this in-depth answer. About your last point: why is pressing `Esc` twice (or `\M-\e`) not the same as `Tab` in bash, if both do completion? – flow2k Sep 13 '17 at 06:19
  • 2
    @flow2k It is the same if both Esc-Esc and Tab are bound to the `complete` function, which they are by default, but not generally (i.e. the key-presses are not "the same"). You can't substitute Tab with Esc-Esc everywhere, only in readline-capable programs. However, you may substitute Tab with Ctrl-i or Esc with Ctrl-[ anywhere. – Kusalananda Sep 13 '17 at 06:21
  • A followup if I may: I don't see `Tab` in the output of `bind -p`. Why is that? Maybe because `Tab` is "hardcoded" and can't be bound? – flow2k Sep 13 '17 at 06:36
  • 1
    @flow2k No, Tab is _identical_ to Ctrl-i. Look for `\C-i`. This is an equivalence on the hardware level. – Kusalananda Sep 13 '17 at 06:45
  • How to unbind this default mapping? – ruohola Jun 19 '20 at 08:13
  • Found the solution for unbinding: https://unix.stackexchange.com/a/527020/337515 – ruohola Jun 19 '20 at 15:16
8

That is called file completion, and if you don't have any preceding command on the commandline, bash will offer you to complete all 2837 commands it knows about, both built-in and the ones found in your PATH

The are presented in sorted order

Anthon
  • 78,313
  • 42
  • 165
  • 222