Getting “complete” and “menu-complete” to work together

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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


Tab


$ 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):


/etc/inputrc


menu-complete


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


/etc/inputrc


readline


Ctrl-Tab: complete



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


Tab


Ctrl-Tab


complete



Does anyone know what I am doing wrong?



Thanks in advance!





On a system I have handy, man readline suggests that the abbreviation for "Control" is C, not Ctrl. (Disclaimer: I haven't tested.)
– ruakh
Aug 20 '12 at 20:47


man readline


C


Ctrl




4 Answers
4



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!





Let's pretend you are using cd and menu-complete 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:31


cd


menu-complete



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.


menu-complete-backward


complete


# 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.


quoted-insert


quoted-insert





In ~/.bash_profile use: bind TAB:menu-complete and bind '"e[Z": menu-complete-backward' (note the usage of single and double quotes!).
– Erik
15 hours ago


~/.bash_profile


bind TAB:menu-complete


bind '"e[Z": menu-complete-backward'



I'm not sure Ctrl-Tab is a real character; my terminal, for instance, ignores the combination. I think the only way to use Ctrl-Tab is to use your terminal emulator to map it to some otherwise unused escape sequence, then bind that sequence to complete.


complete



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



In your .inputrc


.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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered