简体   繁体   中英

Emacs Brace and Bracket Highlighting?

When entering code Emacs transiently highlights the matching brace or bracket. With existing code however is there a way to ask it to highlight a matching brace or bracket if I highlight its twin?

I am often trying to do a sanity check when dealing with compiler errors and warnings. I do enter both braces usually when coding before inserting the code in between, but have on occasion unintentionally commented out one brace when commenting out code while debugging.

Any advice with dealing with brace and bracket matching with Emacs?

OS is mostly Linux/Unix, but I do use it also on OS X and Windows.

If you're dealing with a language that supports it, give ParEdit a serious look. If you're not using with a Lisp dialect, it's not nearly as useful though.

For general brace/bracket/paren highlighting, look into highlight-parentheses mode (which color codes multiple levels of braces whenever point is inside them). You can also turn on show-paren-mode through customizations (that is Mx customize-variable show-paren-mode ); that one strongly highlights the brace/bracket/paren matching one at point (if the one at point doesn't match anything, you get a different color).

my .emacs currently contains (among other things)

(require 'highlight-parentheses)

(define-globalized-minor-mode global-highlight-parentheses-mode highlight-parentheses-mode
  (lambda nil (highlight-parentheses-mode t)))

(global-highlight-parentheses-mode t)

as well as that show-paren-mode customization, which serves me well (of course, I also use paredit when lisping, but these are still marginally useful).

tried on emacs 26

(show-paren-mode 1)
(setq show-paren-style 'mixed)
  • enable showing parentheses
  • set the showing in such as highlit the braces char., or if either one invisible higlight what they enclose

for toggling the cursor position / point between both, put this script in .emacs

(defun swcbrace ()(interactive)
  (if (looking-at "(")(forward-list)
  (backward-char)
  (cond
    ((looking-at ")")(forward-char)(backward-list))
    ((looking-at ".)")(forward-char 2)(backward-list))
  )))
(global-set-key (kbd "<C-next>") 'swcbrace)

it works toggling by press Control-Pgdn

除了直接来自手册维基的答案外,还可以查看autopair

BTW, for the immediate question: Mx blink-matching-open will "re-blink" for an existing close paren, as if you had just inserted it. Another way to see the matching paren is to use MCb and MCf (which jump over matched pairs of parens), which are also very useful navigation commands.

If you just want to check the balanced delimiters, be them parentheses, square brackets or curly braces, you can use backward-sexp (bound to Ctrl Alt B ) and forward-sexp (bound to Ctrl Alt F ) to skip backward and forward to the corresponding delimiter. These commands are very handy to navigate through source files, skipping structures and function definitions, without any buffer modifications.

You can set the below in your init.el :

(setq show-paren-delay 0)
(show-paren-mode 1)

to ensure matching parenthesis are highlighted.

Note that (setq show-paren-delay 0) needs to be set before (show-paren-mode 1) so that there's no delay in highlighting, as per the wiki .

If you want to do a quick check to see whether brackets in the current file are balanced: Mx check-parens

Both options tested on Emacs 27.1

I second ParEdit. it is very good atleast for lisp development.

FWIW I use this function often to go to matching paren (back and forth).

;; goto-matching-paren
;; -------------------
;; If point is sitting on a parenthetic character, jump to its match.
;; This matches the standard parenthesis highlighting for determining which
;; one it is sitting on.
;;
(defun goto-matching-paren ()
  "If point is sitting on a parenthetic character, jump to its match."
  (interactive)
  (cond ((looking-at "\\s\(") (forward-list 1))
        ((progn
           (backward-char 1)
           (looking-at "\\s\)")) (forward-char 1) (backward-list 1))))
(define-key global-map [(control ?c) ?p] 'goto-matching-paren) ; Bind to C-c p

Declaimer: I am NOT the author of this function, copied from internet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM