Automatically closing braces in Emacs?

前端 未结 4 1652
臣服心动
臣服心动 2020-12-05 13:30

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

相关标签:
4条回答
  • 2020-12-05 14:04

    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).

    0 讨论(0)
  • 2020-12-05 14:13

    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.

    0 讨论(0)
  • 2020-12-05 14:16

    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:

    • balancedel
    • electricdotanddash
    • universalcloseparen
    • electricpair
    • skeleton
    • parenthesis
    • AutoPairs
    0 讨论(0)
  • 2020-12-05 14:23

    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.

    0 讨论(0)
提交回复
热议问题