Making M-( the default behavior in Emacs when “(” is typed – Education Career Blog

How does one make M-( the default behavior for typing an opening “(” character? I want Emacs to automatically insert the closing “)” after the cursor when I type a “(” character regardless of whether it’s part of an M-key combination. Additionaly, I want to extend this behavior to quotes, subquotes, brackets and braces. Typing M-( is a pain, and there don’t appear to be any comparable forms for those other characters.

,

I don’t write much elisp myself, but this is something I cribbed off somebody. The code goes into your .emacs.

(setq skeleton-pair t)
(setq skeleton-pair-on-word t) ; apply skeleton trick even in front of a word.                                               
(global-set-key "" 'skeleton-pair-insert-maybe)
(global-set-key "{" 'skeleton-pair-insert-maybe)
(global-set-key "(" 'skeleton-pair-insert-maybe)
(global-set-key "\"" 'skeleton-pair-insert-maybe)

,

Check out paredit.el which keeps parens/braces/quotes balanced as you wish, and also does offers many other features to assist with s-exp manipulation. If you’re going to be writing Lisp code (as your name implies) you will probably want to use this library eventually.

,

“(” is bound to self-insert-command while M-‘(‘ is insert-parenthesis. You can reverse that simply by using global-set-key or define-key to bind “(” to insert-parenthesis.

Leave a Comment