I like show-paren-mode in Emacs, but I would really like to change the highlighting behavior for closing brackets.

That is, I want the opening bracket to be highlighted when the point is on the closing bracket. The default behavior highlights the opening bracket when the point is on the character following the closing bracket.

Is this easy to change? Also, I would be interested in potential benefits of keeping the show-paren-mode behavior as it is.


As of Emacs 24.3, this functionality is not available in Show Paren mode.

Here's some completely untested code (typed directly in my browser) that tweaks Show Paren mode to match a closing parenthesis before the cursor instead of after.

(defadvice show-paren-function 
  (around show-paren-closing-before
          activate compile)
  (if (eq (syntax-class (syntax-after (point))) 5)

This picks up closing parentheses before the cursor as well, but if the cursor is on a closing parenthesis that follows a closing parenthesis, the closing parenthesis under the cursor has precedence. Fixing this to never look at a closing parenthesis before the cursor looks tricker (it could be done with a gross hack such as (flet ((char-syntax …)) ad-do-it)).

| improve this answer | |
  • I tried this, but unfortunately, it made the behavior I want to remove occur on the opening parenthesis in addition to the closing one. Additionally, I'm fairly new to Elisp, but I've seen people advise (no pun intended) against using defadvice. Would its use be justified in this instance? – bitflips Nov 5 '13 at 4:12
  • 1
    @mellowmaroon I was going in the wrong direction, I've fixed the bug. Regarding defadvice, it redefines a function. That can be risky if you change the behavior of a function that's used by other functions, but it's ok to redefine functions that are only called manually or in known contexts, such as here. defadvice exists for precisely this sort of things. – Gilles 'SO- stop being evil' Nov 6 '13 at 1:10
  • Can I also apply this to highlight closing bracket when point is on the beginning braker foo(^ )? @Gilles'SO-stopbeingevil' – alper Jun 17 at 1:48

In 25.1, there is a variable enabling that:

(setq show-paren-when-point-inside-paren t)

| improve this answer | |

You can provide your own function as the value of show-paren-data-function:

| show-paren-data-function is a variable defined in `paren.el'.
| Its value is show-paren--default
|   This variable can be risky when used as a file-local variable.
| Documentation:
| Function to find the opener/closer at point and its match.
| The function is called with no argument and should return either nil
| if there's no opener/closer at point, or a list of the form
| Where HERE-BEG..HERE-END is expected to be around point.

See the definition of show-paren--default for inspiration.

As for the advantage: you see the matching opening paren each time you add a closing paren. Makes sense, no?

| improve this answer | |
  • Where is show-paren-data-function? It isn't in the current Emacs 24 tree. – Gilles 'SO- stop being evil' Nov 4 '13 at 22:28
  • It is in recent development versions of Emacs 24. It is not in Emacs 24.3. If you have 24.3 or earlier then advise or redefine show-paren-function. The newer code for show-paren-function uses show-paren-data-function. – Drew Nov 5 '13 at 3:06
  • I'm sorry, but I don't fully understand your answer. Does this method entail modifying paren.el? – bitflips Nov 5 '13 at 4:07
  • If your Emacs has show-paren-function then you can use that to define the behavior you want. If not then you can redefine show-paren-function (or advise it). It is generally better to do that in your own code, not by modifying paren.el directly. – Drew Nov 5 '13 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.