Weekly Emacs tip #14: Highlighting parentheses and the region they enclose
One of the features that Emacs has to help you with your code is show-paren-mode
. By default this global minor-mode is turned on, which means that Emacs will highlight matching parentheses. Take the following R expression, for example:
myfun <- function(parA, parB) { paste("Doing nothing") }
Here, if the cursor is between the last n
of function
and the opening parenthesis, it will highlight the closing parenthesis following parB
. Similarly, if the cursor is positioned directly after the }
, the opening {
will be highlighted, see the light blue background colour in the screenshot.
But there is more! The variable show-paren-style
accepts three values, each of which change the way the expression between the matching parentheses should be highlighted (or not). The three styles are parenthesis
, expression
and mixed
. The first one is the default and acts as discussed above. The second, expression
doesn’t just highlight the two parentheses, but the whole expression between them, and mixed
is indeed a sort of mix of the other two: it highlights the parentheses if the whole expression fits in the current window. If, however, the opening or closing parenthesis is off-screen, it will highlight the expression.
In my Emacs config file, I explicitly set show-paren-mode
to t
(this might be a left-over from older versions of Emacs where t
wasn’t the default) and set the style to mixed
. As these are part of Emacs itself, I have put them in the use-package
call for emacs
(together with a bunch of other settings that, for clarity, I haven’t shown here).
(use-package emacs :custom ;; Code display options (highlight parens) (show-paren-mode 1) ;; Three options for paren-style: 'expression, 'parenthesis, and ;; 'mixed The first one highlights the complete region between ;; parens, the second only highlights the matching paren, the third ;; does 'expression when the matching paren is not visible. (show-paren-style 'mixed) )
No Comments