How can I iterate through possible completions in gnu readline?

2

1

In a shell that implements GNU Readline, I can click the key to get a list of possible completions to what I started typing. For example:

C:\>cd P<TAB>
PerfLogs\             Program Files (x86)\  Python27\
Program Files\        ProgramData\

How can I now choose which completion I wish to use?

I know that If I want "Program Files" in the above example, I can type in "rogram Files", but I'm lazy :).

Isn't there some way for me to hit a keyboard shortcut that will iterate through the possible completions so I can quickly choose one? Something similar to auto-completion / intellisense in modern IDEs?

Edit: Might my solution be to use GNU Readline's menu-complete command (described below)? But how do I bind it to a key combination?

menu-complete ()

    Similar to complete, but replaces the word to be completed with a single match
from the list of possible completions. Repeated execution of menu-complete steps 
through the list of possible completions, inserting each match in turn. At the end of 
the list of completions, the bell is rung (subject to the setting of bell-style) and 
the original text is restored. An argument of n moves n positions forward in the list 
of matches; a negative argument may be used to move backward through the list. This 
command is intended to be bound to TAB, but is unbound by default. 

urig

Posted 2013-08-03T13:44:50.470

Reputation: 387

1You could just continue by typing r Tab (which will complete to Program) and then \ Tab to get to Program Files. The \ might be annoying to represent a single space, which is related to the convention to avoid spaces in file names in general. In this case it would in total be six keystrokes to reach Program Files: Pr Tab \ Tab. I'm testing this in Bash; you are obviously on a Windows system, though, but I would guess it works in the same way there. I don't know how to enable "intellisense" like completion. – Daniel Andersson – 2013-08-03T14:05:43.300

@DanielAndersson Windows tab-complete works differently; each Tab gives you a different option, unlike bash, which requires you to input another character to narrow it down – nc4pk – 2013-08-03T14:11:37.197

Answers

5

In a shell that uses readline, you need to bind menu-complete to a key. It is not bound by default, complete is.

To do this, edit ~/.inputrc and add the following:

TAB: menu-complete

This will probably affect all programs that use readline. Use the following to only have this apply to bash:

$if Bash
   TAB: menu-complete
$endif

Daniel Beck

Posted 2013-08-03T13:44:50.470

Reputation: 98 421

You should not modify clink_inputrc_base as it will be overwritten when you upgrade clink. That file contains the following lines: $include ~/clink_inputrc, $include ~/_inputrc, $include ~/.inputrc, so you may rather use any of those files in your HOME directory. – Dawid Ferenczy Rogožan – 2018-03-09T16:34:02.260

Many thanks! Any idea what do I do if I'm on Windows, using clink? :) http://code.google.com/p/clink/

– urig – 2013-08-03T14:37:34.440

1@urig It should use the path in the INPUTRC environment variable, or the file _inputrc in your home directory. See rl_read_init_file in readline/readline/bind.c – Daniel Beck – 2013-08-03T14:45:16.560

Found it! For clink, the inputrc file is somwhere like: C:\Program Files (x86)\clink\0.3.1\clink_inputrc_base – urig – 2013-08-03T14:46:36.470

1@urig True -- just wanted to add that. This is what clink sets the INPUTRC environment variable to in clink/clink/dll/rl_env.c. It even appears to ignore any predefined INPUTRC as well, always overriding it. – Daniel Beck – 2013-08-03T14:49:39.093

1

Command-line completion works in two different ways, depending on platform.

Windows (NT and later)

First of all, the Windows command processor (cmd.exe) does not implement GNU Readline. Despite this, it does support tab-completion.

Specifically, cmd.exe utilizes "rotating completion", where each Tab presents a different option.

In your example, pressing Tab would first give you PerfLogs, then Program Files, etc.

bash (and other Unix shells)

Most Unix shells utilize "prompting completion", where, as Daniel said above, you have to input another character to narrow the completion down.

See this section of the linked-above Wikipedia article for more details.

nc4pk

Posted 2013-08-03T13:44:50.470

Reputation: 8 261

Thanks, I did not know how Windows handled this. Haven't used the command-line in that context extensively since the actual MS-DOS days. – Daniel Andersson – 2013-08-03T14:26:59.067

Actually, Windows command line iterates through completion options - This is where I've gotten used to this functionality :) – urig – 2013-08-03T14:42:03.197

0

Specifically regarding this part of the question

Isn't there some way for me to hit a keyboard shortcut that will iterate through the possible completions so I can quickly choose one? Something similar to auto-completion / intellisense in modern IDEs?

I'd say fzf is what you might want to investigate. Its a pretty fast fuzzy find and selection tool.

Red Pill

Posted 2013-08-03T13:44:50.470

Reputation: 101

Welcome to Superuser. Please try to contain most relevant information from the link in your post. Read more about it here.

– styrofoam fly – 2017-06-27T18:27:07.453