其他提示
是的,此模式称为电动。您可以将电动行为与这个简单的宏结合起来,以获得最大的舒适度:
(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)))
然后通过在您喜欢的编程模式中将相应的字符绑定到它来启用它。例如,对于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)))
CPerl模式将此作为内置提供:
;; 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!
其他模式可以提供类似的东西。
cmarcelo已撰写了一篇关于此事的精彩帖子使用骨架模式。他还展示了如果删除开口支架以及如何处理意外键入右括号的情况,如何移除平衡支架。 (顺便说一下,这两种行为都复制了TextMate。)
更新
自从我发布此答案后,我发现 Autopair 非常完美这个用例的系统。我一直在使用它并喜欢它。
我要去修这个帖子并提供另一种选择。最近开始的一个新项目涉及自动插入对,区域包装,平衡表达式导航以及更多内容。功能列表太长了,所以我只需链接到 smartparens github repo ,您可以在其中阅读详细的自述文件
它是上述AutoPair的超集,并提供了paredit的大部分核心功能(并扩展到各种不同的对,而不仅仅是emacs语法表识别的那些)。
不隶属于 StackOverflow