Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

libreadline - Autoclose GNU Readline

Working on various GNU Readline-based CLIs and it would dramatically speed me up if there was a way to have brackets and quotes automatically closed when you type.

Thus typing a ' or ( on Bash (or other CLIs) would actually append the closing quote or bracket '' or () and place the cursor inbetween for writing.

I've looked around for quite some time trying to find out anything related (e.g. ~/.inputrc setting), but didn't find anything and I wonder if that's at all achievable. Any comments would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a bit tricky, but doable. As a bash command:

bind '"(" "C-v()e[D"'
bind '""" "C-v"C-v"e[D"'

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

"(": "C-v()e[D"
""": "C-v"C-v"e[D"

You can prefix each key with Control-v to type "plain" quotes and left parentheses without triggering the auto-close behavior.

The above assumes Emacs keybindings. For vi bindings, use

bind '"(": "C-v()ei"'
bind '""" "C-v"C-v"ei"'

or

"(": "C-v()ei"
 """: "C-v"C-v"ei"

Essentially, just replace the [D with i; instead of sending the escape sequence to move the cursor left, just send e to drop back into command mode after inserting the parentheses/quotes, then re-enter insert mode, which should position the cursor inside the characters just typed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...