Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "complete" and "menu-complete" to work together

I found out that the Bash shell supports a type of autocompletion that is different from the "traditional" autocompletion, where all possibilities get listed on the following line.

With the "traditional" autocompletion, if I type ch and then press the Tab key, I get something like:

$ ch
chacl  chgrp  chmod  chown  chvt

But if I add the following line to my /etc/inputrc (which remaps the Tab key to the built-in menu-complete function):

Tab: menu-complete

then the behavior of the shell changes: the word to be completed is replaced "inline" with a single match from the list of possible completions, and if I press the Tab key again, the word gets replaced with the next match.

I found this useful, but I still wanted to keep the traditional autocompletion and have it bound to the key combination Ctrl + Tab. So I added the following line to my /etc/inputrc file, according to what the readline library documentation suggests:

Ctrl-Tab: complete

However, adding this line only seems to make both Tab and Ctrl-Tab call the traditional complete function.

Does anyone know what I am doing wrong?

Thanks in advance!

like image 233
kYuZz Avatar asked Aug 20 '12 20:08

kYuZz


3 Answers

To start with, I'm not a massive expert in this area, but I think I can answer your question. First of all, while you are using Bash, Bash is a shell which interprets keyboard commands that it receives from a terminal / console. While you are informing Bash how to react to specific key combinations in the inputrc file, your Terminal determines precisely which character is 'sent' to the Shell before the inputrc file even enters the equation.

Unfortunately, on my system (granted, it's OSX - but I don't think this is strange behaviour when compared to Linux), both Tab and Ctrl-Tab send the same keyboard input to the shell. Infact, both Tab and Ctrl-Tab send a Ctrl-I command to the shell, and indeed, if I enter Ctrl-I when using the terminal, it performs the completion as if I hit Tab.

The software (installed on most Linux systems by default), showkey will tell you what keys the shell is receiving when you press specific keyboard inputs as you push them.

Anyway, my suggestion to you is to use Shift-Tab, which does appear to send it's own key-code to the shell. Shift-Tab on my computer shows up (using showkey) as '<ESC>[Z', which I think is pretty standard across the board. As such, your inputrc file with the following bindings should allow you to use shift-tab instead of ctrl-tab to achieve what you desire:

Tab: menu-complete
"\e[Z": complete

The \e in the second binding represents the escape character, and the [Z are simply the characters as shown using showkey. You can get a similar effect on OSX by simply using cat, running cat from within a terminal and pressing Shift-Tab will show you "^[[Z", where ^[ represents the escape character and the other characters are as before.

I know this doesn't resolve your question precisely, but as I don't think you are able to use Ctrl-Tab as a key combination, without re-mapping Ctrl-Tab to another keybinding within your terminal (more likely to be easier if you are using a GUI terminal), this is likely as close as you can get without significant effort!

like image 142
John Wordsworth Avatar answered Nov 18 '22 00:11

John Wordsworth


I have ShiftTab bound to menu-complete-backward, so it goes back one step if I skipped the right completion, and I've mapped Ctrlq to complete, so if there are several possible completions I hit Ctrlq to list them without having to cycle through them.

# Make Tab cycle between possible completions
# Cycle forward: Tab
# Cycle backward: Shift-Tab

TAB: menu-complete
"\e[Z": menu-complete-backward

# Make C-q display the list of possible completions

Control-q: complete

# Display the list of matches when no further completion is possible

set show-all-if-unmodified on

Edit: Ctrlq is bound to quoted-insert by default, that is, it tells the shell to take the next key literally. quoted-insert is also bound to Ctrlv, so you don't lose that functionality if you rebind Ctrlq. Anyway, I've found that AltESC also works, by default, for showing the possible completions (as far as I can tell it is equivalent to TAB); note that it may be seized by Gnome, then either double press ESC or rebind "Switch windows directly" in Settings → Devices → Keyboard → Navigation.

like image 11
Arch Stanton Avatar answered Nov 17 '22 23:11

Arch Stanton


The following should achieve what you're looking for (if I understand correctly!)

In your .inputrc

# display all possible matches for an ambiguous pattern at first tab
set show-all-if-ambiguous on

# next tab(s) will cycle through matches
TAB: menu-complete
# shift tab cycles backward
"\e[Z": menu-complete-backward
like image 6
Tate Thurston Avatar answered Nov 18 '22 01:11

Tate Thurston