Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there any way to have bash autocomplete matching symbols like '"[{( with their right handed counterparts? I would like to do this without having to press tab.

For example typing the string echo " would immediately result in this string echo "|", where my cursor (the pipe) is in between the two quotes.

Bonus points if you can find a way that, when the first quote is deleted, the second one is also deleted.

If you've ever used any mature text editor (eg. sublime text), you can understand how this interaction works. Answers utilizing fish shell are also accepted.

share|improve this question

1 Answer 1

Although you can not bind keys on itself in GNU Readline — something like "(": "()" drives to endless loop — there is a way. Not for readline in general, only for bash. Add this to you .bashrc:

readline-brackets() {
    READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}${1}${READLINE_LINE:$READLINE_POINT}"
    ((READLINE_POINT+=1))
}
bind -m vi-insert -x '"\"" : "readline-brackets \"\""'
bind -m vi-insert -x $'"\047" : "readline-brackets \\\047\\\047"' # SINGLE QUOTE
bind -m vi-insert -x '"<" : "readline-brackets \<\>"'
bind -m vi-insert -x '"(" : "readline-brackets \(\)"'
bind -m vi-insert -x '"[" : "readline-brackets []"'
bind -m vi-insert -x '"{" : "readline-brackets {}"'

Remove -m vi-insert if you use emacs mode, not vi.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.