Auto Lock GNU Readline

Working with various CLIs based on the GNU Readline, and this will greatly speed up me if there is a way to have brackets and quotes automatically closes when I type.

So by typing ' or ( in the Bash (or other CLI), actually add the final quote or bracket '' or () and place the inbetween cursor to write.

I looked around for quite some time, trying to find out something related (for example, ~/.inputrc ), but couldn’t find anything, and I’m interested in everything that’s achievable. Any comments would be appreciated.

+3
libreadline
Jul 14 '12 at 19:25
source share
2 answers

It is a bit complicated, but doable. Like the bash command:

 bind '"(" "\Cv()\e[D"' bind '"\"" "\Cv\"\Cv\"\e[D"' 

As a parameter in .inputrc (so any program using readline gets the behavior):

 "(": "\Cv()\e[D" "\"": "\Cv\"\Cv\"\e[D" 

You can prefix each key with Control-v to enter “simple” quotes and left parentheses without triggering automatic closure.

The above implies Emacs bindings. For vi bindings use

 bind '"(": "\Cv()\ei"' bind '"\"" "\Cv\"\Cv\"\ei"' 

or

 "(": "\Cv()\ei" "\"": "\Cv\"\Cv\"\ei" 

Essentially, just replace [D with i ; instead of sending an escape sequence to move the cursor to the left, just send \e to return to command mode after inserting the brackets / quotes, then re-enter the insert mode, which should place the cursor inside the characters just entered.

+3
Jul 26 2018-12-12T00:
source share

Doing exactly what you want is impossible, but there is work. Put this in inputrc:

 "\Cx\"": "\"\"Cb" 

Run:

 info readline "comm" "readline init" "sample" 

for the whole sample.

0
Jul 25 '12 at 16:10
source share



All Articles