关于括号
如何自动插入双括号
开启`electric-pair-mode`这个minor mode后,插入左括号会自动补全右括号, 而删除一方括号不会删除另一个.
(electric-pair-mode 1)
默认补全的括号与各个major-mode的syntax table有关(可用通过describe-syntax查看). 要定义自己的括号列表可用编辑`electric-pair-pairs`
(setq electric-pair-pairs '( (?\" . ?\") (?\{ . ?\}) ) )
此外也可以定义自己的括号补全函数
(if (region-active-p) (let ( (p1 (region-beginning)) (p2 (region-end)) ) (goto-char p2) (insert rightBracket) (goto-char p1) (insert leftBracket) (goto-char (+ p2 2)) ) (progn (insert leftBracket rightBracket) (backward-char 1) ) ) ) (defun insert-pair-paren () (interactive) (insert-bracket-pair "(" ")") ) (defun insert-pair-bracket () (interactive) (insert-bracket-pair "[" "]") ) (defun insert-pair-brace () (interactive) (insert-bracket-pair "{" "}") ) (defun insert-pair-double-straight-quote () (interactive) (insert-bracket-pair "\"" "\"") ) (defun insert-pair-single-straight-quote () (interactive) (insert-bracket-pair "'" "'") ) (defun insert-pair-single-angle-quote () (interactive) (insert-bracket-pair "" "") ) (defun insert-pair-double-angle-quote () (interactive) (insert-bracket-pair "" "") ) (defun insert-pair-double-curly-quote“” () (interactive) (insert-bracket-pair "“" "”") ) (defun insert-pair-single-curly-quote‘’ () (interactive) (insert-bracket-pair "‘" "’") ) (defun insert-pair-corner-bracket「」 () (interactive) (insert-bracket-pair "「" "」") ) (defun insert-pair-white-corner-bracket『』 () (interactive) (insert-bracket-pair "『" "』") ) (defun insert-pair-angle-bracket〈〉 () (interactive) (insert-bracket-pair "〈" "〉") ) (defun insert-pair-double-angle-bracket《》 () (interactive) (insert-bracket-pair "《" "》") ) (defun insert-pair-white-lenticular-bracket〖〗 () (interactive) (insert-bracket-pair "〖" "〗") ) (defun insert-pair-black-lenticular-bracket【】 () (interactive) (insert-bracket-pair "【" "】") ) (defun insert-pair-tortoise-shell-bracket〔〕 () (interactive) (insert-bracket-pair "〔" "〕") )
如何删除一个括号自动删除另一个匹配括号
需要安装名为`autopairs`的package
如何高亮括号
开启show-paren-mode
(show-paren-mode 1)
show-paren-mode有两种高亮括号的方式:
(setq show-paren-style 'parenthesis) ; 只高亮括号 (setq show-paren-style 'expression) ; 高亮整个括号内的内容
在嵌套括号中翱翔
Emacs提供了多个命令在嵌套括号表达式中定位:
Key Command Purpose 【Ctrl+Alt+←】 backward-sexp Move to previous sibling (move to the (beginning of) previous sexp unit) 【Ctrl+Alt+→】 forward-sexp Move to next sibling (move to the (end of) next sexp unit) 【Ctrl+Alt+↑】 backward-up-list Move to parent (move to the (beginning of) outer paren pair) 【Ctrl+Alt+↓】 down-list Move to first child (move into the (beginning of) first inner paren pair)