autocomplete - How can I iterate through possible completions in gnu readline?

07
2014-07
  • urig

    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. 
    
  • Answers
  • Daniel Beck

    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
    
  • tapped-out

    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.


  • Related Question

    osx - Terminal tab completion
  • sa125

    Possible Duplicate:
    Bash completion for commands in Mac OS

    I'm just moved from linux to mac, and one of the problems I'm having is that tab completion is only 1 level deep, and I'm not sure how to correct that.

    Let me explain by example - when I used git on ubuntu, I'll start typing

    $ git
    

    and hit tab, and the shell would complete the command or display the options available for the letter\s I've typed. That's what happens on my mac as well (1 level). However, when I'd continue typing the next part of the command

    $ git commit
    

    and hit tab, mac terminal would not complete the next subcommand (for git) as it would in Linux. Same goes for other programs that support this multilevel command completion. I suspect this might be a readline library issue, but I'm not sure which one I should install.


  • Related Answers
  • hop

    Download bash_completion and source it from your .bashrc. Then put contrib/completion/git-completion.bash from the git distribution into bash_completion.d.

    Alternatively you can install it via MacPorts.

  • Aurélien Bottazini

    For git there is an option available on compilation to support completion.

    If you used port to install Git (which I recommend), you can reinstall git with this command

    sudo port install git-core +svn+bash_completion+doc
    

    Which will install git with bash completion, git-svn support and docs