Emacs has a command check-parens to check for unbalanced parenthesis in a buffer. But I want to check for unbalanced parenthesis in a selected region (not the entire buffer) e.g. a region selected with the mouse. How do I do this?
1 Answer
You can use check-parens
on a region by narrowing to that region: select a region normally (either with the mouse or by marking one end with C-<space>
and moving to the other end) and then narrow to that region with C-x n n
. That makes the rest of the buffer invisible and check-parens
only operates on the visible portion of the buffer, so you can invoke it with M-x check-parens
to do its thing. You can then widen the buffer with C-x n w
. The doc string of check-parens
says:
Check for unbalanced parentheses in the current buffer. More accurately, check the narrowed part of the buffer for unbalanced expressions ("sexps") in general. This is done according to the current syntax table and will find unbalanced brackets or quotes as appropriate. (See Info node ‘(emacs)Parentheses’.) If imbalance is found, an error is signaled and point is left at the first unbalanced character.
Another alternative is it use sexp
motion commands to skip over a balanced "sexp" (that stands for S-expression
, a construct in Lisp - including Emacs Lisp - where these commands are most often used, but they are available everywhere: they use the syntax table
of the buffer in question and they are often the best way to deal with matched pairs of delimiters. You can move up, down and across such matched groups of parens[1]:
C-M-f
Move forward over a balanced expression (forward-sexp
).
C-M-b
Move backward over a balanced expression (backward-sexp
).
C-M-n
Move forward over a parenthetical group (forward-list
).
C-M-p
Move backward over a parenthetical group (backward-list
).
C-M-u
Move up in parenthesis structure (backward-up-list
).
C-M-d
Move down in parenthesis structure (down-list
).
See Expressions with balanced parentheses and Moving by parens in the Emacs manual.
It's worth reading the whole Commands for Editing with Parentheses
that contains the subsections above. You can read it on the web or you can use Info (which I highly recommend you familiarize yourself with, if you are not familiar with it already): C-h i g(emacs)m Parentheses
or even M-: (info "(emacs)Parentheses") RET
.
[1] BTW, AFAICS what you are describing in the question is a matched set of parens - they just happen to be separated by thousands of lines - is that correct? If so, the motion commands above should work fine, as should check-parens
. The commands that blink a matching paren are more limited IIRC.
makeindex
(or whatever it is called) on some.tex
file that they would have to write in order to produce an example that they can test in Emacs. Chances are that you are going to get an answer much faster that way. Put yourself in the place of a person who might be willing to answer if the work requires five minutes extra of her/his time, but if it requires half an hour, they might pass.Check for unbalanced parentheses in the current buffer. More accurately, check the narrowed part of the buffer for unbalanced expressions ("sexps") in general.
So select a region normally, narrow the buffer to the region withC-x n n
and thenM-x check-parens
. When you are done, widen the buffer:C-x n w
.C-M-f
, bound toforward-sexp
andC-M-b
, bound tobackward-sexp
in "unmached-paren" situations? You might find them useful.