29

I've seen a plugin for Vim called AutoClose (discovered from this post) which automatically adds the closing brace when typing '(', '{' etc.

For example; when I type the following ( | is the cursor):

int main(|

I would like the closing ) to be inserted automatically for me:

int main(|)

Does anyone know of a similar feature for emacs - Google has failed me this time!

Community
  • 1
  • 1
DaveR
  • 9,540
  • 3
  • 39
  • 58

4 Answers4

17

There's also 'paredit. The cheat sheet shows you all the commands available. happen to like it better than the electric mode suggested in another answer. Though paredit does only apply to (), so it may not meed your needs.

But, to be honest, there's a bunch of packages surrounding parenthesis. The wiki has them all listed here. The modes addressing your question are:

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • And what if I need something more... Many editors have feature that auto close e.g. html tags and in addition insert empty line between them and do proper indentation. How can I achieve this in emacs? – jesper Nov 29 '10 at 09:57
  • 1
    @jesper Some of the modes listed might already do what you want. For sure the more general mode, yasnippet ( http://www.emacswiki.org/emacs/Yasnippet ) can do what you want. It's not really a parentheses balancing mode so I won't add it to the answer, but it can do what you're asking. – Trey Jackson Nov 29 '10 at 16:44
  • 2
    Starting from Emacs 24.4 . The author of `autopair` recommend to use `electric-pair-mode` instead. – azzamsa Jul 09 '18 at 03:35
16

yes, this mode is called electric. You can combine the electric behaviour with this simple macro for maximum confort:

(defun electric-pair ()
  "If at end of line, insert character pair without surrounding spaces.
   Otherwise, just insert the typed character."
  (interactive)
  (if (eolp) (let (parens-require-spaces) (insert-pair)) 
    (self-insert-command 1)))

Then enable it by binding the appropriate characters to it in your favorite programming modes. For example, for PythonMode:

(add-hook 'python-mode-hook
          (lambda ()
            (define-key python-mode-map "\"" 'electric-pair)
            (define-key python-mode-map "\'" 'electric-pair)
            (define-key python-mode-map "(" 'electric-pair)
            (define-key python-mode-map "[" 'electric-pair)
            (define-key python-mode-map "{" 'electric-pair)))

The CPerl mode provides this as a builtin:

;; from my .emacs
(add-hook 'cperl-mode-hook
  (lambda ()
    (setq cperl-hairy nil
      abbrev-mode t     ;; automatic keyword expansion
      cperl-highlight-variables-indiscriminately t
      cperl-auto-newline t
      cperl-auto-newline-after-colon t
      cperl-regexp-scan nil
      cperl-electric-keywords t 
      cperl-electric-linefeed t  
      cperl-electric-parens nil) ;; <------ electric parens!

Other modes could provides something similar.

dfa
  • 114,442
  • 31
  • 189
  • 228
  • 13
    The `electric-pair-mode` has been built in emacs 24 and can be used by `(electric-pair-mode 1)`. [link](http://ergoemacs.org/emacs/emacs_insert_brackets_by_pair.html). – liuminzhao Oct 25 '13 at 18:37
  • electric mode doesn't close single quotes and curly braces. Anyway to enable that? – Flame of udun Mar 13 '15 at 05:46
  • Just M-x customize group 'electricity' and look for 'electric-pair-pairs'. But be warned that setting this regardless your major mode may be unwise -- for example in text mode -- other major modes usually provide customization, ie. Python single quote works as expected (adds the ending one)). – bubak Apr 01 '15 at 09:20
6

I'm gonna necro this thread and provide another alternative. There's a recently started new project that deals with auto-insertion of pairs, wrapping of regions, navigation around balanced expressions and much much more. The list of features is too long to give here, so I'll just link to the smartparens github repo where you can read detailed readme.

It is superset of the aforementioned AutoPair and provide most of the core features of paredit (and extended to all kinds of different pairs, not only those recognized by emacs syntax-tables).

Matus Goljer
  • 61
  • 1
  • 3
  • Worth also noting: by default smartparens always inserts a pair, which may annoy. But it also allows for an alternative behavior, when it only inserts a pair based on a heuristic. For details see [this file](https://github.com/Fuco1/smartparens/blob/master/sp-sublimetext-like.el) that is distributed in smartparens package. – Hi-Angel Dec 17 '20 at 12:04
5

cmarcelo has written a wonderful post about this using skeleton mode. He also shows how to remove the balanced bracket if you delete the opening bracket and how to deal with the case that you accidentally type the closing bracket. (Incidentally both behaviors copy TextMate).

Update:

Since I posted this answer, I've discovered Autopair which is a pretty much perfect system for this use case. I've been using it a lot and loving it.

Singletoned
  • 5,089
  • 3
  • 30
  • 32
  • autopair doesn't close single quotes and curly braces. Anyway to enable that? – Flame of udun Mar 13 '15 at 05:49
  • @VineetKaushik It depends what mode you are using autopair in. It works fine for me in Python mode. Maybe you are using a mode where they aren't considered pairs. Can you give more details about what you are doing? – Singletoned Mar 13 '15 at 15:14